0% found this document useful (0 votes)
1 views

03-Python Libraries - Numpy - Matplotlib

The document provides an introduction to Python libraries, including the Python Standard Library and third-party libraries, which facilitate programming by allowing code reuse. It covers the use of libraries in Python programs, particularly for data science and machine learning, and introduces NumPy for numerical computing. Additionally, it discusses NumPy's features, array manipulation, and I/O functions, along with an introduction to data visualization with Matplotlib.

Uploaded by

Nguyễn Lê Vy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

03-Python Libraries - Numpy - Matplotlib

The document provides an introduction to Python libraries, including the Python Standard Library and third-party libraries, which facilitate programming by allowing code reuse. It covers the use of libraries in Python programs, particularly for data science and machine learning, and introduces NumPy for numerical computing. Additionally, it discusses NumPy's features, array manipulation, and I/O functions, along with an introduction to data visualization with Matplotlib.

Uploaded by

Nguyễn Lê Vy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 56

Introduction to Python Libraries

Nguyen Ngoc Thao, B.SE


In Charge of Deep Medicine,
Phan Chau Trinh Medical University
[email protected]
0935192556
Chapter Content
➢ Python Libraries
➢ Use of Libraries in Python Program
➢ The Python Standard Library
➢ 3rd Party Libraries
Python Library
➔ A Python library is a collection of
related modules containing bundles
of code that can be used repeatedly
in different programs.
➔ It makes Python Programming
simpler and convenient for the
programmer when we needn’t to
write the same code again and
again for different programs
Use of Libraries in Python Program
➔ Importing the libraries: # Importing math
library
import math
A = 16
print(math.sqrt(A))
➔ Importing specific items from a library module:
# Importing specific items
from math import sqrt, sin
A = 16
B = 3.14
print(sqrt(A))
print(sin(B))
Python Standard Library
➔ Python Standard library plays
a very important role to access
to the system functionalities of
Python
https://docs.python.org/3/library/
➔ The library contains more than
200 core prebuilt prestested
modules (written in C) that are
provided as part of the default
python installation itself.
3rd Party Libraries
➔ Are open-source libraries (packages) developed by somebody else
and made available for download using a standard interface.
➔ used to create applications and models in a variety of fields, e.g.
machine learning, data science, data visualization, image and data
manipulation, and many more.
➔ Find, install and publish Python packages with the Python
Package Index https://pypi.org/
➔ Using pip to install packages
pip install <packages>
Data Science & AI

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.

list1 = [1, 2, 3, 4, tuple1 = (1, 2, 3, 4, 5)


5] arr = np.array(tuple1)
arr = np.array(list1)
Numpy Array Creation

➔ Using special Numpy functions: empty(shape, dtype) ,


ones(shape, dtype), zeros(shape,dtype),
arange(start,stop,step,dtype), random.random(size),
full(shape, fill_value,dtype), eyes(nrow,[ncol],dtype).

Ex: arr = np.empty(2, dtype=int)


arr = np.zeros(2, dtype=int)
Numpy Array Attributes
(3,3
) 9
ndarray.shape ndarray.size
return the size of array return number of
1 2 3 elements in the array
ndarray.ndim
2 4 5 6
return number of array ndarray.itemsize
dimension 7 8 9
return the size (in bytes)
ndarray.dtype of elements in the array
8
return data type of
elements in the array int64
Numpy Array Indexing
1-D Array 3-D Array arr[1,2,: arr[0,:,1
] ]
arr[0
2-D Array ]
arr[0,2 arr[:,1]
]
arr[1,2,:]

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)
)

np.resize(2,4 np.concatenate((a,b), np.append(x,[[40,50,60],


) 1) [70,80,90]])
Numpy Broadcasting
➔ Broadcasting refers to how numpy treats arrays with different
dimension during arithmetic operations which lead to certain
constraints, the smaller array is broadcast across the larger array
so that they have compatible shapes.
Numpy Broadcasting
➔ Broadcasting Rules:
◆ If the arrays don’t have the same rank then prepend the shape of the lower
rank array with 1s until both shapes have the same length.
◆ The two arrays are compatible in a dimension if they have the same size in the
dimension or if one of the arrays has size 1 in that dimension.
◆ The arrays can be broadcast together iff they are compatible with all
dimensions.
◆ After broadcasting, each array behaves as if it had shape equal to the element-
wise maximum of shapes of the two input arrays.
◆ In any dimension where one array had size 1 and the other array had size
greater than 1, the first array behaves as if it were copied along that dimension.
Numpy Statistical Operations
➔ np.median(array,[axis],...)

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

➔ np.var(array, [axis],...) # variance

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

➔ outer(): compute the outer product of two vectors


numpy.outer(a, b, out=None)
Parameters:
- a : (M,) array_like
First input vector. Input is flattened if not already 1-dimensional.
- b : (N,) array_like
Second input vector. Input is flattened if not already 1-dimensional.
- out : (M, N) ndarray, optional
A location where the result is stored
Numpy Linear Algebra
➔ matmul(): Matrix product of two arrays
numpy.matmul(x1, x2, /, out=None, *, casting='same_kind',
order='K', dtype=None, subok=True[, signature, extobj, axes, axis])
= <ufunc 'matmul'>
Parameters: x1, x2: array_like
Input arrays, scalars not allowed.
Out: ndarray, optional
If provide allocation, it must have a shape that matches the
signature (n,k),(k,m)->(n,m). If not provided or None, a freshly-
allocated array is returned.
Numpy Linear Algebra
Another linear algebra functions
➔ det()
➔ inv()
➔ trace()
➔ rank()
Numpy Matrix Library matlib
➔ has functions that return matrices instead of ndarray objects.
import numpy as np
import numpy.matlib
# with the specified shape and type without initializing entries
mat_e =np.matlib.empty((3, 2), dtype = int)
# filled with 0
mat_zeros = np.matlib.zeros(5, 3)
# filled with 1
mat_ones = np.matlib.ones(4, 3)
# diagonal elements filled with 1, others with 0
mat_ones = np.matlib.eye(3,5)
# create square matrix with 0, diagonal filled with 1, others
with 0
mat_zeros = np.matlib.identity(5)
# filled with random data
mat_e =np.matlib.empty(3, 2))
I/O with Numpy
➔ What are the I/O functions of NumPy?
The I/O functions provided by NumPy are:
● load() and save() functions handle numPy binary files (with
npyextension)
● loadtxt() and savetxt() functions handle normal text files
I/O with Numpy
➔ numpy.save(): The input array is stored in a disk file with
the numpy.save() file and with an npy extension.

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:

You might also like