Jovia Report
Jovia Report
Jovia Report
NUMPY-Numpy stands for “Numeric Python” or “Numerical python”. Numpy is a package that
contains several classes, functions, variables etc. to deal with scientific calculations in Python.
Numpy is useful to create and process single and multi-dimensional arrays.
ARRAYS IN NUMPY
1. 1D ARRAY
This is the one dimensional array and it contains elements only in one dimension.
Example
import numpy
a = numpy.array([10,20,30,40,50])
print(a)
a = npp.array([10,20,30,40,50])
print(a)
NB: When we use the statement “from numpy import *” we do not add anything in
front of the array function just as seen below
a = array([10,20,30,40,50])
print(a)
Output
[10, 20,30,40,50]
Arr=array([10,20,30,40,50],int)
print (Arr)
Output
[10, 20,30,40,50]
Note: If you are creating an array and you include a float value, the output will all
be converted to float data type
a=array([10,30,40.5,50,100])
print(a)
Output
[10.0,30.0,40.5,50.0,100.0]
It is used to create an array with evenly spaced points between a starting and
ending point
import numpy as np
a=np.linspace(1,10,10)
print(a)
Output
[ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.]
Syntax
arange(start,stop,stepsize)
Examples
import numpy as np
a = np.arange(10)
b = np.arange(5,10)
c = np.arange(10,1,-1)
print(a)
print(b)
print(c)
Output
[0,1,2,3,4,5,6,7,8,9]
[5,6,7,8,9]
[10, 9, 8, 7, 6, 5, 4, 3, 2]
We can use zeros() function to create an array with all zeros or use ones() function to create an
array with all 1s.
zeros(n,datatype)
ones(n,datatype)
Example
import numpy as np
k = np.zeros(5)
R = np.ones(5)
print(k)
print(R)
Output
[0.,0.,0.,0.,0.]
[1.,1.,1.,1.,1.]
Examples
import numpy as np
k = np.array([10,20,30,40,50])
k = k+5 (to add 5 to the values in the array)
print(k)
print(k)
print(k)
print(k)
Output
[15 25 35 45 55]
[10 20 30 40 50]
Aliasing an array
This means creating a new array in reference to the original array in place. It does not make a
new copy of the array defined earlier. It just refers to it.
Example
import numpy as np
k = np.array([3,5,6,7,8])
print(k)
print(h)
print(k)
k[0] = 45
print(h)
print(k)
Output
[3 5 6 7 8]
[3 5 6 7 8]
[3 5 6 7 8]
[45 5 6 7 8]
[45 5 6 7 8]
import numpy as np
k = np.array([3,5,6,7,8])
print(k)
print(h)
print(k)
k[0]= 45
print(h)
print(k)
Output
[3 5 6 7 8]
[3 5 6 7 8]
[3 5 6 7 8]
[3 5 6 7 8]
[45 5 6 7 8]
2-D ARRAY
This is known as the two dimensional array and it contains more than one row (arranged
horizontally) or column(arranged vertically).
Example
import numpy as np
x =np.array([[2,4,6],[6,8,10]])
print(x)
Output
[ [2 4 6]
[6 8 10] ]
Using the ndim attribute
ndim attribute is used to represent the number of dimensions of axes of the array. The number
of dimensions is also known as “rank”.
import numpy as np
A = np.array([5,6,7,8])
R = np.array([[4,5,6],[7,8,9]])
Output
The shape attribute gives the shape of an array. The shape is tuple listing the number of
elements along each dimension. For 1D array, it will display a single value and for a 2D array it
will display two values separated by commas to represent rows and columns.
Example
import numpy as np
k = np.array([1,2,3,4,5])
d = np.array([[5,6,7],[7,8,9]])
Output
(5,)
(2, 3)
Example
import numpy as np
a1 = np.array([1,2,3,4,5])
print(a1.size)
Output
import numpy as np
k = np.array([[5,6,7],[7,8,9]])
print(k.size)
Output
The itemsize attributes gives the memory size of array elements in bytes.
Example
import numpy as np
a1 = np.array([1,2,3,4,5])
print(a1.itemsize)
Output
NB: The new array should have the same number of elements as in the original array
Example
import numpy as np
d =np.array([[4,5,6,7],[5,6,7,8],[7,8,9,6]])
print(d)
print(d)
print(d)
d = d.reshape(12,1) (to reshape array d to 12rows and 1 column)
print(d)
Output
[[4 5 6 7]
[5 6 7 8]
[7 8 9 6]]
[[4 5]
[6 7]
[5 6]
[7 8]
[7 8]
[9 6]]
[[4 5 6 7 5 6 7 8 7 8 9 6]]
[[4]
[5]
[6]
[7]
[5]
[6]
[7]
[8]
[7]
[8]
[9]
[6]]
The empty() function is used to create the empty array or an uninitialized array of specified
data types and shape.
Example
import numpy as np
x = np.empty([3,2], dtype = int)
print(x)
print(y)
Output
[[0 0]
[0 0]
[0 0]]
The eye() function creates a 2D array and fills the elements in the diagonal with 1s.
Syntax
eye(n, dtype=datatype)
This function will create an array with n rows and n columns with diagonal elements as 1s.
Example
import numpy
a = numpy.eye(3)
print(a)
output
[ [ 1. 0. 0.]
[ 0. 1. 0.]
[0. 0. 1.] ]
Using the zeros() function in 2D array
This function is used to create two dimensional array with the 0 as default value and default
data type is float.
Example
import numpy
print(Q)
print(Z)
Output
[[0 0]
[0 0]
[0 0]]
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
This function will be used to create the array with 1 as default value for each of individual
defined element.
Example
import numpy
Q = numpy.ones([3,2],dtype = int)
Z = numpy.ones([4,4],dtype = float)
print(Q)
print(Z)
Output
[[1 1]
[1 1]
[1 1]]
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
JOINING IN ARRAYS
concatenate()
This is used to join more than one array but the array must be of the same shape
Example
import numpy as np
a=np.array([2,3,4,50])
b=np.array([8,9,10,11,15])
c=np.concatenate([a,b])
print(c)
Output
[2 3 4 50 8 9 10 11 15]
Example 2
import numpy as np
a=np.array([[2,3,4],[4,5,6],[7,8,9]])
print(b)
Output
[ [2 3 4 2 3 4]
[4 5 6 4 5 6]
[7 8 9 7 8 9] ]
Example 3
import numpy as np
a=np.array([[2,3,4],[4,5,6],[7,8,9]])
b=np.concatenate([a,a],axis=0) ( to concatenate array a with array a row wise)
print(b)
Output
[ [2 3 4]
[4 5 6]
[7 8 9]
[2 3 4]
[4 5 6]
[7 8 9] ]
hstack()
This is another joining method used to join more than one array horizontally or row wise.
Example
import numpy as np
a=np.array([1,2,3])
b=np.array([10,11,12])
c=np.hstack((a,b))
print(c)
Output
[1 2 3 10 11 12]
vstack()
This is the third joining method used to join more than one array vertically or column wise.
Example
import numpy as np
a=np.array([1,2,3])
b=np.array([10,11,12])
c=np.hstack((a,b))
print(c)
Output
[[1 2 3 ]
[10 11 12]]
ARRAY SUBSETS
We can get subsets of a numpy array by using any of the following methods;
Using split()
Example
import numpy as np
x =[1,2,3,99,99,3,2,1]
print(x1,x2,x3)
Output
[1 2 3] [99 99] [3 2 1]
Using hsplit()
Example
import numpy as np
a= np.arange(16).reshape((4,4))
print(a)
Output
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
left,right =np.hsplit(a,2)
print(left)
print(right)
Output
[[ 0 1]
[ 4 5]
[ 8 9]
[12 13]]
[[ 2 3]
[ 6 7]
[10 11]
[14 15]]
Using vsplit()
Example
import numpy as np
a= np.arange(16).reshape((4,4))
print(a)
Output
[[ 0, 1, 2, 3]
[ 4, 5, 6, 7]
[ 8, 9, 10, 11]
print(top)
print(bottom)
Output
[[0 1 2 3]
[4 5 6 7]]
[[ 8 9 10 11]
[12 13 14 15]]
STATISTICAL FUNCTION IN NUMPY
Examples
import numpy as np
array1 = np.array([[10,20,30],[40,50,60]])
Output
Mean: 35.0
Std: 17.07825127659933
Var: 291.6666666666667
Sum: 210
Prod: 720000000
Covariance
Example
import numpy as np
x =np.array([0,1,2])
y =np.array([2,1,0])
print("\nOriginal array1:")
print(x)
print("\nOriginal array2:")
print(y)
Output
Original array1:
[0 1 2]
Original array2:
[2 1 0]
[[ 1. -1.]
[-1. 1.]]
REFERENCES