numpy

Indexing 3d numpy array with 2d array

假如想象 提交于 2021-02-09 06:49:15
问题 I would like to create a numpy 2d-array based on values in a numpy 3d-array, using another numpy 2d-array to determine which element to use in axis 3. import numpy as np #-------------------------------------------------------------------- arr_3d = np.arange(2*3*4).reshape(2,3,4) print('arr_3d shape=', arr_3d.shape, '\n', arr_3d) arr_2d = np.array(([3,2,0], [2,3,2])) print('\n', 'arr_2d shape=', arr_2d.shape, '\n', arr_2d) res_2d = arr_3d[:, :, 2] print('\n','res_2d example using element 2 of

Python Numpy Loadtxt - Convert unix timestamp

。_饼干妹妹 提交于 2021-02-09 04:57:16
问题 I have a text file with many rows of data - the first piece of data in each row is a unix timestamp such as 1436472000 . I am using numpy.loadtxt and in the parameters for converters I want to specify for it to convert the timestamp into whatever numpy understands as a date time. I know this needs to go after the 0: in the curly brackets, but I can't work out how to convert it. I know a converter can be used from matplotlib.dates.strpdate2num for normal dates, but I this won't work for unix

I have a problem with plotting sphere and a curve on it

旧街凉风 提交于 2021-02-08 23:43:22
问题 I am trying to plot a curve on a sphere but I can not plot them at the same time. I identified some points with Euclidean norm 10 for my curve, and some other points to plot the sphere of radius 10 , respectively as following. Points for curve: random_numbers=[] basevalues=np.linspace(-0.9,0.9,100) for i in range(len(basevalues)): t=random.random() random_numbers.append(t*10) xvalues=[random_numbers[i]*np.cos(basevalues[i]) for i in range(len(basevalues))] yvalues=[random_numbers[i]*np.sin

Remove zero from each column and rearranging it with python pandas/numpy

旧时模样 提交于 2021-02-08 23:42:34
问题 I am a total novice in python and currently I am stumbled with a simple but tricky situation. Is it possible to remove all these zeroes and rearrange the column from this : A B C D E F 10 10 5 0 0 0 0 0 0 13 3 4 0 13 41 55 0 0 0 0 31 30 21 0 11 19 20 0 0 0 To be something like this: A B C 10 10 5 13 3 4 13 41 55 31 30 21 11 19 20 回答1: Assuming all rows have the same amount of zeros: a = df.to_numpy() a = a[a!=0].reshape(-1,3) pd.DataFrame(a, columns=df.columns[:a.shape[1]]) A B C 0 10 10 5 1

Remove zero from each column and rearranging it with python pandas/numpy

╄→гoц情女王★ 提交于 2021-02-08 23:39:36
问题 I am a total novice in python and currently I am stumbled with a simple but tricky situation. Is it possible to remove all these zeroes and rearrange the column from this : A B C D E F 10 10 5 0 0 0 0 0 0 13 3 4 0 13 41 55 0 0 0 0 31 30 21 0 11 19 20 0 0 0 To be something like this: A B C 10 10 5 13 3 4 13 41 55 31 30 21 11 19 20 回答1: Assuming all rows have the same amount of zeros: a = df.to_numpy() a = a[a!=0].reshape(-1,3) pd.DataFrame(a, columns=df.columns[:a.shape[1]]) A B C 0 10 10 5 1

Remove zero from each column and rearranging it with python pandas/numpy

末鹿安然 提交于 2021-02-08 23:39:28
问题 I am a total novice in python and currently I am stumbled with a simple but tricky situation. Is it possible to remove all these zeroes and rearrange the column from this : A B C D E F 10 10 5 0 0 0 0 0 0 13 3 4 0 13 41 55 0 0 0 0 31 30 21 0 11 19 20 0 0 0 To be something like this: A B C 10 10 5 13 3 4 13 41 55 31 30 21 11 19 20 回答1: Assuming all rows have the same amount of zeros: a = df.to_numpy() a = a[a!=0].reshape(-1,3) pd.DataFrame(a, columns=df.columns[:a.shape[1]]) A B C 0 10 10 5 1

macOS Big Sur python3 cannot import numpy due to polyfit error

最后都变了- 提交于 2021-02-08 20:49:46
问题 update from Jan 2021: I performed a clean install of Big Sur in Jan 2021, and upgrade pip to latest version using python3 -m pip install --upgrade pip --user , and installed numpy without issues, and without the error message below. original question from Nov 2020 I'm using macOS big sur on 2019 macbook pro. I'm able to install numpy using python3 -m pip install numpy --user . Note I do not have brew; I'm just using the python3 from the Xcode command line tools. I've seen this error reported

Why is vectorized numpy code slower than for loops?

让人想犯罪 __ 提交于 2021-02-08 19:54:46
问题 I have two numpy arrays, X and Y , with shapes (n,d) and (m,d) , respectively. Assume that we want to compute the Euclidean distances between each row of X and each row of Y and store the result in array Z with shape (n,m) . I have two implementations for this. The first implementation uses two for loops as follows: for i in range(n): for j in range(m): Z[i,j] = np.sqrt(np.sum(np.square(X[i] - Y[j]))) The second implementation uses only one loop by vectorization: for i in range(n): Z[i] = np

Why is vectorized numpy code slower than for loops?

元气小坏坏 提交于 2021-02-08 19:54:07
问题 I have two numpy arrays, X and Y , with shapes (n,d) and (m,d) , respectively. Assume that we want to compute the Euclidean distances between each row of X and each row of Y and store the result in array Z with shape (n,m) . I have two implementations for this. The first implementation uses two for loops as follows: for i in range(n): for j in range(m): Z[i,j] = np.sqrt(np.sum(np.square(X[i] - Y[j]))) The second implementation uses only one loop by vectorization: for i in range(n): Z[i] = np

fastest way to load images in python for processing

丶灬走出姿态 提交于 2021-02-08 15:36:15
问题 I want to load more than 10000 images in my 8gb ram in the form of numpy arrays.So far I have tried cv2.imread,keras.preprocessing.image.load_image,pil,imageio,scipy.I want to do it the fastest way possible but I can't figure out which on is it. 回答1: One of the Fastest way is to get your multiprocessors do your job in Parallel it asks for parallelisation of your desired job, it brings multiple processors to work on your tasks at the same time when concurrent running isn't an issue. This