Meaning of array[..., np.newaxis) with numpy [duplicate] - python

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 !

Related

Changing shape of multidimensional array in Python [duplicate]

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 ]]])

Adding new feature (column) to given numpy array [duplicate]

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)

Numpy indexing and slicing not working as intended! (Bug?) [duplicate]

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]

python vector apply mean across axis in chunks of size 5 [duplicate]

This question already has answers here:
Average every x numbers in NumPy array
(4 answers)
Closed 3 years ago.
I have a long vector (50,000 datapoints)
and I want to reduce it by applying mean in chunks of 5. (So I will get vector at the size of 10,000).
For example,
if the vector is
[1,8,-1,0,2 , 6,8,11,4,6]
the output will be
[2,7]
What is the most efficient way to do so?
Thanks
When you know that the vector is always divisible by 5:
import numpy as np
vec = np.array([1,8,-1,0,2 , 6,8,11,4,6])
averaged = vec.reshape(-1, 5).mean(axis=1).flatten()
print(averaged)
Output
array([2., 7.])

Numpy shape is same, mean returns different shapes [duplicate]

This question already has answers here:
What are the differences between numpy arrays and matrices? Which one should I use?
(7 answers)
Closed 3 years ago.
I have the following snippet.
values = [[0.1, 0.7, 0.5], [0.6, 0.3, 0.2], [0.2, 0.8, 0.77]]
A = np.array(values).reshape(3,3)
print A.shape
print np.mean(A, axis=1)
B = np.mat(np.random.rand(3, 3));
print B.shape
print np.mean(B, axis=1)
Output of print statements:
(3, 3)
[ 0.43333333 0.36666667 0.59 ]
(3, 3)
[[ 0.47252016]
[ 0.44380355]
[ 0.51070646]]
I have two same shaped numpy array's with different values as an input, one is generated with the rand function the other is a python list created with the array function and calling reshape on it.
However, the shape of the mean return's a different shape for both, even though the input shapes are the same. Any Ideas what can cause this?
As explained in the documentation for numpy.matrix,
A matrix is a specialized 2-D array that retains its 2-D nature through operations
Additionally,
It is no longer recommended to use this class, even for linear algebra. Instead use regular arrays. The class may be removed in the future.

Categories