Python Numpy
Python Numpy
NUMPY
NUMPY - ARRAY
NumPy stands for Numerical Python.It is the core library for
scientific computing in Python. It consist of multidimensional
array objects, and tools for working with these arrays.
Arrays
Numpy Array is a grid of values with same type, and is indexed
by a tuple of nonnegative integers. The number of dimensions of
it ,is the rank of the array; the shape of an array depends upon a
tuple of integers giving the size of the array along each
dimension.
ii) ndarray.shape: It gives the sequence of integers indicating the size of the
array for each dimension.
iv) ndarray.dtype: is the data type of the elements of the array. All the
elements of an array are of same data type. Common data types are int32,
int64, float32, float64, U32, etc.
NUMPY - ARRAY
Attributes of NumPy Array
v) ndarray.itemsize: It specifies the size in bytes of each element of the
array. Data type int32 and float32 means each element of the array occupies
32 bits in memory. 8 bits form a byte. Thus, an array of elements of type
int32 has itemsize 32/8=4 bytes. Likewise, int64/float64 means each item
has itemsize 64/8=8 bytes.
NUMPY - ARRAY
Data types: Some of the data types supported by Numpy
NUMPY - ARRAY
Data types: Some of the data types supported by Numpy
NUMPY - ARRAY
1 D ARRAY
Creation of 1D array
One dimension array can be created using array method with list object
with one dimensional elements.
e.g.program
import numpy as np
a = np.array([500, 200, 300]) # Create a 1D Array
print(type(a)) # Prints "<class 'numpy.ndarray'>"
print(a.shape) # Prints "(3,)" means dimension of array
print(a[0], a[1], a[2]) # Prints "500 200 300"
a[0] = 150 # Change an element of the array
print(a)
NUMPY - ARRAY
1 D ARRAY
Creation of 1D array Using functions
import numpy as np
p = np.empty(5) # Create an array of 5 elements with random values
print(p)
import numpy as np
x = np.array([1, 2, 3])
y=x
z = np.copy(x)
x[0] = 10
print(x)
print(y)
print(z)
This function builds an ndarray object from any iterable object. A new one-
dimensional array is returned by this function.
OUTPUT:
NUMPY - ARRAY
numpy.linspace()
This function is similar to arange() function. In this function, instead of step size, the
number of evenly spaced values between the interval is specified. The usage of this
function is as follows:
import numpy as np
data = np.array([5,2,7,3,9])
print (data[:]) #print [5 2 7 3 9]
print(data[1:3]) #print [2 7]
print(data[:2]) #print [5 2]
print(data[-2:]) #print [3 9]
NUMPY - ARRAY
1 D ARRAY JOINING
Joining of two or more one dimensional array
is possible with the help of
concatenate() function of numpy object.
e.g.program
import numpy as np
a = np.array([1, 2, 3])
b = np.array([5, 6])
c=np.concatenate([a,b,a])
print(c) #print [1 2 3 5 6 1 2 3]
NUMPY - ARRAY
Print all subsets of a 1D Array
If A {1, 3, 5}, then all the possible/proper subsets of A are { }, {1}, {3}, {5}, {1,
3}, {3, 5}
e.g.program
import pandas as pd
import numpy as np
def sub_lists(list1):
# store all the sublists
sublist = [[]]
# first loop
for i in range(len(list1) + 1):
# second loop
for j in range(i + 1, len(list1) + 1):
# slice the subarray
sub = list1[i:j]
sublist.append(sub)
return sublist OUTPUT
x = np.array([1, 2, 3,4]) [[], array([1]), array([1, 2]),
# driver code array([1, 2, 3]), array([1, 2, 3, 4]),
print(sub_lists(x)) array([2]), array([2, 3]), array([2, 3, 4]),
array([3]), array([3, 4]), array([4])]
NUMPY - ARRAY
Basic arithmetic operation on Aggregate operation on 1D
1D Array Array
e.g.program e.g.program
This technique stores firstly the first row of the array, then second row and so
forth.
This technique stores firstly the first column of the array, then second column
and so forth.
Column-
Row-Major Major
NUMPY - ARRAY
2 D ARRAY
Creation of 2D array
Two dimension array can be created using array method with list object
with two dimensional elements.
e.g.program
import numpy as np
a = np.array([[3, 2, 1],[1, 2, 3]]) # Create a 2D Array
print(type(a)) # Prints "<class 'numpy.ndarray'>"
print(a.shape) # Prints (2, 3)
print(a[0][1]) # Prints 2
a[0][1] = 150 # Change an element of the array
print(a) # prints [[ 3 150 1] [ 1 2 3]]
NUMPY - ARRAY
2 D ARRAY
Creation of 2D array Using functions
import numpy as np
p = np.empty([2,2]) # Create an array of 4 elements with random values
print(p)
e.g. program
import numpy as np
A = np.array([1,2,3,4,5,6])
B = np.reshape(A, (2, 3))
print(B)
OUTPUT
[[1 2 3]
[4 5 6]]
NUMPY - ARRAY
2 D ARRAY SLICES
Slicing of numpy 2d array elements is just similar to
slicing of list elements with 2 dimension.
e.g.program
import numpy as np
A = np.array([[7, 5, 9, 4],
[ 7, 6, 8, 8],
[ 1, 6, 7, 7]])
print(A[:2, :3]) #print elements of 0,1 rows and 0,1,2 columns
print(A[:3, ::2]) #print elements of 0,1,2 rows and alternate column
position
print(A[::-1, ::-1]) #print elements in reverse order
print(A[:, 0]) #print all elements of 0 column
print(A[0, :]) #print all elements of 0 rows
print(A[0]) #print all elements of 0 row
NUMPY - ARRAY
2 D ARRAY JOINING
e.g.program
import numpy as np
A = np.array([[7, 5],
[1, 6]])
# concatenate along the first axis OUTPUT
print(np.concatenate([A, A])) [[7 5]
# concatenate along the second [1 6]
axis (zero-indexed) [7 5]
[1 6]]
In 2-D array, sorting can be done along either of the axes i.e., row-wise or
column-wise. By default, sorting is done row-wise (i.e., on axis = 1). It means
to arrange elements in each row in ascending order. When axis=0, sorting is
done column-wise, which means each column is sorted in ascending order.
NUMPY - ARRAY
Splitting arrays
We can split an array into two or more subarrays. numpy.split() splits an array
along the specified axis. We can either specify sequence of index values where an
array is to be split; or we can specify an integer N, that indicates the number of
equal parts in which the array is to be split, as parameter(s) to the NumPy.split()
function. By default, NumPy.split() splits along axis = 0. Consider the array given
below:
NUMPY - ARRAY
Splitting arrays
NUMPY - ARRAY
Splitting arrays
NUMPY - ARRAY
2 D ARRAY – ARITHMETIC OPERATION
Arithmetic operation over 2d array is possible with
add,substract,multiply,divide () functions.
E.G.PROGRAM
import numpy as np
a = np.array([[7, 5, 9], OUTPUT
[ 2, 6, 8]]) [[7 5 9]
print(a) [2 6 8]]
b = np.array([10,10,10])
c=np.add(a,b) # c=a+b, similar [[17 15 19]
[12 16 18]]
print(c)
c=np.subtract(a,b) # c=a-b, similar [[-3 -5 -1]
print(c) [-8 -4 -2]]
c=np.multiply(a,b) # c=a*b, similar
print(c) [[70 50 90]
c=np.divide(a,b) # c=a/b, similar [20 60 80]]
print(c) [[0.7 0.5 0.9]
[0.2 0.6 0.8]]
Note:-
1. if both 2d arrays are with same dimension[matrix form] then one to one
arithmetic operation will be performed.
2. No of elements of a dimension must match otherwise error message thrown
NUMPY - ARRAY
2 D ARRAY – ARITHMETIC OPERATION
Arithmetic operation over 2d array can be done with single value
also.
E.G.PROGRAM
import numpy as np
a = np.array([[7, 5, 9], OUTPUT
[ 2, 6, 8]]) [[7 5 9]
print(a) [2 6 8]]
c=np.add(a,2)
print(c) [[ 9 7 11]
c=np.subtract(a,2) [ 4 8 10]]
print(c)
[[5 3 7]
[0 4 6]]
c=np.multiply(a,2)
print(c) [[14 10 18]
[ 4 12 16]]
c=np.divide(a,2)
print(c) [[3.5 2.5 4.5]
[1. 3. 4. ]]
NUMPY - ARRAY
2 D ARRAY – Mathematical Functions
Maths functions like power,abs,ceil,floor,around and trigonometric
functions like sin,cos,tan,asin etc are supported by numpy
E.G.PROGRAM
import numpy as np
a = np.array([[7.333, 5.223],
[ 2.572, 6.119]]) OUTPUT
print(np.power(a,2)) [[53.772889 27.279729]
[ 6.615184 37.442161]]
Output:
10
1
4
2.5
2.5
NUMPY - ARRAY
2 D ARRAY
VARIANCE
The average of the squared differences from the Mean.
STEPS
1. find the mean of values
2. Subtract value with mean value then square the result,sum all
results of each value
3. Find the average of sum value
e.g. program
import numpy as np
b = np.array([600,470,170,430,300])
print(b.mean()) # print 394.0
print(np.var(b,ddof=0)) # print 21704.0
Example
variance variance
YEAR ITEM1 PRICE ITEM 2 PRICE A B AxB
2015 1000 130 -266.667 10 -2666.67
2016 1200 110 -66.6667 -10 666.6667
2017 1600 120 333.3333 10 3333.333
1266.666667 120 1333.333
covariance= 666.6667
import numpy as np
a = np.array([1000,1200,1600])
b = np.array([130,110,120])
print(np.cov(a,b,bias=True)[0,1])
OUTPUT
-666.6666666666666
NUMPY - ARRAY
2 D ARRAY
CORRELATION
Correlation is the scaled measure of covariance. Besides, it is
dimensionless. In other words, the correlation coefficient is always a
pure value and not measured in any units.
Formula
import numpy as np
a = np.array([1000,1200,1600])
b = np.array([130,110,120])
print(np.corrcoef(a, b))
OUTPUT
[[ 1. -0.32732684]
[-0.32732684 1. ]]
NUMPY - ARRAY
2 D ARRAY
Linear Regression
Linear regression is a method used to find a relationship between a
dependent variable and independent variable(s).
Types
1. Simple Linear Regression: There is only one independent
variable in it. E.g. the price of the house depend only one field
that is the size of the plot.
2. Multiple Linear Regression: There is more independent variable
in it. E.g. the price of the house depend one field that is the size
of the plot and number of rooms.
import numpy as np
import matplotlib.pyplot as plt
def estcoefficients(x,y):
n=np.size(x)
meanx, meany = np.mean(x), np.mean(y)
sy = np.sum(y*x - n*meany*meanx)
sx = np.sum(x*x - n*meanx*meanx)
a=sx/sy
b=meany-a*meanx
return(a,b)
def plotregline(x,y,b):
plt.scatter(x,y,color="r",marker="o",s=30)
ypred=b[0]+b[1]*x
plt.plot(x,ypred,color="g")
plt.xlabel('SIZE')
plt.ylabel('COST')
plt.show()
x=np.array([10,20,30,40,50]) # independent variable
y=np.array([400,800,1100,1700,2100]) # dependent variable
b=estcoefficients(x,y)
plotregline(x,y,b)