import numpy as np
ar=np.array([1,5,2,8,3])
print(ar) # [1 5 2 8 3]
ar=np.array([[1,5,2,8,3],[8,5,3,11,1]])
print(ar)
Output
[[ 1 5 2 8 3]
[ 8 5 3 11 1]]
We can use shape to get the rows and columns of the array. In above code we can check size of the arrays.
print(ar.shape) # (3, 4)
There are 3 rows and 4 columns in above array. You can try shape for other arrays.
Read more on eye() here. ar=np.eye(3,4)
print(ar)
Output
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]]
We can create array of any shape and fill it by 1s by using ones().
ar=np.ones((4,3))
print(ar)
Output
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
print(np.arange(1,5,dtype=np.int8)) # [1 2 3 4]
print(np.linspace(2,10,5)) # [ 2. 4. 6. 8. 10.]
my_data=np.random.rand(4)
print(my_data)
Output
[0.3630618 0.76575666 0.36043609 0.69937788]
ar=np.zeros(4,dtype=int)
print(ar) # [0 0 0 0]
ar=np.full((1,3),5)
print(ar) # [[5 5 5]]
ar=np.empty((1,3))
print(ar)
Output
[[5.e-324 5.e-324 5.e-324]]
ar = np.array([1, 2, 3, 4,5,6], ndmin=2)
print(ar)
print('Shape :', ar.shape)
print('Dimension :',ar.ndim)
Output
[[1 2 3 4 5 6]]
Shape : (1, 6)
Dimension : 2
shape() : Shape and dimension of the input array
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.