SlideShare a Scribd company logo
1 of 14
Download to read offline
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
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.
http://www.physics.nyu.edu/pine/pymanual/html/chap3/chap3_arrays.html
Numpy – Multidimensional Arrays
• Numpy’s main object is a multi-dimensional array.
• Creating a Numpy Array as a Vector:
data = np.array( [ 1, 2, 3 ] )
Numpy function to create a numpy array
Value is: array( [ 1, 2, 3 ] )
• Creating a Numpy Array as a Matrix:
data = np.array( [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] )
Outer Dimension Inner Dimension (rows)
Value is: array( [ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ] )
Numpy – Multidimensional Arrays
• Creating an array of Zeros:
data = np.zeros( ( 2, 3 ), dtype=np.int )
Numpy function to create an array of zeros
Value is: array( [ 0, 0, 0 ],
[ 0, 0, 0 ] )
• Creating an array of Ones:
data = np.ones( (2, 3), dtype=np.int )
rows
columns
data type (default is float)
Numpy function to create an array of ones
Value is: array( [ 1, 1, 1 ],
[ 1, 1, 1 ] )
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’.
import pandas as pd
Keyword to import a library Keyword to refer to library by an alias (shortcut) name
PyData.org : high-performance, easy-to-use data structures and data analysis tools for the
Python programming language.
Used for:
• Data Analysis
• Data Manipulation
• Data Visualization
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.
s = pd.Series( data, index=[ ‘x1’, ‘x2’, ‘x3’, ‘x4’ ] )
Series Raw data Column Index Labels
• Data Frame is a 2D labeled (indexed) matrix and can hold any
data type, and mix of data types.
df = pd.DataFrame( data, index=[‘one’, ‘two’], columns=[ ‘x1’, ‘x2’, ‘x3’, ‘x4’ ] )
Data Frame Row Index Labels Column Index Labels
Pandas – Selecting
• Selecting One Column
x1 = df[ ‘x1’ ]
Selects column labeled x1 for all rows
1
4
8
• Selecting Multiple Columns
x1 = df[ [ ‘x1’, ‘x3’ ] ]
Selects columns labeled x1 and x3 for all rows
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
import matplotlib.pyplot as plt
Keyword to import a library Keyword to refer to library by an alias (shortcut) name
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.
Used for:
• Plots
• Histograms
• Bar Charts
• Scatter Plots
• etc
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
2
4
6
8
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” ) # Label on the X-axis
plt.ylabel( “Y Numbers” ) # Label on the Y-axis
plt.title( “My Plot of X and Y”) # Title for the Plot
plt.show()
1
2
4
6
8
2 3
X Numbers
Y
Numbers
My Plot of X and Y
Matplotlib – Multiple Plots and Legend
• You can add multiple plots in a Graph
plt.plot( [ 1, 2, 3 ], [ 4, 6, 8 ], label=‘ 1st Line’ ) # Plot for 1st Line
plt.plot( [ 1, 2, 3 ], [ 2, 4, 6 ], label=‘2nd Line’ ) # Plot for 2nd Line
plt.xlabel( “X Numbers” )
plt.ylabel( “Y Numbers” )
plt.title( “My Plot of X and Y”)
plt.legend() # Show Legend for the plots
plt.show()
1
2
4
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
2
4
6
8
2 3
And many more functions: hist, scatter, …

More Related Content

Similar to Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkualitas publikasi dalam berbagai format cetak dan lingkungan interaktif di berbagai platform.

R programming & Machine Learning
R programming & Machine LearningR programming & Machine Learning
R programming & Machine LearningAmanBhalla14
 
Comparing EDA with classical and Bayesian analysis.pptx
Comparing EDA with classical and Bayesian analysis.pptxComparing EDA with classical and Bayesian analysis.pptx
Comparing EDA with classical and Bayesian analysis.pptxPremaGanesh1
 
