Numpy

 Lets understand the numpy:

By using the numpy our life become a very simple so that it is important to the learn the numpy 

First of all we have to install the numpy in our computer by using the pip install numpy,


import numpy as np
ndarray
#Dimensions in arrays is one level of array depth(nested arrays).
#nested arrarys that have arrays as their elements.
#0D arrays are the scalars present in the array
#arr=np.array(22)
#print(arr)
output=22
#1D arrays have a 0D arrays as elements is called unidimention or 1D array
# arr=np.array([1,2,3,4,5,5])
# print(arr)
output=[1,2,3,4,5,5]
# 2D arrays have 1D arrays as elements is called 2D arrays
#2D is used to represent matrix or any other
#matrix=np.array([[1,2,3],[4,5,6]])
#print(matrix)
output=[[1,2,3],[4,5,6]]
#3D is used to represent matrix in matrix
#matrix3=np.array([[[1,2,3],[2,1,3]],[[2,3,4],[5,6,2]]])
#print(matrix3)
#To find the dimention of the array there is built in function ndim
#print(matrix3.ndim)
output=3
#print(np.__version__)
#multidimention arrays is array in arrays
# arr=np.array([1,2,3,4,5],ndmin=3)
# print(arr)
output=[[[1,2,3,4,5]]]
# slicing of the array is done in the numpy
#print(arr[0,2])
output=3
#Data types in the numpy is more than the python library
#i-integer b-boolean u-unsigned integer f-float c-complex float
#m-timedelta M-datetime O-object S-string U-unicode string
#V-fixed chunk of memory for other type(void)
#Checking the data type in the array by dtype
#arr=np.array([1,2,3,4],'O')
# arr=np.array(["manja ","ganja","tabaki"])
# newarr=arr.astype("c")
# newaee=newarr.astype("U")
# print(newaee)
# print(arr.dtype)
# print(arr)
# arr=np.array([1,2,3,4])
# arrnew=arr.astype(bool)
# print(arrnew)
# print(arrnew.dtype)
#To copy the array write the newarr=arr.copy()
#BY using the copy it not changes the original array
#But by using the view it changes the the original array
#arr=np.array([1,2,3,4])
#newarr=arr.copy()
#newarr=arr.view()
#It changes the array totally by using the view
# arr[1]=9
# print(newarr)
# print(arr)
#Shape of an array is the number of elements in each dimension
#arr=np.array([[1,2,3],[4,5,6]])
#print(arr.shape)
#output be (2,3) it means dimension 2 and and each dimension contents 3el
# arr=np.array([[1,2,3],[4,5,6]],ndmin=5)
# print(arr.shape)
#Reshaping the arrays like 1D to 2D arrays
#arr=np.array([1,2,3,4,5,4,2,23,2,2])
#newarr=arr.reshape(2,5)
#reshape number of elements is 2*5
#print(newarr)
arr=np.array([1,2,3,4,5,4,2,23,2,2])
print(arr.reshape(2,5).base)
#Check if returned value is copy or view


import numpy as np
# arrs=np.array([1,2,3,4,5,6])
# for j in arrs:
# print(j,end=" ")
# print(arrs[1])
# print(arrs.size)
# zeros=np.zeros((2,3))
# print(zeros)
# rng=np.arange(19)
# print(rng)
# x=np.matrix(arrs)
# print(x.shape)
# aa=np.arange(10)
# print(aa)
# print(aa.reshape(2,5))
# print(arrs.revel())
arrs=np.matrix([[1,2,3],[4,5,6],[2,3,4]])
#for columns axis is 1 and for the rows it is 0
#when axis is 0 then there is sum of the each single columns
#when axis is 1 then there is sum of the each single row
print(arrs)
#print(arrs.sum(axis=0))
print(arrs.T)
It gives the transpose of the matrix
print(arrs.flat)
print(arrs.ndim)
it gives the dimenstion
print(arrs.size)
size gives the number of like (dimenstion,elements in the each dimenstion)
print(arrs.nbytes)
one=np.array([1,3,4,2,6])
# print(one.argmax()) it gives the max elements of the array in the form of the index
# print(one.argmin()) it gives the min elements of the array in the form of the index
# print(one.argsort()) sorting the array in the form of the index wise it is not showing the
all elements in the array but it shows only the index wise it is the more imporatant
arr=np.array([[1,2,3],[9,3,4],[1,23,3]])
print(arr.argmin(axis=0))
In the argmin(axis=0) we get the min number in the array in the form of the index of the columns
print(arr.argsort())
print(arr.ravel())
ar2=np.array([[1,2,3],[9,3,4],[1,23,3]])
print(arr*ar2)
print(np.sqrt(ar2))
print(ar2.max())
print(np.where(ar2>5))



Post a Comment

Previous Post Next Post