numpy

convert array of tuples to 2 dimensional array

て烟熏妆下的殇ゞ 提交于 2021-02-08 04:51:37
问题 I have an array of tuples loaded from a csv file using np.genfromtxt() function. import numpy as np import re from matplotlib.dates import strpdate2num def convert_string_to_bigint(x): p = re.compile(r'(\d{4})/(\d{1,2})/(\d{1,2}) (\d{1,2}):(\d{2}):\d{2}') m = p.findall(x) l = list(m[0]) l[1] = ('0' + l[1])[-2:] l[2] = ('0' + l[2])[-2:] return long("".join(l)) #print convert_string_to_bigint("2012/7/2 14:07:00") csv = np.genfromtxt ('sr00-1min.txt', delimiter=',', converters={0:convert_string

Reproducing Excel's LINEST function with NumPy

≯℡__Kan透↙ 提交于 2021-02-08 04:45:54
问题 I have to use Excel's LINEST function to compute error in my linear regression. I was hoping to reproduce the results using Numpy's polyfit function. I was hoping to reproduce the following LINEST usage: LINEST(y's, x's,,TRUE) with polyfit. I'm not sure how I can get the two functions to produce the same values because nothing I've tried gives similar results. I tried the following: numpy.polyfit(x,y,3) and various other values in the third position. 回答1: This question is actually a result of

Vectorize numpy array for loop

 ̄綄美尐妖づ 提交于 2021-02-08 04:41:50
问题 I'm trying to figure out how to vectorize the following loop: for i in range(1,size): if a[i] < a[i-1]: b[i] = a[i] else: b[i] = b[i-1] b is a (large) array of the same size as a. I could use numpy.where(a[1:]<a[:-1]) to replace the if statement but how do you simultaneously replace the else statement? 回答1: I think you want something like this: import numpy as np def foo(a, b): # cond is a boolean array marking where the condition is met cond = a[1:] < a[:-1] cond = np.insert(cond, 0, False)

图像识别之物体识别

橙三吉。 提交于 2021-02-08 04:05:33
''' 物体识别 ''' import cv2 as cv import os import warnings import numpy as np import hmmlearn.hmm as hl warnings.filterwarnings( ' ignore ' , category= DeprecationWarning) np.seterr(all = ' ignore ' ) def search_objects(directory): directory = os.path.normpath(directory) if not os.path.isdir(directory): raise IOError( ' the directory ' + directory + ' doesnt exist! ' ) objects = {} for curdir, subdirs, files in os.walk(directory): for jpeg in (file for file in files if file.endswith( ' .jpg ' )): path = os.path.join(curdir, jpeg) label = path.split(os.path.sep)[-2 ] if label not in objects:

Using NumPy with JyNI

耗尽温柔 提交于 2021-02-08 03:46:25
问题 I am trying to use a Python program with Java. My Python program is as follows: import CostCalculatorType import os from Evaluate.read_input_data import * class CostCalculator(CostCalculatorType, object): def __init__(self): print 'Initializing' pass def calculateCost(self, chromosome): inputData = ReadInputData(chromosome, 'input_data.txt') return inputData I am calling this from a Java interface. I am using the following command as specified on JyNI.org: java -cp /home/ch/jython.jar:/home

generate a 2D array of numpy.random.choice without replacement

痞子三分冷 提交于 2021-02-08 03:38:34
问题 I'm tyring to make my code faster by removing some for loops and using arrays. The slowest step right now is the generation of the random lists. context: I have a number of mutations in a chromosome, i want to perform 1000 random "chromosomes" with the same length and same number of mutation but their positions are randomized. here is what I'm currently running to generate these randomized mutation positions: iterations=1000 Chr_size=1000000 num_mut=500 randbps=[] for k in range(iterations):

eig(a,b) in Python giving error “takes 1 positional argument but 2 were given”

假如想象 提交于 2021-02-08 03:29:48
问题 According to https://docs.scipy.org/doc/numpy-1.15.0/user/numpy-for-matlab-users.html, the equivalent numpy expression for the MATLAB [V,D]=eig(a,b) is V,D = np.linalg.eig(a,b) . But when I try this I get the error: TypeError: eig() takes 1 positional argument but 2 were given I'm confused, the documentation says np.linalg.eig can take two arguments? Curiously, when I look at the linalg documentation at https://docs.scipy.org/doc/numpy-1.15.1/reference/routines.linalg.html, under the heading

How to replace value in specific index in each row with corresponding value in numpy array

心已入冬 提交于 2021-02-08 03:29:07
问题 My dataframe looks like this: datetime1 datetime2 datetime3 datetime4 id 1 5 6 5 5 2 7 2 3 5 3 4 2 3 2 4 6 4 4 7 5 7 3 8 9 and I have a numpy array like this: index_arr = [3, 2, 0, 1, 2] This numpy array refers to the index in each row, respectively, that I want to replace. The values I want to use in the replacement are in another numpy array: replace_arr = [14, 12, 23, 17, 15] so that the updated dataframe looks like this: datetime1 datetime2 datetime3 datetime4 id 1 5 6 5 14 2 7 2 12 5 3

eig(a,b) in Python giving error “takes 1 positional argument but 2 were given”

♀尐吖头ヾ 提交于 2021-02-08 03:29:00
问题 According to https://docs.scipy.org/doc/numpy-1.15.0/user/numpy-for-matlab-users.html, the equivalent numpy expression for the MATLAB [V,D]=eig(a,b) is V,D = np.linalg.eig(a,b) . But when I try this I get the error: TypeError: eig() takes 1 positional argument but 2 were given I'm confused, the documentation says np.linalg.eig can take two arguments? Curiously, when I look at the linalg documentation at https://docs.scipy.org/doc/numpy-1.15.1/reference/routines.linalg.html, under the heading

Sliding windows along last axis of a 2D array to give a 3D array using NumPy strides

青春壹個敷衍的年華 提交于 2021-02-08 02:03:46
问题 I am trying to use the function as_strided from numpy.lib.stride_tricks to extract sub series from a larger 2D array, but I struggled to find the right thing to write for the strides argument. Let's say I have a matrix m which contains 5 1D array of length ( a= )10. I want to extract sub 1D arrays of length ( b= )4 for each 1D array in m . import numpy from numpy.lib.stride_tricks import as_strided a, b = 10, 4 m = numpy.array([range(i,i+a) for i in range(5)]) # first try sub_m = as_strided(m