This question already has answers here:
Unexpected behaviour numpy array indexing [duplicate]
(1 answer)
Why using an array as an index changes the shape of a multidimensional ndarray?
(1 answer)
Closed 2 years ago.
Consider the following code
import numpy as np
z = np.zeros((3,5,10,100))
indices = np.array([8, 0, 6, 1])
print(z[:,:,indices,:].shape)
print(z[1,:,indices,:].shape)
The output is as follows:
(3, 5, 4, 100)
(4, 5, 100)
I want to assign z[1,:,indices,:] = some_array where some_array has shape (5,4,100) but that assignment throws an error as there is shape mismatch.
I am confused about the second output (shape of z[1,:,indices,:]). I thought it should be (5,4,100). Why are the first 2 axes getting switched? Is this a bug or is there any explanation why this is the correct behavior?
I got my code to go the desired thing by changing it to:
for i,index in enumerate(indices):
z[1,:,index,:] = some_array[i]
Related
This question already has an answer here:
Very Basic Numpy array dimension visualization
(1 answer)
Closed 1 year ago.
I've a 3 dimensional numpy array. When I try to print it's shape, I get (4, 1, 2). Now I'm struggling to figure out which value corresponds to row, column and depth.
Code:
import numpy as np
zone = np.array([[[221,529]],
[[156,850]],
[[374,858]],
[[452,537]]])
print(zone.shape)
.shape returns dimensions respectively. So it means you have 4 rows, 1 column, and depth of 2.
This question already has answers here:
How do I add an extra column to a NumPy array?
(17 answers)
Closed 2 years ago.
I met with a problem when doing appending in NumPy. array_1 will throw an error: ValueError: could not broadcast input array from shape (4) into shape (3) but the bottom one does not, where am I doing it wrong? I need to write a loop to append arrays to each array in array_1.
The blunt way is to convert my 2d-arrays to a 2d-list, but as a keen learner, I am really curious about how to do it properly.
array_1 = np.array([[1,2,3], [4,5,6]])
array_1[0] = np.append(array_1[0], 1)
array_2 = np.array([[1,2,3]])
array_2 = np.append(array_2, 1)
Change it to this:
array_1 = np.array([[1,2,3], [4,5,6]])
array_1 = [np.append(array_1[0], 1), array_1[1]]
This question already has answers here:
Reshape an array in NumPy
(3 answers)
Closed 3 years ago.
I am getting IndexError if I try doing it using a for loop.
Any good way to go about it?
Use the reshape() method on the array. (It's also avilable as a NumPy function, np.reshape().)
Try this:
import numpy as np
arr = np.random.random((10000, 10, 10))
arr.reshape((10000, 100, 1))
In general, you almost never need a loop with NumPy arrays.
This question already has answers here:
Concatenating two one-dimensional NumPy arrays
(6 answers)
Closed 4 years ago.
x.shape = (256,1)
y.shape = (256,1)
how should I command?, so that it returns,
output.shape = (256,2).
Simple question, but I am unable to solve. please help.
I'm using for this append option:
import numpy as np
matrix_1 = np.random.uniform(1, 10, (256, 1))
matrix_2 = np.random.uniform(1, 10, (256, 1))
print(matrix_1.shape)
print(matrix_2.shape)
print('-------------')
out_matrix = np.append(matrix_1, matrix_2, axis=1)
print(out_matrix.shape)
This question already has answers here:
Numpy reshape 1d to 2d array with 1 column
(7 answers)
Closed 4 years ago.
I can't turn a numpy array into a vector. What I want to do is transform this:
>> x.shape
(784,)
into this:
>> x.shape
(784,1)
x was created in the normal numpy.array() way.
Note: I want to change, not create a new array.
You can add a new axis to a vector in multiple ways: e.g. using np.reshape
x = np.zeros(784)
x0 = x.reshape(784, 1) # shape: (784, 1)
or using np.newaxis while slicing:
x = np.zeros(784)
x0 = x[:,None] # short-hand for x[:,np.newaxis], shape: (784, 1)
Vectors, arrays and lists are totally different data structures, especially in Python. In your question you mentioned I can't turn a numpy array into a vector. Actually what you are trying to do is exactly the opposite, means you want to turn a vector of shape (784,) to an array of shape(784, 1). Anyways #cheersmate gave you the correct answer.