SlideShare a Scribd company logo
Python Numpy/Pandas Libraries
Machine Learning
Portland Data Science Group
Created by Andrew Ferlitsch
Community Outreach Officer
July, 2017
Libraries - Numpy
• A popular math library in Python for Machine Learning
is ‘numpy’.
import numpy as np
Keyword to import a library Keyword to refer to library by an alias (shortcut) name
Numpy.org : NumPy is the fundamental package for scientific computing with Python.
• a powerful N-dimensional array object
• sophisticated (broadcasting) functions
• tools for integrating C/C++ and Fortran code
• useful linear algebra, Fourier transform, and random number capabilities
Libraries - Numpy
http://www.physics.nyu.edu/pine/pymanual/html/chap3/chap3_arrays.html
The most import data structure for scientific computing in Python
is the NumPy array. NumPy arrays are used to store lists of numerical
data and to represent vectors, matrices, and even tensors.
NumPy arrays are designed to handle large data sets efficiently and
with a minimum of fuss. The NumPy library has a large set of routines
for creating, manipulating, and transforming NumPy arrays.
Core Python has an array data structure, but it’s not nearly as versatile,
efficient, or useful as the NumPy array.
Numpy – Multidimensional Arrays
• Numpy’s main object is a multi-dimensional array.
• Creating a Numpy Array as a Vector:
Numpy function to create a numpy array
Value is: array( [ 1, 2, 3 ] )
data = np.array( [ 1, 2, 3 ] )
• Creating a Numpy Array as a Matrix:
data = np.array( [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] )
OuterDimension Inner Dimension (rows)
Value is: array( [ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ] )
Numpy – Multidimensional Arrays
• Creating an array of Zeros:
Numpy function to create an array of zeros
Value is: array( [ 0, 0, 0 ],
[ 0, 0, 0 ] )
data = np.zeros( ( 2, 3 ), dtype=np.int )
rows
columns
• Creating an array of Ones:
Value is: array( [ 1, 1, 1 ],
[ 1, 1, 1 ] )
data type (default is float)
Numpy function to create an array of ones
data = np.ones( (2, 3), dtype=np.int )
And many more functions: size, ndim, reshape, arange, …
Libraries - Pandas
• A popular library for importing and managing datasets in Python
for Machine Learning is ‘pandas’.
Keyword to import a library Keyword to refer to library by an alias (shortcut) name
import pandas as pd
Used for:
• Data Analysis
• Data Manipulation
• Data Visualization
PyData.org : high-performance, easy-to-use data structures and data analysis tools for the
Python programming language.
Pandas – Indexed Arrays
• Pandas are used to build indexed arrays (1D) and matrices (2D),
where columns and rows are labeled (named) and can be accessed
via the labels (names).
1 2 3 4
4 5 6 7
8 9 10 11
1 2 3 4
4 5 6 7
8 9 10 11
one
two
three
x1 x2 x3 x4
raw data
Row (samples)
index
Columns (features)
index
Panda Indexed Matrix
Pandas – Series and Data Frames
• Pandas Indexed Arrays are referred to as Series (1D) and
Data Frames (2D).
• Series is a 1D labeled (indexed) array and can hold any data type,
and mix of data types.
Series Raw data Column Index Labels
s = pd.Series( data, index=[ ‘x1’, ‘x2’, ‘x3’, ‘x4’ ] )
• Data Frame is a 2D labeled (indexed) matrix and can hold any
data type, and mix of data types.
Data Frame Row Index Labels Column Index Labels
df = pd.DataFrame( data, index=[‘one’, ‘two’], columns=[ ‘x1’, ‘x2’, ‘x3’, ‘x4’ ] )
Pandas – Selecting
• Selecting One Column
x1 = df[ ‘x1’ ]
Selects column labeled x1 for all rows
1
4
8
• Selecting Multiple Columns
Selects columns labeled x1 and x3 for all rows
x1 = df[ [ ‘x1’, ‘x3’ ] ]
1 3
4 6
8 10
x1 = df.ix[ :, ‘x1’:’x3’ ]
Selects columns labeled x1 through x3 for all rows
1 2 3
4 5 6
8 9 10
Note: df[‘x1’:’x3’ ] this python syntax does not work!
rows (all) columns
Slicing function
And many more functions: merge, concat, stack, …
Libraries - Matplotlib
• A popular library for plotting and visualizing data in Python
Keyword to import a library Keyword to refer to library by an alias (shortcut) name
import matplotlib.pyplot as plt
Used for:
• Plots
• Histograms
• Bar Charts
• Scatter Plots
• etc
matplotlib.org: Matplotlib is a Python 2D plotting library which produces publication quality
figures in a variety of hardcopy formats and interactive environments across platforms.
Matplotlib - Plot
• The function plot plots a 2D graph.
plt.plot( x, y )
Function to plot
X values to plot
Y values to plot
• Example:
plt.plot( [ 1, 2, 3 ], [ 4, 6, 8 ] ) # Draws plot in the background
plt.show() # Displays the plot
X Y
1
8
6
4
2
2 3
Matplotlib – Plot Labels
• Add Labels for X and Y Axis and Plot Title (caption)
plt.plot( [ 1, 2, 3 ], [ 4, 6, 8 ] )
plt.xlabel( “X Numbers” )
plt.ylabel( “Y Numbers” )
plt.title( “My Plot of X and Y”)
plt.show()
# Label on the X-axis
# Label on the Y-axis
# Title for the Plot
1 2 3
X Numbers
My Plot of X and Y
8
6
4
2
Y
Numbers
Matplotlib – Multiple Plots and Legend
• You can add multiple plots in a Graph
plt.plot( [ 1, 2, 3 ], [ 4, 6, 8 ], label=‘ 1st Line’ )
plt.plot( [ 1, 2, 3 ], [ 2, 4, 6 ], label=‘2nd Line’ )
plt.xlabel( “X Numbers” )
plt.ylabel( “Y Numbers” )
plt.title( “My Plot of X and Y”)
# Plot for 1st Line
# Plot for 2nd Line
plt.legend() # Show Legend for the plots
plt.show()
1
4
2
6
8
2 3
X Numbers
Y
Numbers
My Plot of X and Y
---- 1st Line
---- 2nd Line
Matplotlib – Bar Chart
• The function bar plots a bar graph.
plt.plot( [ 1, 2, 3 ], [ 4, 6, 8 ] ) # Plot for 1st Line
plt.bar() # Draw a bar chart
plt.show()
1
8
6
4
2
2 3
And many more functions: hist, scatter, …

