03-Python Libraries - Numpy - Matplotlib
03-Python Libraries - Numpy - Matplotlib
Data mining
from data
sources.
Python Packages for Data Science & AI
Python Libraries for Data Mining
➔ There are several Python tools for scraping data used in
Python machine learning models.
➔ These libraries are known for web crawling, data scraping
and arrange it into the required format
Python Libraries for Data Analytics
➔ There are several Python tools for processing and
visualizing data.
Machine Learning Python Libraries
(Traditional Algorithms)
➔ provide the implementation of traditional Machine Learning
Algorithms like classification (SVM, Random Forest, Decision Tree,
etc), Clustering (K-Mean, etc ),... except neural networks.
Deep Learning Python Libraries
Reinforcement Learning Python Libraries
➔ There are libraries designed to have all the necessary
tools to both implement and test Reinforcement Learning
models.
NLP Machine Learning
➔ There are many Python libraries that can be used for
Libraries
practically implementing natural language processing
(NLP) and text mining tasks.
Computer Vision Python Libraries
➔ Python provides several computer vision libraries and
frameworks for developers to help them automate tasks,
which includes detections and visualisations.
Introduction to Numpy
Content
➢ Introduction to NumPy
➢ Why should use Numpy?
➢ Numpy Array
➢ Numpy Linear Algebra
➢ Numpy Matrix Library matlib
➢ I/O with Numpy
Introduction to NumPy
➔ NumPy is short for Numerical Python
➔ Array oriented computing
➔ Efficiently implemented multi-dimensional arrays
➔ Used for scientific computing.
Why should we use Numpy?
➔ Convenient interface for working with multi-dimensional
array data structures efficiently (ndarray).
➔ Less memory to store the data.
➔ High computational efficiency
Numpy Getting Started
➔ Installing Numpy: pip install numpy
➔ Import Numpy: import numpy
➔ Alias of Numpy: import numpy as np
➔ Check Numpy version: np.__version__
Numpy Data Types
➔ supports a much greater variety of numerical
types than Python does
Boolean bool_
Integer int_, intc, intp, int8, int16, int32,
int64
Unsigned uint8, uint16, uint32, uint64
Integer
Float float_, float 16, float32, float 64
Complex complex_, complex64,
complex128
Numpy Array
3-D Array
1 2 2
➔ ndarray (N-Dimensional array) 1 1 91 0 1
2 2 2
0 1 22
1 21 31 21
3
2
4
2
2-D Array 3 4 55
0-D Array
Axis 0 4 51 61 1 6 7
1 2 3 6 7 8
1 7 8 9 Axis 2
Axis 0 4 5 6 Matr
Axis 1
np.array(1 7 8 9 ix np.array([[[1, 2, 3],
) 1-D Array Axis 1 [4, 5, 6],
[7, 8, 9]],
np.array([[1, 2, 3],
1 2 3 Vect [[10, 11, 12],
or [13, 14, 15],
[4, 5, 6],
[16, 17, 18]],
np.array([1, 2, [7, 8,
[[19, 20, 21],
3]) 9]])
[22, 23, 24],
[25, 26,
27]]])
Numpy Array
✓ Numpy Array Creation
✓ Numpy Array Indexing
✓ Numpy Array Slicing
✓ Numpy Arithmetic Operations
✓ Numpy Arithmetic Functions
✓ Numpy Array Manipulation Functions
✓ Numpy Broadcasting
✓ Numpy Statistical Operations
Numpy Array Creation
➔ Using the function array()import numpy as np
arr = np.array([1, 2, 3, 4,
5])
➔ Converting from lists, tuples.
arr[2,0
]
arr[2,:,:]
arr[:,1,: arr[:,:,0
] ]
Numpy Array Slicing
1-D Array 2-D Array
arr[:1]
[start:end]
3-D Array
arr[1:,2:4]
arr[:2,1:,:2
]
Numpy Arithmetic Operations
import numpy as np [[ 0, 1, 2]
a = np.arange(9, dtype = np.float_).reshape(3,3) [ 3, 4, 5 ]
b = np.array([10,10,10]) [ 6, 7,
[10, 10,
10] 8 ]]
np.add (a,b) np.subtract (a,b) np.multiply (a,b)
[[ 10, 11, 12 ] [[ 10, 11, 12] [[-10, -9, -8]
[ 13, 14, 15 ] [ 13, 14, 15] [-7, -6, -5]
[ 16, 17 18 ]] [ 16, 17, 18]] [-4, -3, -2]]
[[ 0, 0.1, 0.2]
np.divide [ 0.3, 0.4,
(a,b) 0.5] [ 0.6,
0.7, 0.8]]
Numpy Arithmetic Functions
import numpy as np
a =
np.array([7,3,4,5,1])
b =
np.remainder (a,b) np.power (a,b)
np.array([3,4,5,6,7])
[343, 81,1024,15625,
[1, 3, 4, 5, 1]
1]
np.mod (a,b) np.reciprocal (a)
[1, 3, 4, 5, 1] [0, 0, 0, 0, 1]
Numpy Array Manipulation
Functions
np.reshape(3,2) np.insert([1],[3],[4],
np.transpose(
np.delete(1,0 )[5],axis=1)
)
axis=0
axis=1
➔ np.mean(array, [axis],...)
axis=1 axis=0
Numpy Statistical Operations
➔ np.std(array,[axis],...) # standard deviation
axis=0 axis=1
axis=0 axis=1
Numpy Linear Algebra
➔ Linalg : the package in NumPy for Linear Algebra
➔ dot(): product of two arrays
vdot(): Complex-conjugating dot product
Example : a = [[1, 0], [0, 1]]
>>> b = [[4, 1], [2, 2]]
>>> np.dot(a, b)
array([[4, 1], [2, 2]])
Numpy Linear Algebra
➔ inner(): product of two arrays
- numpy.inner(a, b, /)
- a, b: array_like
If a and b are non-scalar, their last dimensions must match
- Returns: out: ndarray
If a and b are both scalars or both 1-D arrays then a scalar is
returned; otherwise an array is returned. out.shape =
(*a.shape[:-1], *b.shape[:-1])
Numpy Linear Algebra
Ex:
#The input array is stored in a disk file with
the numpy.save() file and with an npy
extension.
import numpy as np #The output as:
a = np.array([1,2,3,4,5]) array([1, 2, 3, 4,
np.save('outfile',a) 5])
# use load() function to reconstruct
import numpy as np
b = np.load('outfile.npy')
print b
I/O with Numpy
➔ numpy.savetxt() and numpy.loadtxt() functions help in
storage and retrieval of the array data in simple text file format.
Ex:
import numpy as np
a = np.array([1,2,3,4,5])
np.savetxt('out.txt',a)
b = np.loadtxt('out.txt')
print b
The output produced appears as: [ 1. 2. 3. 4. 5.]
Introduction to Matplotlib
Content
➢ Introduction
➢ Install
➢ Pandas vs SQL
➢ Pandas Features
➢ Pandas Getting Started With
➢ Pandas Data Structure
➢ Working on DataFrames
Introduction
➔ Data Visualization:
● is the process of presenting data in the form of graphs or charts.
● is also used in high-level data analysis for Machine Learning and
Exploratory Data Analysis (EDA)
➔ Matplotlib
● is a low-level library of Python which is used for data visualization.
● is easy to use and emulates MATLAB like graphs and visualization.
Install
➔ Step 1 − Make sure Python and pip is preinstalled on your
system:
● Check Python : “python --version” ;
● Check pip : “pip -V”
➔ Step 2 − Install Matplotlib
● Command : “pip install matplotlib”
➔ Step 3 - Check if it is installed successfully
● Command : “import matplotlib
Matplotlib.__version__”
Types of plot in matplotlib
➔ Line Plot
➔ Example:
Types of plot in matplotlib
➔ Bar Plot
➔ Example:
Types of plot in matplotlib
➔ Histogram
➔ Example:
Types of plot in matplotlib
➔ Scatter Plot
➔ Example:
Types of plot in matplotlib
➔ Adding title and Labeling the Axes in the graph
➔ Add Title:
“matplotlib.pyplot.title("My title")”
➔ Label the x-axis and y-axis :
“matplotlib.pyplot.xlabel("Time (Hr)")
matplotlib.pyplot.ylabel("Position (Km)")”
Types of plot in matplotlib
➔ Adding title and Labeling the Axes in the graph
➔ Example:
Types of plot in matplotlib
➔ Multiple Graphs: by repeating the show() function or use a
function called subplot() in order to print them horizontally as well.
➔ Example: