This question already has answers here:
How to change array shapes in in numpy?
(5 answers)
Closed 8 months ago.
I have an array B with shape (1,9,1). Is it possible to convert into a new array B1 with shape (1,3,3)? The desired output is attached.
import numpy as np
B=np.array([[[0.67873113],
[1.24442563],
[0.02109 ],
[0.76788408],
[2.00615422],
[3.07375839],
[1.037729 ],
[5.03294753],
[0.0105 ]]])
print("B shape =",B.shape)
The desired output is
B1=np.array([[[0.67873113,1.24442563,0.02109 ],
[0.76788408,2.00615422,3.07375839],
[1.037729,5.03294753,0.0105 ]]])
B1 shape = (1, 3, 3)
You can just reshape it. See here
B.reshape((1,3,3))
>>> array([[[0.67873113, 1.24442563, 0.02109 ],
[0.76788408, 2.00615422, 3.07375839],
[1.037729 , 5.03294753, 0.0105 ]]])
Related
This question already has answers here:
How do I use np.newaxis?
(4 answers)
Numpy: use reshape or newaxis to add dimensions
(2 answers)
Closed 4 months ago.
consider the following code :
test_1 = np.zeros((2, 2))
test_2 = np.zeros((2, 2, 1))
print(test_1[..., np.newaxis])
print(test_1[np.newaxis])
Result :
[[[0.]
[0.]]
[[0.]
[0.]]]
[[[0. 0.]
[0. 0.]]]
I really don't understand the meaning of this '...' and why the two lines don't produce a similar array. Do you have an idea ?
Thanks a lot !
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:
How do I add an extra column to a NumPy array?
(17 answers)
Closed 2 years ago.
lets say I have a numpy array
Y =
array([[4.96473614, 6.02336215],
[2.56213959, 2.74621531],
[4.36170066, 5.60413956],
...,
[5.93540184, 3.62864816],
[2.34483661, 2.37333593],
[6.3250522 , 7.09102362]])
with
Y.shape
(1000,2)
(for example 2dim datapoints with x and y)
How can I easily add a third column to this given array (a z-value) such that
Y.shape
(1000,3)
?
Assume that you have:
Y array of shape (3,2) (3 rows instead of 1000):
array([[4.96473614, 6.02336215],
[2.56213959, 2.74621531],
[4.36170066, 5.60413956]])
Y2 array (1-D) of shape (3,):
array([10, 20, 30])
To get your result, you should:
first convert Y2 to (3,1) shape (3 rows, 1 column),
hstack them.
The code to do it is:
Y = np.hstack([Y, Y2[:, np.newaxis]])
The result is:
array([[ 4.96473614, 6.02336215, 10. ],
[ 2.56213959, 2.74621531, 20. ],
[ 4.36170066, 5.60413956, 30. ]])
I hope this helps:
Create the desired column in a (1000,1) array.
Call ArrayName.append(DesiredColumn, axis = 1)
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:
using an numpy array as indices of the 2nd dim of another array? [duplicate]
(2 answers)
Closed 5 years ago.
Given a numpy matrix a of shape (5,3), and a index vector b of shape (5,), where each entry in the index vector is between 0 to 2, how can I create a new vector c based on a and its index vector b.
Use arange for the other dimension:
c = a[np.arange(5), b]