numpy-ndarray

how to copy numpy array value into higher dimensions

安稳与你 提交于 2019-11-28 11:13:47
I have a (w,h) np array in 2d. I want to make a 3d dimension that has a value greater than 1 and copy its value over along the 3rd dimensions. I was hoping broadcast would do it but it can't. This is how i'm doing it arr = np.expand_dims(arr, axis=2) arr = np.concatenate((arr,arr,arr), axis=2) is there a a faster way to do so? You can push all dims forward, introducing a singleton dim/new axis as the last dim to create a 3D array and then repeat three times along that one with np.repeat , like so - arr3D = np.repeat(arr[...,None],3,axis=2) Here's another approach using np.tile - arr3D = np

Zero pad numpy array

十年热恋 提交于 2019-11-28 08:07:00
What's the more pythonic way to pad an array with zeros at the end? def pad(A, length): ... A = np.array([1,2,3,4,5]) pad(A, 8) # expected : [1,2,3,4,5,0,0,0] In my real use case, in fact I want to pad an array to the closest multiple of 1024. Ex: 1342 => 2048, 3000 => 3072 numpy.pad with constant mode does what you need, where we can pass a tuple as second argument to tell how many zeros to pad on each size, a (2, 3) for instance will pad 2 zeros on the left side and 3 zeros on the right side: Given A as: A = np.array([1,2,3,4,5]) np.pad(A, (2, 3), 'constant') # array([0, 0, 1, 2, 3, 4, 5, 0,

How to create a numpy array of all True or all False?

三世轮回 提交于 2019-11-28 03:13:14
In Python, how do I create a numpy array of arbitrary shape filled with all True or all False? numpy already allows the creation of arrays of all ones or all zeros very easily: e.g. numpy.ones((2, 2)) or numpy.zeros((2, 2)) Since True and False are represented in Python as 1 and 0 , respectively, we have only to specify this array should be boolean using the optional dtype parameter and we are done. numpy.ones((2, 2), dtype=bool) returns: array([[ True, True], [ True, True]], dtype=bool) UPDATE: 30 October 2013 Since numpy version 1.8 , we can use full to achieve the same result with syntax

What is the purpose of meshgrid in Python / NumPy?

被刻印的时光 ゝ 提交于 2019-11-28 02:31:22
Can someone explain to me what is the purpose of meshgrid function in Numpy? I know it creates some kind of grid of coordinates for plotting, but I can't really see the direct benefit of it. I am studying "Python Machine Learning" from Sebastian Raschka, and he is using it for plotting the decision borders. See input 11 here . I have also tried this code from official documentation, but, again, the output doesn't really make sense to me. x = np.arange(-5, 5, 1) y = np.arange(-5, 5, 1) xx, yy = np.meshgrid(x, y, sparse=True) z = np.sin(xx**2 + yy**2) / (xx**2 + yy**2) h = plt.contourf(x,y,z)

What is the difference between ndarray and array in numpy?

江枫思渺然 提交于 2019-11-27 16:58:32
What is the difference between ndarray and array in Numpy? And where can I find the implementations in the numpy source code? wim numpy.array is just a convenience function to create an ndarray ; it is not a class itself. You can also create an array using numpy.ndarray , but it is not the recommended way. From the docstring of numpy.ndarray : Arrays should be constructed using array , zeros or empty ... The parameters given here refer to a low-level method ( ndarray(...) ) for instantiating an array. Most of the meat of the implementation is in C code, here in multiarray , but you can start

Better way to shuffle two numpy arrays in unison

拈花ヽ惹草 提交于 2019-11-27 16:48:46
I have two numpy arrays of different shapes, but with the same length (leading dimension). I want to shuffle each of them, such that corresponding elements continue to correspond -- i.e. shuffle them in unison with respect to their leading indices. This code works, and illustrates my goals: def shuffle_in_unison(a, b): assert len(a) == len(b) shuffled_a = numpy.empty(a.shape, dtype=a.dtype) shuffled_b = numpy.empty(b.shape, dtype=b.dtype) permutation = numpy.random.permutation(len(a)) for old_index, new_index in enumerate(permutation): shuffled_a[new_index] = a[old_index] shuffled_b[new_index]

Why using an array as an index changes the shape of a multidimensional ndarray?

别说谁变了你拦得住时间么 提交于 2019-11-27 15:19:12
I have a 4-D NumPy array, with axis say x,y,z,t. I want to take slice corresponding to t=0 and to permute the order in the y axis. I have the following import numpy as np a = np.arange(120).reshape(4,5,3,2) b = a[:,[1,2,3,4,0],:,0] b.shape I get (5, 4, 3) instead of (4,5,3). When, instead, I enter aa = a[:,:,:,0] bb = aa[:,[1,2,3,4,0],:] bb.shape I get the expected (4,5,3). Can someone explain why does the first version swap the first two dimensions? As @hpaulj mentioned in the comments, this behaviour is because of mixing basic slicing and advanced indexing : a = np.arange(120).reshape(4,5,3

Deprecation status of the NumPy matrix class

安稳与你 提交于 2019-11-27 13:25:58
What is the status of the matrix class in NumPy? I keep being told that I should use the ndarray class instead. Is it worth/safe using the matrix class in new code I write? I don't understand why I should use ndarray s instead. tl; dr: the numpy.matrix class is getting deprecated. There are some high-profile libraries that depend on the class as a dependency (the largest one being scipy.sparse ) which hinders proper short-term deprecation of the class, but users are strongly encouraged to use the ndarray class (usually created using the numpy.array convenience function) instead. With the

How does the axis parameter from NumPy work?

会有一股神秘感。 提交于 2019-11-27 11:21:02
Can someone explain exactly what the axis parameter in NumPy does? I am terribly confused. I'm trying to use the function myArray.sum(axis=num) At first I thought if the array is itself 3 dimensions, axis=0 will return three elements, consisting of the sum of all nested items in that same position. If each dimension contained five dimensions, I expected axis=1 to return a result of five items, and so on. However this is not the case, and the documentation does not do a good job helping me out (they use a 3x3x3 array so it's hard to tell what's happening) Here's what I did: >>> e array([[[1, 0]

Transforming a row vector into a column vector in Numpy

徘徊边缘 提交于 2019-11-27 06:54:47
问题 Let's say I have a row vector of the shape (1, 256). I want to transform it into a column vector of the shape (256, 1) instead. How would you do it in Numpy? 回答1: you can use the transpose operation to do this: Example: In [2]: a = np.array([[1,2], [3,4], [5,6]]) In [5]: np.shape(a) Out[5]: (3, 2) In [6]: a_trans = a.transpose() In [8]: np.shape(a_trans) Out[8]: (2, 3) In [7]: a_trans Out[7]: array([[1, 3, 5], [2, 4, 6]]) Note that the original array a will still remain unmodified. The