0% found this document useful (0 votes)
32 views15 pages

Numpy

Uploaded by

Its Me SR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views15 pages

Numpy

Uploaded by

Its Me SR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

NUMPY

NumPy can be used to perform a wide variety of mathematical operations on arrays. It adds
powerful data structures to Python that guarantee efficient calculations with arrays and
matrices

Import numpy: -
We can import numpy using the import command

import numpy
We can also use any shorthand name like ‘np’

import numpy as np

Convert a list into array: -


We can convert a list into numpy array using the np.array() method. Converting a list into
array helps to do mathematical operations into it.
arrname = np.array(listname)

arange function: -
The arange( ) function is used to create an array between a range and with specific number
of gaps or steps in between.
arrname = np.arange(start,stop,step,datatype)
The ‘start’ is inclusive but the ‘stop’ is exclusive.
Multidimensional Array: -
We can create a multidimensional array by simply using the np.array function to a
multidimensional list.
arrname = np.array(multidimensional list)

Size function: -
By using the size function we can calculate the number of elements, number of rows,
number of columns.

• arrname.size -> Number of elements


• np.size(arrname,0) -> Number of Rows
• np.size(arrname,1) -> Number of Columns

Shape function: -
The shape function returns the no of rows and columns in an array.
arrname.shape

No of rows No of columns
Dtype function: -
The dtype function is used to show the datatype of an array.
arrname.dtype

Ndim function: -
The dtype function is used to show the dimension of an array whether it is one dimension
or two dimensions.
arrname.ndim

Zeros function: -
The zeros function returns an array of specific shape and specific data type which contains
all zeroes (0).
arrname = np.zeros((row,column),dtype=datatype)
Ones function: -
The ones function returns an array of specific shape and specific data type which contains
all ones (1).
arrname = np.ones((row,column),dtype=datatype)

Eyes function: -
The eyes function returns an identity array where the diagonals are all ones(1) and the
other elements are all zeroes(0).
arrname = np.eye(row,column,dtype=datatype,k)
• By default, row = column but we can also give the number of columns.
• The ‘k’ value specifies where the diagonal will be with respect to the main diagonal.
▪ k>0 means above the main diagonal.
▪ k<0 means below the main diagonal.
• Example: - k = 1 means 1 place above the main diagonal and k = -1 means 1 place
below the main diagonal.

without ‘k’ k=1 k = -1

1 place above main 1 place below main


diagonal diagonal
Random functions: -
There are several random functions that we can use –
• np.random.rand( ) : - This function is used to return an array of given shape and
fills it with random values between 0 and 1.
arrname = np.random.rand(row,column)

• np.random.randint( ) : - This function is used to return random numbers within a


specific range and we can also specify the size.
arrname = np.random.randint(start,end,shape)

The start is inclusive and the stop is exclusive.

• np.random.randn( ) : - This function is used to create an array of specified shape


and fills it with random values as per standard normal distribution.
arrname = np.random.randn(row,column)
Reshape function: -
The reshape function is used to change the shape of the array but the number of elements
should be same before and after reshaping.
Ex: - A 3X4 array has 12 elements so it can be converted to 4X3, 6X2, 2X6, 12X1 and 1X12.
arrname = array.reshape(row,column)

Linspace function: -
The linspace function is used to generates a number of randoms between a range. All the
values are equal distance from each other.
arrname = array.linspace(start,end,number of elements)
The start and end both are inclusive.

Flatten function: -
The flatten function collapses an array into a one-dimensional array.
arrname = array.flatten()
Logspace function: -
The logspace function generates a number of randoms between a range but in the log
space. Ex: - if we want to generate 2 numbers between 2 and 3 then the result will be [100 ,
1000] because log 10 (100) = 2 and log 10 (1000) = 3.
arrname = np.logspace(start, end, no of element,base = ‘base’)

• Start and end both are inclusive.


• By default, the base is 10.

Copy function: -
The copy function is used to make a copy of an array and both the arrays will have different
ID. But if we simply assign one array to the other like – arr1 = arr2 then their ID will be
same.
arr2 = np.copy(arr1)

Without using copy( ) function the By using copy( ) function the ID of


ID of both are same. both are different.
max, min, sum function: -
The max function is used to return the maximum number of a given array.
arrname.max()
The min function is used to return the minimum number of a given array.
arrname.min()
The sum function is used to return the sum of all numbers of a given array.
arrname.sum()
Here we can also set the axis value to get max, min, sum of rows or columns.
• axis = 0 means column wise.
• axis = 1 means row wise.

Overall

Row wise Column wise


Seed function: -
The seed() method is used to initialize the random number generator. If we use the same
seed value then we will get the same random number again and again.
np.random.seed(value)

without using seed using seed

Sort function: -
The sort function is used to sort the array rowwise or column wise and by specifying the
algorithm name like quicksort, mergesort etc.
np.sort(arrayname,axis,kind)

• axis = 1 (by default) means row wise sort.


• axis = 0 means column wise sort.
• kind = name of algorithm e.g. ‘mergesort’, ‘quicksort’ etc.
Mathematical operations: -
There are several mathematical functions that can be done in numpy array like –

• addition (+)
• subtraction (-)
• multiplication (*)
• division (/)
• modulus (%)
• power (**)

Dot product: -
The dot product between two arrays can be done using the ‘dot’ method.
arr1.dot(arr2)
The dot product between two arrays can also be done using the ‘@’ method.
arr1 @ arr2

Percentile function: -
The percentile function is used to compute the nth percentile of the given data (array
elements) along the specified axis.
np.percentile(arr,value)
Here we can also give axis value to select row or column like previous ones.
Mean, Variance and Standard Deviation: -
The mean( ), var( ) and std( ) functions are used to calculate mean, variance and standard
deviation of an array elements respectively.

arr.mean()
arr.var()
arr.std()

Filtering in a numpy array: -


We can filter in a numpy array by directly giving the condition inside the array.
arr[condition]
Transposing a numpy array: -
We can transpose a numpy array by using the ‘.T’ object. Transpose can only be done for an
array having dimensions greater than or equal to 2.
arr.T

Where clause: -
We can use the where clause to do certain things by applying any condition in an array.
np.where(condition,task,arrname)
Merging Arrays: -
We can merge two arrays in different ways and different formats –
• Merge Vertically –
▪ np.vstack((arr1,arr2))
▪ np.concatenate((arr1, arr2), axis=0)

• Merge Horizontally –
▪ np.hstack((arr1,arr2))
▪ np.concatenate((arr1, arr2), axis=1)
Splitting Arrays: -
We can split two arrays in different formats –
• Split Vertically –
▪ np.vsplit((arr1,arr2))

• Split Horizontally –
▪ np.hsplit((arr1,arr2))

You might also like