More Related Content

PDF
Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...
PPTX
python-numwpyandpandas-170922144956.pptx
PPTX
Python - Numpy/Pandas/Matplot Machine Learning Libraries
PDF
Numpy.pdf
PPTX
Chapter 5-Numpy-Pandas.pptx python programming
PPTX
UNIT-03_Numpy (1) python yeksodbbsisbsjsjsh
PPTX
getting started with numpy and pandas.pptx
PPTX
Unit 3_Numpy_VP.pptx
Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...
python-numwpyandpandas-170922144956.pptx
Python - Numpy/Pandas/Matplot Machine Learning Libraries
Numpy.pdf
Chapter 5-Numpy-Pandas.pptx python programming
UNIT-03_Numpy (1) python yeksodbbsisbsjsjsh
getting started with numpy and pandas.pptx
Unit 3_Numpy_VP.pptx

Similar to python-numpyandpandas-170922144956 (1).pptx (20)

PPTX
Unit 3_Numpy_Vsp.pptx
PPTX
NumPy.pptx
PPTX
Introduction to a Python Libraries and python frameworks
PPT
Python crash course libraries numpy-1, panda.ppt
PDF
Python Interview Questions PDF By ScholarHat
PPTX
numpydocococ34554367827839271966666.pptx
PDF
PPTX
Comparing EDA with classical and Bayesian analysis.pptx
PPTX
object oriented programing in python and pip
PPTX
Language R
PPTX
Unit 3_Numpy_VP.pptx
PDF
R programming & Machine Learning
PPT
Introduction to Numpy Foundation Study GuideStudyGuide
PPTX
Lecture 2 _Foundions foundions NumPyI.pptx
PPTX
NUMPY-2.pptx
PPTX
Aggregate.pptx
PPTX
data analytics and visualization CO4_18_Data Types for Plotting.pptx
DOCX
Data Manipulation with Numpy and Pandas in PythonStarting with N
PPTX
numpy code and examples with attributes.pptx
PPTX
Lecture_3.5-Array_Type Conversion_Math Class.pptx
Unit 3_Numpy_Vsp.pptx
NumPy.pptx
Introduction to a Python Libraries and python frameworks
Python crash course libraries numpy-1, panda.ppt
Python Interview Questions PDF By ScholarHat
numpydocococ34554367827839271966666.pptx
Comparing EDA with classical and Bayesian analysis.pptx
object oriented programing in python and pip
Language R
Unit 3_Numpy_VP.pptx
R programming & Machine Learning
Introduction to Numpy Foundation Study GuideStudyGuide
Lecture 2 _Foundions foundions NumPyI.pptx
NUMPY-2.pptx
Aggregate.pptx
data analytics and visualization CO4_18_Data Types for Plotting.pptx
Data Manipulation with Numpy and Pandas in PythonStarting with N
numpy code and examples with attributes.pptx
Lecture_3.5-Array_Type Conversion_Math Class.pptx
Ad

Recently uploaded (20)

PPTX
2025-08-10 Joseph 02 (shared slides).pptx
PPTX
AcademyNaturalLanguageProcessing-EN-ILT-M02-Introduction.pptx
PPT
First Aid Training Presentation Slides.ppt
PPTX
Project and change Managment: short video sequences for IBA
PPTX
Lesson-7-Gas. -Exchange_074636.pptx
PPTX
Introduction-to-Food-Packaging-and-packaging -materials.pptx
DOCX
"Project Management: Ultimate Guide to Tools, Techniques, and Strategies (2025)"
DOC
LSTM毕业证学历认证,利物浦大学毕业证学历认证怎么认证
PPTX
BIOLOGY TISSUE PPT CLASS 9 PROJECT PUBLIC
PPTX
FINAL TEST 3C_OCTAVIA RAMADHANI SANTOSO-1.pptx
PPTX
MERISTEMATIC TISSUES (MERISTEMS) PPT PUBLIC
PDF
COLEAD A2F approach and Theory of Change
PDF
Microsoft-365-Administrator-s-Guide_.pdf
PPTX
Hydrogel Based delivery Cancer Treatment
PPTX
Effective_Handling_Information_Presentation.pptx
PPTX
Human Mind & its character Characteristics
PDF
natwest.pdf company description and business model
PPTX
chapter8-180915055454bycuufucdghrwtrt.pptx
PPT
The Effect of Human Resource Management Practice on Organizational Performanc...
PPTX
Anesthesia and it's stage with mnemonic and images
2025-08-10 Joseph 02 (shared slides).pptx
AcademyNaturalLanguageProcessing-EN-ILT-M02-Introduction.pptx
First Aid Training Presentation Slides.ppt
Project and change Managment: short video sequences for IBA
Lesson-7-Gas. -Exchange_074636.pptx
Introduction-to-Food-Packaging-and-packaging -materials.pptx
"Project Management: Ultimate Guide to Tools, Techniques, and Strategies (2025)"
LSTM毕业证学历认证,利物浦大学毕业证学历认证怎么认证
BIOLOGY TISSUE PPT CLASS 9 PROJECT PUBLIC
FINAL TEST 3C_OCTAVIA RAMADHANI SANTOSO-1.pptx
MERISTEMATIC TISSUES (MERISTEMS) PPT PUBLIC
COLEAD A2F approach and Theory of Change
Microsoft-365-Administrator-s-Guide_.pdf
Hydrogel Based delivery Cancer Treatment
Effective_Handling_Information_Presentation.pptx
Human Mind & its character Characteristics
natwest.pdf company description and business model
chapter8-180915055454bycuufucdghrwtrt.pptx
The Effect of Human Resource Management Practice on Organizational Performanc...
Anesthesia and it's stage with mnemonic and images
Ad

python-numpyandpandas-170922144956 (1).pptx

  • 1. Python Numpy/Pandas Libraries Machine Learning Portland Data Science Group Created by Andrew Ferlitsch Community Outreach Officer July, 2017
  • 2. Libraries - Numpy • A popular math library in Python for Machine Learning is ‘numpy’. import numpy as np Keyword to import a library Keyword to refer to library by an alias (shortcut) name Numpy.org : NumPy is the fundamental package for scientific computing with Python. • a powerful N-dimensional array object • sophisticated (broadcasting) functions • tools for integrating C/C++ and Fortran code • useful linear algebra, Fourier transform, and random number capabilities
  • 3. Libraries - Numpy http://www.physics.nyu.edu/pine/pymanual/html/chap3/chap3_arrays.html The most import data structure for scientific computing in Python is the NumPy array. NumPy arrays are used to store lists of numerical data and to represent vectors, matrices, and even tensors. NumPy arrays are designed to handle large data sets efficiently and with a minimum of fuss. The NumPy library has a large set of routines for creating, manipulating, and transforming NumPy arrays. Core Python has an array data structure, but it’s not nearly as versatile, efficient, or useful as the NumPy array.
  • 4. Numpy – Multidimensional Arrays • Numpy’s main object is a multi-dimensional array. • Creating a Numpy Array as a Vector: Numpy function to create a numpy array Value is: array( [ 1, 2, 3 ] ) data = np.array( [ 1, 2, 3 ] ) • Creating a Numpy Array as a Matrix: data = np.array( [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] ) OuterDimension Inner Dimension (rows) Value is: array( [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] )
  • 5. Numpy – Multidimensional Arrays • Creating an array of Zeros: Numpy function to create an array of zeros Value is: array( [ 0, 0, 0 ], [ 0, 0, 0 ] ) data = np.zeros( ( 2, 3 ), dtype=np.int ) rows columns • Creating an array of Ones: Value is: array( [ 1, 1, 1 ], [ 1, 1, 1 ] ) data type (default is float) Numpy function to create an array of ones data = np.ones( (2, 3), dtype=np.int ) And many more functions: size, ndim, reshape, arange, …
  • 6. Libraries - Pandas • A popular library for importing and managing datasets in Python for Machine Learning is ‘pandas’. Keyword to import a library Keyword to refer to library by an alias (shortcut) name import pandas as pd Used for: • Data Analysis • Data Manipulation • Data Visualization PyData.org : high-performance, easy-to-use data structures and data analysis tools for the Python programming language.
  • 7. Pandas – Indexed Arrays • Pandas are used to build indexed arrays (1D) and matrices (2D), where columns and rows are labeled (named) and can be accessed via the labels (names). 1 2 3 4 4 5 6 7 8 9 10 11 1 2 3 4 4 5 6 7 8 9 10 11 one two three x1 x2 x3 x4 raw data Row (samples) index Columns (features) index Panda Indexed Matrix
  • 8. Pandas – Series and Data Frames • Pandas Indexed Arrays are referred to as Series (1D) and Data Frames (2D). • Series is a 1D labeled (indexed) array and can hold any data type, and mix of data types. Series Raw data Column Index Labels s = pd.Series( data, index=[ ‘x1’, ‘x2’, ‘x3’, ‘x4’ ] ) • Data Frame is a 2D labeled (indexed) matrix and can hold any data type, and mix of data types. Data Frame Row Index Labels Column Index Labels df = pd.DataFrame( data, index=[‘one’, ‘two’], columns=[ ‘x1’, ‘x2’, ‘x3’, ‘x4’ ] )
  • 9. Pandas – Selecting • Selecting One Column x1 = df[ ‘x1’ ] Selects column labeled x1 for all rows 1 4 8 • Selecting Multiple Columns Selects columns labeled x1 and x3 for all rows x1 = df[ [ ‘x1’, ‘x3’ ] ] 1 3 4 6 8 10 x1 = df.ix[ :, ‘x1’:’x3’ ] Selects columns labeled x1 through x3 for all rows 1 2 3 4 5 6 8 9 10 Note: df[‘x1’:’x3’ ] this python syntax does not work! rows (all) columns Slicing function And many more functions: merge, concat, stack, …
  • 10. Libraries - Matplotlib • A popular library for plotting and visualizing data in Python Keyword to import a library Keyword to refer to library by an alias (shortcut) name import matplotlib.pyplot as plt Used for: • Plots • Histograms • Bar Charts • Scatter Plots • etc matplotlib.org: Matplotlib is a Python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms.
  • 11. Matplotlib - Plot • The function plot plots a 2D graph. plt.plot( x, y ) Function to plot X values to plot Y values to plot • Example: plt.plot( [ 1, 2, 3 ], [ 4, 6, 8 ] ) # Draws plot in the background plt.show() # Displays the plot X Y 1 8 6 4 2 2 3
  • 12. Matplotlib – Plot Labels • Add Labels for X and Y Axis and Plot Title (caption) plt.plot( [ 1, 2, 3 ], [ 4, 6, 8 ] ) plt.xlabel( “X Numbers” ) plt.ylabel( “Y Numbers” ) plt.title( “My Plot of X and Y”) plt.show() # Label on the X-axis # Label on the Y-axis # Title for the Plot 1 2 3 X Numbers My Plot of X and Y 8 6 4 2 Y Numbers
  • 13. Matplotlib – Multiple Plots and Legend • You can add multiple plots in a Graph plt.plot( [ 1, 2, 3 ], [ 4, 6, 8 ], label=‘ 1st Line’ ) plt.plot( [ 1, 2, 3 ], [ 2, 4, 6 ], label=‘2nd Line’ ) plt.xlabel( “X Numbers” ) plt.ylabel( “Y Numbers” ) plt.title( “My Plot of X and Y”) # Plot for 1st Line # Plot for 2nd Line plt.legend() # Show Legend for the plots plt.show() 1 4 2 6 8 2 3 X Numbers Y Numbers My Plot of X and Y ---- 1st Line ---- 2nd Line
  • 14. Matplotlib – Bar Chart • The function bar plots a bar graph. plt.plot( [ 1, 2, 3 ], [ 4, 6, 8 ] ) # Plot for 1st Line plt.bar() # Draw a bar chart plt.show() 1 8 6 4 2 2 3 And many more functions: hist, scatter, …