NumPy Intro
NumPy Intro
in
NumPy:
NumPy is an open source library available in Python that used in
mathematical, statistical operations, scientific, engineering, and data
science programming.
a = [10,20,30,40]
print(a)
# np.array() is used to convert python list to a numpy array
b = np.array(a)
print(b)
a = np.array([10,20,30,40)
print(a)
b = np.array([10.5,20.2,30.6])
print(b.dtype)
a = np.array([1,2,3,4,5], ndmin = 2)
print(a)
print(a)
# 2 dim array
c = np.array([(1,2,3),
(4,5,6)])
print(c.shape)
# 3 dim array
d = np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])
print(d.shape)
a = np.array([[1,2,3],[4,5,6]])
print(a.shape)
a = np.array([[1,2,3],[4,5,6]])
a.shape = (3,2)
print(a)
a = np.array([(1,2,3),(4,5,6)])
print(a)
a.reshape(3,2)
a = (1,2,3)
b = np.asarray(a) # ndarray from tuple
print(b)
a = [(1,2,3),(4,5)]
b = np.asarray(a) # ndarray from list of tuples
print(b)
np.arange(10,20,2)
a = np.array([[1,2,3],[3,4,5],[4,5,6]])
a[1:] # slice items starting from index
a[...,1] # this returns array of items in the second column
a[1,...] # Now we will slice all items from the second row
x = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
x[1:4,1:3] # slicing
y = x[1:4,[1,2]] # using advanced index for column
a = np.array([1,2,3,4])
b = np.array([10,20,30,40])
print(a+b)
a = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
print(a+b) # b is broadcast to become compatible with a
a.T # Transpose
Statistical Functions
np.min(a)
np.max(a)
np.mean(a)
np.median(a)
np.std(a)
Arithmetic operations
np.add(a,b)
np.subtract(a,b)
np.multiply(a,b)
np.divide(a,b)
np.power(a,2)
print(np.random.randn(2,2))