Lecture_3.5-Array_Type Conversion_Math Class.pptx
Lecture_3.5-Array_Type Conversion_Math Class.pptxLecture_3.5-Array_Type Conversion_Math Class.pptx
Lecture_3.5-Array_Type Conversion_Math Class.pptxShahinAhmed49
 
Data Manipulation with Numpy and Pandas in PythonStarting with N
Data Manipulation with Numpy and Pandas in PythonStarting with NData Manipulation with Numpy and Pandas in PythonStarting with N
Data Manipulation with Numpy and Pandas in PythonStarting with NOllieShoresna
 
Advanced Data Analytics with R Programming.ppt
Advanced Data Analytics with R Programming.pptAdvanced Data Analytics with R Programming.ppt
Advanced Data Analytics with R Programming.pptAnshika865276
 
Pa1 session 3_slides
Pa1 session 3_slidesPa1 session 3_slides
Pa1 session 3_slidesaiclub_slides
 
Introduction to R.pptx
Introduction to R.pptxIntroduction to R.pptx
Introduction to R.pptxkarthikks82
 
Standardizing on a single N-dimensional array API for Python
Standardizing on a single N-dimensional array API for PythonStandardizing on a single N-dimensional array API for Python
Standardizing on a single N-dimensional array API for PythonRalf Gommers
 
Python-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptxPython-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptxParveenShaik21
 

Similar to Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkualitas publikasi dalam berbagai format cetak dan lingkungan interaktif di berbagai platform. (20)

numpy.pdf
numpy.pdfnumpy.pdf
numpy.pdf
 
NUMPY-2.pptx
NUMPY-2.pptxNUMPY-2.pptx
NUMPY-2.pptx
 
R programming & Machine Learning
R programming & Machine LearningR programming & Machine Learning
R programming & Machine Learning
 
Comparing EDA with classical and Bayesian analysis.pptx
Comparing EDA with classical and Bayesian analysis.pptxComparing EDA with classical and Bayesian analysis.pptx
Comparing EDA with classical and Bayesian analysis.pptx
 
NUMPY
NUMPY NUMPY
NUMPY
 
Aggregate.pptx
Aggregate.pptxAggregate.pptx
Aggregate.pptx
 
Language R
Language RLanguage R
Language R
 
Lecture_3.5-Array_Type Conversion_Math Class.pptx
Lecture_3.5-Array_Type Conversion_Math Class.pptxLecture_3.5-Array_Type Conversion_Math Class.pptx
Lecture_3.5-Array_Type Conversion_Math Class.pptx
 
NumPy.pptx
NumPy.pptxNumPy.pptx
NumPy.pptx
 
Numpy ndarrays.pdf
Numpy ndarrays.pdfNumpy ndarrays.pdf
Numpy ndarrays.pdf
 
Data Manipulation with Numpy and Pandas in PythonStarting with N
Data Manipulation with Numpy and Pandas in PythonStarting with NData Manipulation with Numpy and Pandas in PythonStarting with N
Data Manipulation with Numpy and Pandas in PythonStarting with N
 
Advanced Data Analytics with R Programming.ppt
Advanced Data Analytics with R Programming.pptAdvanced Data Analytics with R Programming.ppt
Advanced Data Analytics with R Programming.ppt
 
Lecture 9.pptx
Lecture 9.pptxLecture 9.pptx
Lecture 9.pptx
 
R Introduction
R IntroductionR Introduction
R Introduction
 
Pa1 session 3_slides
Pa1 session 3_slidesPa1 session 3_slides
Pa1 session 3_slides
 
R training2
R training2R training2
R training2
 
Python for data analysis
Python for data analysisPython for data analysis
Python for data analysis
 
Introduction to R.pptx
Introduction to R.pptxIntroduction to R.pptx
Introduction to R.pptx
 
Standardizing on a single N-dimensional array API for Python
Standardizing on a single N-dimensional array API for PythonStandardizing on a single N-dimensional array API for Python
Standardizing on a single N-dimensional array API for Python
 
Python-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptxPython-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptx
 

Recently uploaded

Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfLars Albertsson
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130Suhani Kapoor
 
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一fhwihughh
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)jennyeacort
 
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...ThinkInnovation
 
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfKantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfSocial Samosa
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts ServiceSapana Sha
 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Sapana Sha
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdfHuman37
 
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024thyngster
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxEmmanuel Dauda
 
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort servicejennyeacort
 
9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home ServiceSapana Sha
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Jack DiGiovanna
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationshipsccctableauusergroup
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptSonatrach
 
Call Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceCall Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceSapana Sha
 

Recently uploaded (20)

Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdf
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
 
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
 
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
 
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
 
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfKantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
 
E-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptxE-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptx
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts Service
 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf
 
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptx
 
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
 
9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
 
Call Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceCall Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts Service
 

Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkualitas publikasi dalam berbagai format cetak dan lingkungan interaktif di berbagai platform.

  • 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 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. http://www.physics.nyu.edu/pine/pymanual/html/chap3/chap3_arrays.html
  • 4. Numpy – Multidimensional Arrays • Numpy’s main object is a multi-dimensional array. • Creating a Numpy Array as a Vector: data = np.array( [ 1, 2, 3 ] ) Numpy function to create a numpy array Value is: array( [ 1, 2, 3 ] ) • Creating a Numpy Array as a Matrix: data = np.array( [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] ) Outer Dimension Inner Dimension (rows) Value is: array( [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] )
  • 5. Numpy – Multidimensional Arrays • Creating an array of Zeros: data = np.zeros( ( 2, 3 ), dtype=np.int ) Numpy function to create an array of zeros Value is: array( [ 0, 0, 0 ], [ 0, 0, 0 ] ) • Creating an array of Ones: data = np.ones( (2, 3), dtype=np.int ) rows columns data type (default is float) Numpy function to create an array of ones Value is: array( [ 1, 1, 1 ], [ 1, 1, 1 ] ) 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’. import pandas as pd Keyword to import a library Keyword to refer to library by an alias (shortcut) name PyData.org : high-performance, easy-to-use data structures and data analysis tools for the Python programming language. Used for: • Data Analysis • Data Manipulation • Data Visualization
  • 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. s = pd.Series( data, index=[ ‘x1’, ‘x2’, ‘x3’, ‘x4’ ] ) Series Raw data Column Index Labels • Data Frame is a 2D labeled (indexed) matrix and can hold any data type, and mix of data types. df = pd.DataFrame( data, index=[‘one’, ‘two’], columns=[ ‘x1’, ‘x2’, ‘x3’, ‘x4’ ] ) Data Frame Row Index Labels Column Index Labels
  • 9. Pandas – Selecting • Selecting One Column x1 = df[ ‘x1’ ] Selects column labeled x1 for all rows 1 4 8 • Selecting Multiple Columns x1 = df[ [ ‘x1’, ‘x3’ ] ] Selects columns labeled x1 and x3 for all rows 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 import matplotlib.pyplot as plt Keyword to import a library Keyword to refer to library by an alias (shortcut) name 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. Used for: • Plots • Histograms • Bar Charts • Scatter Plots • etc
  • 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 2 4 6 8 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” ) # Label on the X-axis plt.ylabel( “Y Numbers” ) # Label on the Y-axis plt.title( “My Plot of X and Y”) # Title for the Plot plt.show() 1 2 4 6 8 2 3 X Numbers Y Numbers My Plot of X and Y
  • 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’ ) # Plot for 1st Line plt.plot( [ 1, 2, 3 ], [ 2, 4, 6 ], label=‘2nd Line’ ) # Plot for 2nd Line plt.xlabel( “X Numbers” ) plt.ylabel( “Y Numbers” ) plt.title( “My Plot of X and Y”) plt.legend() # Show Legend for the plots plt.show() 1 2 4 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 2 4 6 8 2 3 And many more functions: hist, scatter, …