25
loading...
This website collects cookies to deliver better user experience
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]])
print(a.ndim) # 1
print(b.ndim) # 2
print(array.shape) # (rows ,column) in case of 2d arrays
print(a.shape) # (3, )
print(a.shape) # (2,3)
print(a.dtype) # int32
print(b.dtype) # float64
float64
by default# 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
a_list = [1, 2, 3]
a_array = np.asarray(a_list)
print(a_array) # [1 2 3]
print(type(a_array)) # <class 'numpy.ndarray'>
a = np.arange(5)
print(a) # [0,1,2,3,4]
# Syntax np.ones(shape,dtype)
a = np.ones((3, 3), dtype='int32')
print(a)
"""
[[1 1 1]
[1 1 1]
[1 1 1]]
"""
# 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]]
"""
# Syntax np.zeros(shape,dtype)
a = np.zeros((3, 3), dtype='int32')
print(a)
"""
[[0 0 0]
[0 0 0]
[0 0 0]]
"""
# 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]]
"""
# 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]]
"""
# 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]]
"""
# 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]]
"""
# 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.]]
"""
# 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.]]
"""