numpy-ndarray

What is the difference between flatten and ravel functions in numpy?

懵懂的女人 提交于 2019-11-26 11:59:59
import numpy as np y = np.array(((1,2,3),(4,5,6),(7,8,9))) OUTPUT: print(y.flatten()) [1 2 3 4 5 6 7 8 9] print(y.ravel()) [1 2 3 4 5 6 7 8 9] Both function return the same list. Then what is the need of two different functions performing same job. IanH The current API is that: flatten always returns a copy. ravel returns a view of the original array whenever possible. This isn't visible in the printed output, but if you modify the array returned by ravel, it may modify the entries in the original array. If you modify the entries in an array returned from flatten this will never happen. ravel

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

萝らか妹 提交于 2019-11-26 11:33:54
问题 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? 回答1: As @hpaulj mentioned in the comments,

Convert array of indices to 1-hot encoded numpy array

≯℡__Kan透↙ 提交于 2019-11-26 11:04:48
Let's say I have a 1d numpy array a = array([1,0,3]) I would like to encode this as a 2d 1-hot array b = array([[0,1,0,0], [1,0,0,0], [0,0,0,1]]) Is there a quick way to do this? Quicker than just looping over a to set elements of b , that is. Your array a defines the columns of the nonzero elements in the output array. You need to also define the rows and then use fancy indexing: >>> a = np.array([1, 0, 3]) >>> b = np.zeros((3, 4)) >>> b[np.arange(3), a] = 1 >>> b array([[ 0., 1., 0., 0.], [ 1., 0., 0., 0.], [ 0., 0., 0., 1.]]) >>> values = [1, 0, 3] >>> n_values = np.max(values) + 1 >>> np

Concatenating two one-dimensional NumPy arrays

落爺英雄遲暮 提交于 2019-11-26 04:17:18
问题 I have two simple one-dimensional arrays in NumPy. I should be able to concatenate them using numpy.concatenate. But I get this error for the code below: TypeError: only length-1 arrays can be converted to Python scalars Code import numpy a = numpy.array([1, 2, 3]) b = numpy.array([5, 6]) numpy.concatenate(a, b) Why? 回答1: The line should be: numpy.concatenate([a,b]) The arrays you want to concatenate need to passed in as a sequence, not as separate arguments. From the NumPy documentation:

How does numpy.newaxis work and when to use it?

99封情书 提交于 2019-11-26 02:59:43
问题 When I try numpy.newaxis the result gives me a 2-d plot frame with x-axis from 0 to 1. However, when I try using numpy.newaxis to slice a vector, vector[0:4,] [ 0.04965172 0.04979645 0.04994022 0.05008303] vector[:, np.newaxis][0:4,] [[ 0.04965172] [ 0.04979645] [ 0.04994022] [ 0.05008303]] Is it the same thing except that it changes a row vector to a column vector? Generally, what is the use of numpy.newaxis, and in which circumstances should we use it? 回答1: Simply put, the newaxis is used

What is the difference between flatten and ravel functions in numpy?

自古美人都是妖i 提交于 2019-11-26 02:26:59
问题 import numpy as np y = np.array(((1,2,3),(4,5,6),(7,8,9))) OUTPUT: print(y.flatten()) [1 2 3 4 5 6 7 8 9] print(y.ravel()) [1 2 3 4 5 6 7 8 9] Both function return the same list. Then what is the need of two different functions performing same job. 回答1: The current API is that: flatten always returns a copy. ravel returns a view of the original array whenever possible. This isn't visible in the printed output, but if you modify the array returned by ravel, it may modify the entries in the

Convert array of indices to 1-hot encoded numpy array

让人想犯罪 __ 提交于 2019-11-26 01:56:46
问题 Let\'s say I have a 1d numpy array a = array([1,0,3]) I would like to encode this as a 2d 1-hot array b = array([[0,1,0,0], [1,0,0,0], [0,0,0,1]]) Is there a quick way to do this? Quicker than just looping over a to set elements of b , that is. 回答1: Your array a defines the columns of the nonzero elements in the output array. You need to also define the rows and then use fancy indexing: >>> a = np.array([1, 0, 3]) >>> b = np.zeros((3, 4)) >>> b[np.arange(3), a] = 1 >>> b array([[ 0., 1., 0.,

How do I get indices of N maximum values in a NumPy array?

核能气质少年 提交于 2019-11-26 01:23:31
问题 NumPy proposes a way to get the index of the maximum value of an array via np.argmax . I would like a similar thing, but returning the indexes of the N maximum values. For instance, if I have an array, [1, 3, 2, 4, 5] , function(array, n=3) would return the indices [4, 3, 1] which correspond to the elements [5, 4, 3] . 回答1: The simplest I've been able to come up with is: In [1]: import numpy as np In [2]: arr = np.array([1, 3, 2, 4, 5]) In [3]: arr.argsort()[-3:][::-1] Out[3]: array([4, 3, 1]

How to convert a PIL Image into a numpy array?

被刻印的时光 ゝ 提交于 2019-11-26 00:46:11
问题 Alright, I\'m toying around with converting a PIL image object back and forth to a numpy array so I can do some faster pixel by pixel transformations than PIL\'s PixelAccess object would allow. I\'ve figured out how to place the pixel information in a useful 3D numpy array by way of: pic = Image.open(\"foo.jpg\") pix = numpy.array(pic.getdata()).reshape(pic.size[0], pic.size[1], 3) But I can\'t seem to figure out how to load it back into the PIL object after I\'ve done all my awesome

What are the advantages of NumPy over regular Python lists?

旧时模样 提交于 2019-11-25 22:57:17
问题 What are the advantages of NumPy over regular Python lists? I have approximately 100 financial markets series, and I am going to create a cube array of 100x100x100 = 1 million cells. I will be regressing (3-variable) each x with each y and z, to fill the array with standard errors. I have heard that for \"large matrices\" I should use NumPy as opposed to Python lists, for performance and scalability reasons. Thing is, I know Python lists and they seem to work for me. What will the benefits be