numpy-ndarray

finding nearest neighbor for python numpy.ndarray in 3d-space

帅比萌擦擦* 提交于 2020-01-30 07:00:50
问题 I have a numpy.ndarray of 3d-points, i.e. the np.shape of it is (4350,3) and such a second numpy.ndarray of 3d-points of np.shape (10510,3). Now I am trying to find the right python-package to calculate the nearest neighbors in the second array of the points in the first array as quickly as possible. I've found a quite similar question here: find the k nearest neighbours of a point in 3d space with python numpy but I don't understand how to use the solution there for my problem. I'd very,

np.dot product between two 3D matrices along specified axis

强颜欢笑 提交于 2020-01-25 01:04:20
问题 I have two 3D matrices: a = np.random.normal(size=[3,2,5]) b = np.random.normal(size=[5,2,3]) I want the dot product of each slice along 2 and 0 axes respectively: c = np.zeros([3,3,5]) # c.size is 45 c[:,:,0] = a[:,:,0].dot(b[0,:,:]) c[:,:,1] = a[:,:,1].dot(b[1,:,:]) ... I would like to do that using np.tensordot (for efficiency and speed) I have tried: c = np.tensordot(a, b, axes=[2,0]) but I get a 4D array with 36 elements (instead of 45). c.shape, c.size = ((3L, 2L, 2L, 3L), 36). I have

How to extract an array of same dimension as the original array meeting a condition? [duplicate]

本秂侑毒 提交于 2020-01-23 16:56:46
问题 This question already has answers here : Numpy array loss of dimension when masking (4 answers) Closed 9 months ago . The question sounds very basic. But when I try to use where or boolean conditions on numpy arrays, it always returns a flattened array. I have the NumPy array P = array([[ 0.49530662, 0.07901 , -0.19012371], [ 0.1421513 , 0.48607405, -0.20315014], [ 0.76467375, 0.16479826, -0.56598029], [ 0.53530718, -0.21166188, -0.08773241]]) I want to extract the array of only negative

Expanding numpy array while updating the values

你离开我真会死。 提交于 2020-01-15 08:53:06
问题 I have an numpy array with shape (1, m) and each entry ( n ) is an integer ranging 0-9. I want to create a new matrix that has shape (m, 10) where all the entries are 0, except it is 1 for the nth column. For example: [2, 3, 1] -> [[0, 0, 1, 0, ...], [0, 0, 0, 1, ...], [0, 1, 0, 0, ...]] The code I wrote for it that works is: y_values = np.array([[2, 3, 6, 4, 7]]) y = np.zeros((10, y_values.shape[1])) for i in range(y_values.shape[1]): y[y_values[0][i]][i] = 1 Is there a way I can get rid of

Slice and change numpy 2D array with list of column indices different for each row [duplicate]

江枫思渺然 提交于 2020-01-11 14:40:54
问题 This question already has answers here : Indexing one array by another in numpy (3 answers) Closed yesterday . I would like to slice a 2D numpy array using a list of column indices. The difficulty is the column indices are different for each row. For example: x = np.array([[0, 1, 2] [3, 4, 5] [6, 7, 8]]) and I have a list of column indices as indices = [[0, 1], [2, 1], [2, 2]] which means I would like to get column [0,1] for row 0, column [2, 1] for row 1, and column [2, 2] for row 2. The

Check if two 3D numpy arrays contain overlapping 2D arrays

纵饮孤独 提交于 2020-01-09 05:31:06
问题 I have two very large numpy arrays, which are both 3D. I need to find an efficient way to check if they are overlapping, because turning them both into sets first takes too long. I tried to use another solution I found here for this same problem but for 2D arrays, but I didn't manage to make it work for 3D. Here is the solution for 2D: nrows, ncols = A.shape dtype={'names':['f{}'.format(i) for i in range(ndep)], 'formats':ndep * [A.dtype]} C = np.intersect1d(A.view(dtype).view(dtype), B.view

Perform numpy exp function in-place

荒凉一梦 提交于 2020-01-02 05:36:09
问题 As in title, I need to perform numpy.exp on a very large ndarray, let's say ar , and store the result in ar itself. Can this operation be performed in-place? 回答1: You can use the optional out argument of exp : a = np.array([3.4, 5]) res = np.exp(a, a) print(res is a) print(a) Output: True [ 29.96410005 148.4131591 ] exp(x[, out]) Calculate the exponential of all elements in the input array. Returns out : ndarray Output array, element-wise exponential of x . Here all elements of a will be

Questions regarding the dimension initialization of multiple numpy arrays within a single numpy array

夙愿已清 提交于 2019-12-24 20:46:47
问题 Given that we have 3 Pauli matrices, each with dimension (2x2). As shown below: X = np.array([[0, 1], [1, 0]], dtype=complex) Y = np.array([[0, -1j], [1j, 0]], dtype=complex) Z = np.array([[1, 0], [0, -1]], dtype=complex) Now if I put these each individual (2x2) matrices as entries to another (2x2) matrices. Say: A = np.array([[X, 0], [0, Y]]) B = np.array([[X, X], [Y, Y]]) Weirdly, A has a dim of (2x2) - which is ideally what I want - and B has a dim of (2, 2, 2, 2)whatever this is, as show

How to index several 2d numpy arrays with different number of rows in a 3d array?

大兔子大兔子 提交于 2019-12-24 18:25:17
问题 I have the following problem in python: I have several numpy 2d-arrays where all have same number of columns ,but different number of rows.I want to index all these 2d-array in a unique numpy 3d-array where the first index keeps into account each 2d-array. For example : let's suppose I got two 2d-arrays like this : [[1,2,3][4,5,6][7,8,9]] (3X3 array) [[11,12,13][14,15,16]] (2X3 array) I want to get a numpy 3d-array name,for example, c where : c[0] has shape (3,3), c[1] (2,3) and so on...So I

Numpy : Grouping/ binning values based on associations

↘锁芯ラ 提交于 2019-12-24 12:17:29
问题 Forgive me for a vague title. I honestly don't know which title will suit this question. If you have a better title, let's change it so that it will be apt for the problem at hand. The problem. Let's say result is a 2D array and values is a 1D array. values holds some values associated with each element in result . The mapping of an element in values to result is stored in x_mapping and y_mapping . A position in result can be associated with different values. Now, I have to find the sum of