NumPy - ndarrays

⚡️ndarrays

One of the key features of NumPy is its N-dimensional array object, or ndarray,
which is a fast, flexible container for large datasets in Python. Arrays enable you to
perform mathematical operations on whole blocks of data using similar syntax to the
equivalent operations between scalar elements.

⚡️Creating arrays

First of all import numpy

import numpy as np

a = np.array([1,2,3,4,5])
b = np.array([[1.0,2.0,3.0],[4.0,5.0,6.0]])

⚡️Getting Dimension of array

print(a.ndim) # 1
print(b.ndim) # 2

⚡️Getting Shape of an array

print(array.shape) # (rows ,column) in case of 2d arrays

print(a.shape) # (3, )
print(a.shape) # (2,3)

⚡️Get type of an array

print(a.dtype) # int32
print(b.dtype) # float64
  • Most of the time dtype is float64 by default
  • I will talk more about dtypes and changing dtypes in next post

⚡️Get size of an array

# Gets size of a single element in the array (dependent on dtype)
print(a.itemsize) # 4 (because int32 has 32 bits or 4 bytes)

# Gets size of array
print(a.size) # 5

# So total size can be written as 
print(a.itemsize * a.size) # 20

# or we can directly use a method called nbytes
print(a.nbytes) # 20

⚡️Methods of initializing an array

📌 assarray

a_list = [1, 2, 3]
a_array = np.asarray(a_list)

print(a_array)        # [1 2 3]
print(type(a_array))  # <class 'numpy.ndarray'>

📌 arange

a = np.arange(5)

print(a) # [0,1,2,3,4]

📌 ones

# Syntax np.ones(shape,dtype)

a = np.ones((3, 3), dtype='int32')

print(a)
"""

[[1 1 1]
 [1 1 1]
 [1 1 1]]

"""

📌 ones_like

# Syntax np.ones_like(numpy_array,dtype)

arr = np.array([[1, 2], [3, 4]])
a = np.ones_like(arr, dtype="int32")

print(a)
"""

[[1 1]
 [1 1]]

"""

📌 zeros

# Syntax np.zeros(shape,dtype)

a = np.zeros((3, 3), dtype='int32')

print(a)
"""

[[0 0 0]
 [0 0 0]
 [0 0 0]]

"""

📌 zeros_like

# Syntax np.zeros_like(numpy_array,dtype)

arr = np.array([[1, 2], [3, 4]])
a = np.zeros_like(arr, dtype="int32")

print(a)
"""

[[0 0]
 [0 0]]

"""

📌 empty

# Syntax np.empty(shape,dtype)
a = np.empty((3, 2))
print(a) # Does not Initialize but fills in with arbitrary values
"""

[[7.14497594e+159 1.07907047e+219]
 [1.17119997e+171 5.02065932e+276]
 [1.48505869e-076 1.93167737e-314]]

"""

📌 empty_like

# Syntax np.empty_like(numpy_array,dtype)

arr = np.array([[1, 2], [3, 4]])
a = np.empty_like(arr, dtype="int32")

print(a)
"""

[[-1290627329  -717194661]
 [ 1707377199  1980049554]]

"""

📌 full

# Syntax np.full(shape,fill_value,dtype)

a = np.full((5, 5), 99)
print(a)
"""

[[99 99 99 99 99]
 [99 99 99 99 99]
 [99 99 99 99 99]
 [99 99 99 99 99]
 [99 99 99 99 99]]

"""

📌 full_like

# Syntax np.full_like(shape,fill_value,dtype)

arr = np.empty((4, 2))
a = np.full_like(arr, 25)

print(a)
"""

[[25. 25.]
 [25. 25.]
 [25. 25.]
 [25. 25.]]

"""

📌 identity

  • Create a square N × N identity matrix (1s on the diagonal and 0s elsewhere)
# Syntax np.identity(n,dtype) (or)
#        np.eye(n,dtype)

a = np.idetity(6)
print(a)

"""

[[1. 0. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0. 0.]
 [0. 0. 1. 0. 0. 0.]
 [0. 0. 0. 1. 0. 0.]
 [0. 0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 0. 1.]]

"""

⚡️Join my Discord for Updates and downloads for future materials

16