Numpy
Numpy
Array function:
sum(): It returns the sum of array elements over the specified
axis.
Syntax: sum(arr, axis)
Parameters :
arr : input array.
axis : axis = 0 means along the column and axis = 1 means
along the row. Otherwise works on all the axis.
Ex:
import numpy as np
x=np.array([[1,2],[3,4]])
s=np.sum(x, axis=0)
print(s) // 4 6
mean():
It computes the arithmetic mean (average) of the given data
(array elements) along the specified axis.
Syntax:
mean(arr, axis = None)
Example:
import numpy as np
a= np.array([[0, 1, 2], [3, 4, 5]])
m=np.mean(a)
print(m)
m1=np.mean(a,axis=0)
print(m1)
var() :
In statistics, variance measures how far a set of numbers are
spread out from their mean value.
Compute the variance of the given data (array elements)
along the specified axis(if any).
Syntax:
var(arr, axis = None)
Example:
import numpy as np
a= np.array([[0, 1, 2], [3, 4, 5]])
print(np.var(a)) //2.9
std():
It shows how much variation in the data.
A low standard deviation indicates that the data points tend to be
very close to the mean, whereas high standard deviation indicates
that the data is spread out over a large range of values.
It computes the standard deviation of the given data along the
specified axis(if any)..
Syntax:
std(arr, axis = None)
Example:
import numpy as np
a= np.array([[0, 1, 2], [3, 4, 5]])
print("std of arr : ", np.std(a)) //1.7
cumprod() :
It is used to compute the cumulative product of array
elements over a given axis.
Syntax:
cumprod(arr, axis=None)
Example:
import numpy as np
a = np.array([ [1, 3, 5], [2, 4, 6]])
csum = np.cumprod(a)
print ("cumulative sum of array elements: ", csum)
Output: [ 1 3 15 30 120 720]
argmin, argmax :
Returns Indices of minimum and maximum elements,
respectively of particular axis.
Syntax:
argmax(arr, axis=None)
argmin(arr, axis=None)
Example:
import numpy as np
a = np.array([ [1, 3, 7], [2, 8, 6]])
amax = np.argmax(a,axis=0)
amin=np.argmin(a,axis=0)
print (amax) # [1 1 0]
print (amin) # [0 0 1]
Ch .Venkata RamiReddy Department Of Computer Science and Engineering
Sorting
import numpy as np
ints = np.array([3, 3, 3, 2, 2, 1, 1, 4, 4])
print(np.unique(ints)) # [1,2,3,4]
import numpy as np
x= np.array([1,2,3,4,5])
y=np.array([4,5,6,7,8])
print(np.intersect1d(x,y)) #[4,5]
print(np.union1d(x,y)) #[1,2,3,4,5,6,7,8]
print(np.setdiff1d(x,y)) #[1,2,3]
print(np.setxor1d(x,y)) #[1,2,3,6,7,8]