numpy-ndarray

Numpy multidimensional indexing for np.ufunc.at and np.ix_

拜拜、爱过 提交于 2021-01-29 10:01:00
问题 I would like to know how I can take index from an array and multiply with another array. I have two 4d arrays and one 2d index array: base = np.ones((2, 3, 5, 5)) to_multiply = np.arange(120).reshape(2, 3, 4, 5) index = np.array([[0, 2, 4, 2], [0, 3, 3, 2]]) The row index of the index array corresponds to the 1st dimension of base and to_multiply, and the value of the index array corresponds to the 3rd dimension of base. I want to take the slice from base according to the index and multiply

array is not callable in python “'numpy.ndarray' object is not callable”

左心房为你撑大大i 提交于 2021-01-29 08:24:35
问题 I am working on a neural network and when i try to shuffle the two numpy.ndarray i get this error. I tried rechecking the shuffle function format and cannot find any faults with that. Please help train_images,train_labels = shuffle(train_images,train_labels) TypeError Traceback (most recent call last) <ipython-input-8-b3f4173331ac> in <module> 18 print("Training the Network") 19 for i in range(epoch): 20 --> train_images,train_labels = shuffle(train_images,train_labels) 21 for offset in range

How to make Default Choice for np.select() a Previous Value of an Array, Series, or DataFrame

拥有回忆 提交于 2021-01-28 13:36:40
问题 I am using np.select() to construct an ndarray with values of either 1, -1, or 0, depending on some conditions. It is possible that none of these will be met, so I need a default value. I would like this value to be the value that the array holds in the previous index, if that makes sense. My naive code, which runs on some columns of a DataFrame named "total" and which raises an error, is below: condlist = [total.ratios > total.s_entry, total.ratios < total.b_entry, (total.ratios > total.b

How to make Default Choice for np.select() a Previous Value of an Array, Series, or DataFrame

血红的双手。 提交于 2021-01-28 13:35:58
问题 I am using np.select() to construct an ndarray with values of either 1, -1, or 0, depending on some conditions. It is possible that none of these will be met, so I need a default value. I would like this value to be the value that the array holds in the previous index, if that makes sense. My naive code, which runs on some columns of a DataFrame named "total" and which raises an error, is below: condlist = [total.ratios > total.s_entry, total.ratios < total.b_entry, (total.ratios > total.b

Can I find out if one numpy vector appears as a slice of another?

眉间皱痕 提交于 2021-01-28 11:51:46
问题 I want to find out if my numpy vector, needle , appears inside another vector, haystack , as a slice, or contiguous sub-vector. I want a function find(needle, haystack) that returns true if and only if there are possible integer indexes p and q such that needle equals haystack[p:q] , where "equals" means elements are equal at all positions. Example: find([2,3,4], [1,2,3,4,5]) == True find([2,4], [1,2,3,4,5]) == False # not contiguous inside haystack find([2,3,4], [0,1,2,3]) == False #

How can I generate data which will show inverted bell curve for normal distribution

瘦欲@ 提交于 2021-01-27 11:22:41
问题 I have generated random data which follows normal distribution using the below code: import numpy as np import matplotlib.pyplot as plt import seaborn as sns rng = np.random.default_rng() number_of_rows = 10000 mu = 0 sigma = 1 data = rng.normal(loc=mu, scale=sigma, size=number_of_rows) dist_plot_data = sns.distplot(data, hist=False) plt.show() The above code generates the below distribution plot as expected: If I want to create a distribution plot that is exactly an inverse curve like below

Convert C++ vector to numpy array in Cython without copying [duplicate]

雨燕双飞 提交于 2021-01-27 04:45:49
问题 This question already has answers here : Passing C++ vector to Numpy through Cython without copying and taking care of memory management automatically (2 answers) Closed last year . There is a C++ function that returns a vector of floats. How to convert this vector to NumPy array without copying? Now I'm doing this: cdef np.ndarray arr = np.ascontiguousarray(cpp_vector, dtype=np.float) return arr but this works very slow (assume copying occurs) on large vectors. 回答1: Casting the vector to

numpy genfromtxt - missing data vs bad data

跟風遠走 提交于 2021-01-07 06:31:10
问题 I'm using numpy genfromtxt, and I need to identify both missing data and bad data. Depending on user input, I may want to drop bad value or raise error. Essentially, I want to treat missing and bad data as the same thing. Say I have a file like this, where the columns are of data types "date, int, float" date,id,value 2017-12-4,0, # BAD. missing data 2017-12-4,1,XYZ # BAD. value should be float, not string. 2017-12-4,2,1.0 # good 2017-12-4,3,1.0 # good 2017-12-4,4,1.0 # good I would like to

np.dot of two 2D arrays

↘锁芯ラ 提交于 2020-12-12 05:43:55
问题 I am new to using numpy so sorry if this sounds obvious, I did try to search through stackoverflow before I post this though.. I have two "list of lists" numpy arrays of length n (n = 3 in the example below) a = np.array([[1, 2], [3, 4], [5, 6]]) b = np.array([[2, 2], [3, 3], [4, 4]]) I want to get a 1d array with the dot product of the lists at each corresponding index, i.e. [(1*2 + 2*2), (3*3 + 4*3), (5*4 + 6*4)] [6, 21, 44] how should I go about doing it? thanks in advance! 回答1: You can do

np.dot of two 2D arrays

前提是你 提交于 2020-12-12 05:42:28
问题 I am new to using numpy so sorry if this sounds obvious, I did try to search through stackoverflow before I post this though.. I have two "list of lists" numpy arrays of length n (n = 3 in the example below) a = np.array([[1, 2], [3, 4], [5, 6]]) b = np.array([[2, 2], [3, 3], [4, 4]]) I want to get a 1d array with the dot product of the lists at each corresponding index, i.e. [(1*2 + 2*2), (3*3 + 4*3), (5*4 + 6*4)] [6, 21, 44] how should I go about doing it? thanks in advance! 回答1: You can do