Numpy
Numpy :
1. Array doesn't support multidimensional then where we can use third party package : numpy
2. NumPy stand for numerical python
3. Used for performing mathematical and logical operations on arrays
4. Provides features for operations on multidimensional arrays and matrix in python
5. In tuple, list can't directly handle maths operations that's why we use array
import numpy as np
#1d array
a = np. array([1,2,3])
a[0]= 0
print(a[:2]) # slicing
print(a[1]) # indexing
print(type(a)) # type
print(a[0:])
print(a.size) #1. size
print(a.dtype) #2. data type
print(a.ndim) #3.ndim no of dim
print(a)
d= a.reshape(3,1) #4 reshpe new variable
print(d) d value is 0,2,3
#2d array
x = np.array([[4,5,6],[7,8,9]])
print(x[0])
print(x[0:])
print(x[:2])
print(x.size) # size
print(x.dtype) # data type
print(x.ndim) # no of dimensio 2d
#5. return the shape
print(x.shape)
#change the shape
x.shape = (3,2)
print(x.shape[0])
print(x)
Different ways to create numpy araay :
# 1.linspace(start,stop,part)
linspace is an in-built function in Python's NumPy library. It is used to create an evenly spaced sequence in a specified interval.
b = np.linspace(3,10,5) # 5 part output 5
print(b)
#2. arange(start,stop,step)
arrange() It creates an array by using the evenly spaced values over the given interval.
c = np.arange(3,10,5)
print(c)
#3. logspace(start,stop,e)
It creates an array by using the numbers that are evenly separated on a log scale
d = np.logspace(3,10,5)
print(d)
#4 zeros(r,c)
The numpy. zeros() function provide a new array of given shape and type, which is filled with zeros.
e = np.zeros((3,4))
print(e)
#5. ones(r,c) rows and colum
The numpy.ones() function returns a new array of given shape and type, with ones.
f = np.ones((3,4))
print(f)
#6 full(r,c) rows and colum
Return a new array with the same shape and type as a given array filled with a fill_value.
g = np.full((3,4),5)
print(g)
#7. random rand, randn, randint
Python Random module is an in-built module of Python which is used to generate random numbers.
h = np.random.randn(3,4)
print(h)
# Numpy array mathematics
#mathematics calculations on 2 variable
import numpy as np
a, b = 40,20
print(np.sum([8,9])) # directly add value
print(np.sum([a, b])) *** sum([ ])
print(np.subtract(a, b))
print(np.multiply(a,b))
print(np.divide(a,b))
print(np.sqrt(a))
print(np.exp(a))
print(np.log(a))
print(np.sin(a))
print(np.cos(a))
#mathematics calculations on 2 Array
a = (np. array([10,20,30]))
b = (np. array([40,50,60]))
print(sum([a, b]))
print(np.subtract(a,b))
print (np.multiply(a,b))
print(np.divide(a,b))
print(np.sqrt(a))
print(np.log(a))
print(np.exp(a))
print(np.sin(a))
print(np.cos(a))
# aggregate function
a = [1,2,3]
b = [4,5,6]
print(np.sum([a, b]))
print(np.min(a)) # lowest value
print(np.mean(a)) #add and then divide
print(np.median(a)) # calculate h.m no
print(np.corrcoef(a))
print(np.std(a))
# array comparison
1. Element wise comparison
x = [1,2,3]
y = [4,5,3]
z = [1,2,3]
print(np.equal(x,y))
2. Array wise comparison
print(np.array_equal(x,z))
Array manipulations
import numpy as np
a = ([[1,2,3],[0,7,8]])
b = ([[4,5,6],[7,8,9]])
print(np.concatenate((a,b)))
#stack
print("\n")
print(np.vstack((a, b)))
print("\n")
print(np.hstack((a, b)))
print("\n")
print(np.stack((a, b)))
print("\n")
#split
x = np.arange(1,7)
print(x)
print(np.split(x,3))
print("\n")
print(np.split(x,6,0))
Comments
Post a Comment
If you have any doubts, please let me know