You are on page 1of 15

The Numpy

 One of the key features of NumPy is its N-dimensional array


object, or ndarray, which is a fast, flexible container for large
data sets in Python.
 An ndarray is a multidimensional container for homogeneous
data; that is, all of the elements must be the same type.
 Every item in an ndarray takes the same size of block in the
memory.
 The number of dimensions is the rank of the array

Ch .Venkata RamiReddy Department Of Computer Science and Engineering


The Numpy

Array function:

The easiest way to create an array is to use the array function.


array function accepts any sequence- like tuple and list and
produces a new array.

a = np.array([1, 2, 3]) # Create a rank 1 array


print(a[0], a[1], a[2]) # Prints "1 2 3"
b = np.array([[1,2,3],[4,5,6]]) # Create a rank 2 array
print(b[0, 0], b[0, 1], b[1, 0]) # Prints "1 2 4"

Ch .Venkata RamiReddy, Department Of Computer Science and Engineering


Mathematical and Statistical Methods

Some of the Mathematical and statistical methods are


1. sum()
2. min()
3. max()
4. mean()
5. var()
6. std()
7. cumsum()
8. cumprod()
9. argmin()
10. argmax()

Ch .Venkata RamiReddy Department Of Computer Science and Engineering


Mathematical and Statistical Methods

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

Ch .Venkata RamiReddy Department Of Computer Science and Engineering


Mathematical and Statistical Methods
max():
 It finds the maximum element in an array over the specified
axis.
Syntax: max(arr, axis)
x=np.array([[1,2],[3,4]])
print (np.max(x))
min():
 It finds the minimum element in an array over the specified
axis.
Syntax: min(arr,axis)
x=np.array([[1,2],[3,4]])
print (np.min(x))
Ch .Venkata RamiReddy Department Of Computer Science and Engineering
Mathematical and Statistical Methods

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)

Ch .Venkata RamiReddy Department Of Computer Science and Engineering


Mathematical and Statistical Methods

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

Ch .Venkata RamiReddy Department Of Computer Science and Engineering


Mathematical and Statistical Methods

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

Ch .Venkata RamiReddy Department Of Computer Science and Engineering


Mathematical and Statistical Methods
cumsum() :
 It is used to compute the cumulative sum of array elements
over a given axis.
Syntax:
cumsum(arr, axis=None)
Example:
import numpy as np
 a = np.array([ [1, 3, 5], [2, 4, 6]])
 csum = np.cumsum(a) 
print ("cumulative sum of array elements: ", csum)
Output: [ 1 4 9 11 15 21]
Ch .Venkata RamiReddy Department Of Computer Science and Engineering
Mathematical and Statistical Methods

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]

Ch .Venkata RamiReddy Department Of Computer Science and Engineering


Mathematical and Statistical Methods

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

np.sort() : This function returns a sorted copy of an array.


 np.sort returns a sorted copy of an array instead of modifying
the array in place.
Ex:
import numpy as np
a= np.array([1,2,8,9,7,5,4,3,6])
print(np.sort(a))

Ch .Venkata RamiReddy Department Of Computer Science and Engineering


Unique and Other Set Logic operations

 NumPy has some basic set operations for one-dimensional


ndarrays.
unique(x): Compute the sorted, unique elements in x
intersect1d(x, y) : Compute the sorted, common elements in x
and y
union1d(x, y): Compute the sorted union of elements
setdiff1d(x, y): Set difference, elements in x that are not in y
setxor1d(x, y): Set symmetric differences; elements that are in
either of the arrays, but not both

Ch .Venkata RamiReddy Department Of Computer Science and Engineering


Unique and Other Set Logic operations

import numpy as np
ints = np.array([3, 3, 3, 2, 2, 1, 1, 4, 4])
print(np.unique(ints)) # [1,2,3,4]

Ch .Venkata RamiReddy Department Of Computer Science and Engineering


Unique and Other Set Logic operations

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]

Ch .Venkata RamiReddy Department Of Computer Science and Engineering

You might also like