numpy-ndarray

How to create or fill an numpy array with another array?

陌路散爱 提交于 2019-12-01 20:43:14
How to create an numpy array with shape [2, 2, 3] , where the elements at axis 2 is another array, for example [1, 2, 3] ? So I would like to do something like this invalid code: a = np.arange(1, 4) b = np.full((3, 3), a) Resulting in an array like: [[[ 1. 2. 3.] [ 1. 2. 3.]] [[ 1. 2. 3.] [ 1. 2. 3.]]] Could of course make the loop for filling like, but thought there may be a shortcut: for y in range(b.shape[0]): for x in range(b.shape[1]): b[y, x, :] = a There are multiple ways to achieve this. One is to use np.full in np.full((2,2,3), a) as pointed out by Divakar in the comments.

How do I consistently flatten a numpy array?

霸气de小男生 提交于 2019-12-01 11:03:52
from numpy import array, eye, matrix x = array([1, 0]) A = eye(2) print(A.dot(x)) prints [1. 0.] . On the other hand, B = matrix([[1, 0], [0, 1]]) print(B.dot(x)) prints [[1 0]] which is a 1-by-2 array. Furthermore, print(B.dot(x).flatten()) also prints [[1 0]] . This is rather annoying. Why does flatten fail here and how else can I get this into the 1-d shape? Stop using matrix . numpy.matrix.flatten returns a 1-row matrix, because that's as flat as matrix instances get. If for some reason you are dead set on using matrix , convert to ndarray with matrix.A before flattening: flat = whatever

How do I consistently flatten a numpy array?

泪湿孤枕 提交于 2019-12-01 08:33:30
问题 from numpy import array, eye, matrix x = array([1, 0]) A = eye(2) print(A.dot(x)) prints [1. 0.] . On the other hand, B = matrix([[1, 0], [0, 1]]) print(B.dot(x)) prints [[1 0]] which is a 1-by-2 array. Furthermore, print(B.dot(x).flatten()) also prints [[1 0]] . This is rather annoying. Why does flatten fail here and how else can I get this into the 1-d shape? 回答1: Stop using matrix . numpy.matrix.flatten returns a 1-row matrix, because that's as flat as matrix instances get. If for some

How to convert Numpy array to Panda DataFrame

萝らか妹 提交于 2019-12-01 05:27:25
问题 I have a Numpy array that looks like this: [400.31865662] [401.18514808] [404.84015554] [405.14682194] [405.67735105] [273.90969447] [274.0894528] When I try to convert it to a Panda Dataframe with the following code y = pd.DataFrame(data) print(y) I get the following output when printing it. Why do I get all those zéros? 0 0 400.318657 0 0 401.185148 0 0 404.840156 0 0 405.146822 0 0 405.677351 0 0 273.909694 0 0 274.089453 I would like to get a single column dataframe which looks like that:

Convert Dictionary to Numpy array

邮差的信 提交于 2019-12-01 04:55:30
问题 I am trying to convert the dictionary {0: {0: 173, 1: 342, 2: 666, 3: 506, 4: 94}, 1: {0: 13, 1: 2171, 2: 1915, 3: 3075, 4: 630}, 2: {0: 0, 1: 265, 2: 5036, 3: 508, 4: 11}, 3: {0: 0, 1: 3229, 2: 2388, 3: 3649, 4: 193}, 4: {0: 3, 1: 151, 2: 591, 3: 1629, 4: 410}} to numpy array array([[ 173, 342, 666, 506, 94], [ 13, 2171, 1915, 3075, 630], [ 0, 265, 5036, 508, 11], [ 0, 3229, 2388, 3649, 193], [ 3, 151, 591, 1629, 410]]) Any ideas how to do it efficiently? 回答1: A Python-level loop is

Confusion in size of a numpy array

时光总嘲笑我的痴心妄想 提交于 2019-11-29 17:15:06
Python numpy array 'size' confuses me a lot a = np.array([1,2,3]) a.size = (3, ) ------------------------ b = np.array([[2,1,3,5], [2,2,5,1], [3,6,99,5]]) b.size = (3,4) 'b' makes sense since it has 3 rows and 4 columns in each But how is 'a' size = (3, ) ? Shouldn't it be (1,3) since its 1 row and 3 columns? No, a numpy.ndarray with shape (1, 3) would look like: np.array([[1,2,3]]) Think about how the shape corresponds to indexing: arr[0, ...] #First row I still have three more options, namely: arr[0,0] arr[0,1] arr[0,2] Try doing that with a 1 dimensional array You should resist the urge to

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

随声附和 提交于 2019-11-29 16:19:56
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(dtype).view(dtype)) # This last bit is optional if you're okay with "C" being a structured array... C =

what does numpy ndarray shape do?

无人久伴 提交于 2019-11-28 19:49:54
问题 I have a simple question about the .shape function, which confused me a lot. a = np.array([1, 2, 3]) # Create a rank 1 array print(type(a)) # Prints "<class 'numpy.ndarray'>" print(a.shape) # Prints "(3,)" b = np.array([[1,2,3],[4,5,6]]) # Create a rank 2 array print(b.shape) # Prints "(2, 3)" What did the .shape exactly do? count how many rows, how many columns, then the a.shape suppose to be, (1,3), one row three columns, right? 回答1: yourarray.shape or np.shape() or np.ma.shape() returns

Transforming a row vector into a column vector in Numpy

做~自己de王妃 提交于 2019-11-28 12:30:10
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? 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 transpose operation will just make a copy and transpose it. We can simply use the reshape functionality of numpy:

Confusion in size of a numpy array

白昼怎懂夜的黑 提交于 2019-11-28 11:31:03
问题 Python numpy array 'size' confuses me a lot a = np.array([1,2,3]) a.size = (3, ) ------------------------ b = np.array([[2,1,3,5], [2,2,5,1], [3,6,99,5]]) b.size = (3,4) 'b' makes sense since it has 3 rows and 4 columns in each But how is 'a' size = (3, ) ? Shouldn't it be (1,3) since its 1 row and 3 columns? 回答1: No, a numpy.ndarray with shape (1, 3) would look like: np.array([[1,2,3]]) Think about how the shape corresponds to indexing: arr[0, ...] #First row I still have three more options,