I have an array A of shape (30,) where each row has a list with 2000 elements. I want to convert this into a 2d array of shape (30, 2000). This is what I tried
A = np.reshape(A, (30, -1))
But, running this gives me an array of shape (30, 1) rather than (30, 2000). What should I do to get the correct shape?
where each row has a list with 2000 elements
As Ahmed Mohamed AEK points out in the comments this won't work as the numpy object is of shape (30,). One easy fix is to stack them into a 30 by 2000 np.array.
For example:
A = np.vstack(A)
or equvalently:
A = np.stack(A, axis=0)
Related
I'm Beginner in Python but i want convert an array by this shape (1100,1) that contain arrays with (100,50) shape, to an array by this shape (1100,100,50), How I Can This Conversion?
X.shape
>> (1100, 1)
X[0][0].shape
>> (100, 50)
This work and solved problem:
np.stack(X[:,0])
I'm newbie with Python and also with Numpy.
I have this code:
one_array.shape
When I run it, I get this output:
(20, 48, 240, 240)
one_array is a Numpy Array that has 20 images.
What do mean the other three numbers in shape output (48, 240, 240)?
Your array consist of 20 images, each of them is the size 48X240X240. Which is odd, I would expect that it will be something like 240X240X3 but for some reason you have way more channels (referring to RGB). ]
So the shape function return the size of dimension along each axis (the current shape of the entire array), so in your case there is (20, 48, 240, 240)
Edit:
As the user said, each image consist of 48 NITFY images of 1 channel which explain the output of shape
Imagine your Numpy Array as a Vector that can be in one dimension, but in your case it looks like it is in dimension 4.
(20, 4, 240, 240) means a big matrix composed of 20 x 4 x 240 x 240 elements.
one_array.shape == (20, 48, 240, 240) means that one_array is a 4-dimensional array with 20*48*240*240 or 55296000 elements.
You are right, you can think of one_array as an array with 20 elements, in which is element in another array with shape (48, 240, 240). However, usually is it better to think that one_array is a 4 dimensional array, that has a total of 20x48x240x240 = 55296000 elements.
I want to multiply two numpy arrays. One numpy array is given by matrix of shape (10, 10) and the other is given by a matrix of matrices, i.e. shape (10, 10, 256, 256).
I now simply want to multiply each matrix in the second matrix of matrices with the corresponding component in the first matrix. For instance, the matrix at position (0, 0) in the second matrix shall be multiplied by the value at position (0, 0) in the first matrix.
Intuitively, this is not really complicated, but numpy does not seem to support that. Or at least I am not smart enough to make it work. The ValueError that is thrown says:
ValueError: operands could not be broadcast together with shapes (10,10) (10,10,256,256)
Can anybody of you help me please? How can I achieve what I want in a numpyy way.
You can use the NumPy einsum function, e.g., (using zeros arrays as dummies in this example):
import numpy as np
x = np.zeros((10, 10))
y = np.zeros((10, 10, 256, 256))
z = np.einsum("ij,ijkm->km", x, y)
print(z.shape)
(256, 256)
See here for a nice description of einsum's usage.
I have a numpy array U with shape (20, 50): 20 spatial points, in a space of 50 dimensions.
How can I transform it into a (20, 1, 50) array, i.e. 20 rows, 1 column, and each element is a 50 dimension point? Kind of encapsulating each row as a numpy array.
Context
The point is that I want to expand the array along the columns (actually, replicating the same array along the columns X times) using numpy.concatenate. But if I would do it straight away I would not get the result I want.
E.g., if I would expand it once along the columns, I would get an array with shape (20, 100). But what I would like is to access each element as a 50-dimensional point, so when I expand it I would expect to have a new U' with shape (20, 2, 50).
You can do U[:, None, :] to add a new dimension to the array.
You can also use reshape:
import numpy as np
a = np.zeros((20, 50))
print a.shape # (20, 50)
b = a.reshape((20, 1, 50))
print b.shape # (20, 1, 50)
I have a 2D array in Python either a normal one or a numpy array with dimensions (150, 5), I wish to split it into two arrays of dimensions (150, 3) and (150, 2) respectively. Somehow I haven't been able to do it.
Any suggestions?
for numpy arrays you can slice them like this:
a, b = the_array[...,:3], the_array[...,3:]
and with lists of lists (that's what I understand for "normal arrays")
a, b = [i[:3] for i in the_array], [i[3:] for i in the_array]