NumPy
What is NumPy?
❖Numerical python Package.
❖It is used for working with the scientific calculations ,Mathematical calculations
and general purpose of array processing.
❖ It provides a high performance multidimensional array object , and tools for
working with these arrays.
❖ Very fast than list.
❖ Elements in Numpy arrays are accessed by using square brackets and can be
initialized by using nested Python Lists.
Things to know in NumPy:-
❖ Difference between List and Numpy
❖Numpy alias name
❖nd array
❖Type of arrays
❖.ndim array
#Import numpy package
import numpy as np
a= np.array([12,78,456,90,10])
print(a)
arange()
import numpy as np
#print the values from 0 to 50 inside an array
a= np.arange(50)
print(a)
Updating array values
We can update the array values with the help of index.
a=[1,2,3,4,5]
a[1]=44
print(a)
reshape()
#To declare rows and columns of an array
import numpy as np
a=np.arange(15).reshape(5,3)
print(a)
#Accessing Array elements
a[1,1]
a[3,2]
Indexing
import numpy as np
# NumPy array with elements from 1 to 9
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
# Index values can be negative.
arr = x[ np. array([1, 3, -3])]
print(arr)
Boolean Indexing
This indexing has some boolean expression as the index. Those elements are
returned which satisfy that Boolean expression. It is used for filtering the desired
element values
import numpy as np
a = np.array([10, 40, 80, 50, 100])
print(a[a>50])
Slicing
import numpy as np
# Arrange elements from 0 to 19
a = np.arange(20)
print("n Array is:n ",a)
# a[ start:stop:step]
print("n a[-8:17:1] = ",a[-8:17:1])
# The : operator means all elements till the end.
print("n a[10:] = ",a[10:])
Basic operations on single array
import numpy as np
a = np.array([1, 2, 5, 3])
# add 1 to every element
print ("Adding 1 to every element:", a+1)
# subtract 3 from each element
print ("Subtracting 3 from each element:", a-3)
# multiply each element by 10
print ("Multiplying each element by 10:", a*10)
# square each element
print ("Squaring each element:", a**2)
Unary operators in numpy
import numpy as np
arr = np.array([[1, 5, 6], [4, 7, 2], [3, 1, 9]])
# maximum element of array
print ("Largest element is:", arr.max())
#print first columns in the array
print ("Row-wise maximum elements:", arr.max(axis = 1))
# minimum element of array
print ("Column-wise minimum elements:", arr.min(axis = 0))
# sum of array elements
print ("Sum of all array elements: “,arr.sum())
Binary operators in NumPy
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[4, 3],[2, 1]])
# add arrays
print ("Array sum:n", a + b)
# multiply arrays (element wise multiplication)
print ("Array multiplication:n", a*b)
# matrix multiplication
print ("Matrix multiplication:n", a.dot(b))
NumPy Functions:-
❖ To find the dimensional of the array - (arr_name.ndim)
❖To find the datatype of the array - (arr_name.dtype)
❖To copy the array elements - (arr_name.copy)
❖To viewing the array - (arr_name.view)
❖To change the data type of the array - (arr_name.astype(bool))

ACFrOgAabSLW3ZCRLJ0i-To_2fPk_pA9QThyDKNNlA3VK282MnXaLGJa7APKD15-TW9zT_QI98dAH5IOk2eYNgVYyu1dOTWKxFVJaorlNte-3MHkwdssPWXbcgzAPTlMuiQ4_Kkpyf9vaMj3LhdE.pdf

  • 1.
  • 2.
    What is NumPy? ❖Numericalpython Package. ❖It is used for working with the scientific calculations ,Mathematical calculations and general purpose of array processing. ❖ It provides a high performance multidimensional array object , and tools for working with these arrays. ❖ Very fast than list. ❖ Elements in Numpy arrays are accessed by using square brackets and can be initialized by using nested Python Lists.
  • 3.
    Things to knowin NumPy:- ❖ Difference between List and Numpy ❖Numpy alias name ❖nd array ❖Type of arrays ❖.ndim array
  • 4.
    #Import numpy package importnumpy as np a= np.array([12,78,456,90,10]) print(a)
  • 9.
    arange() import numpy asnp #print the values from 0 to 50 inside an array a= np.arange(50) print(a)
  • 10.
    Updating array values Wecan update the array values with the help of index. a=[1,2,3,4,5] a[1]=44 print(a)
  • 11.
    reshape() #To declare rowsand columns of an array import numpy as np a=np.arange(15).reshape(5,3) print(a) #Accessing Array elements a[1,1] a[3,2]
  • 12.
    Indexing import numpy asnp # NumPy array with elements from 1 to 9 x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]) # Index values can be negative. arr = x[ np. array([1, 3, -3])] print(arr)
  • 13.
    Boolean Indexing This indexinghas some boolean expression as the index. Those elements are returned which satisfy that Boolean expression. It is used for filtering the desired element values import numpy as np a = np.array([10, 40, 80, 50, 100]) print(a[a>50])
  • 14.
    Slicing import numpy asnp # Arrange elements from 0 to 19 a = np.arange(20) print("n Array is:n ",a) # a[ start:stop:step] print("n a[-8:17:1] = ",a[-8:17:1]) # The : operator means all elements till the end. print("n a[10:] = ",a[10:])
  • 15.
    Basic operations onsingle array import numpy as np a = np.array([1, 2, 5, 3]) # add 1 to every element print ("Adding 1 to every element:", a+1) # subtract 3 from each element print ("Subtracting 3 from each element:", a-3) # multiply each element by 10 print ("Multiplying each element by 10:", a*10) # square each element print ("Squaring each element:", a**2)
  • 16.
    Unary operators innumpy import numpy as np arr = np.array([[1, 5, 6], [4, 7, 2], [3, 1, 9]]) # maximum element of array print ("Largest element is:", arr.max()) #print first columns in the array print ("Row-wise maximum elements:", arr.max(axis = 1)) # minimum element of array print ("Column-wise minimum elements:", arr.min(axis = 0)) # sum of array elements print ("Sum of all array elements: “,arr.sum())
  • 17.
    Binary operators inNumPy import numpy as np a = np.array([[1, 2], [3, 4]]) b = np.array([[4, 3],[2, 1]]) # add arrays print ("Array sum:n", a + b) # multiply arrays (element wise multiplication) print ("Array multiplication:n", a*b) # matrix multiplication print ("Matrix multiplication:n", a.dot(b))
  • 18.
    NumPy Functions:- ❖ Tofind the dimensional of the array - (arr_name.ndim) ❖To find the datatype of the array - (arr_name.dtype) ❖To copy the array elements - (arr_name.copy) ❖To viewing the array - (arr_name.view) ❖To change the data type of the array - (arr_name.astype(bool))