SlideShare a Scribd company logo
1 of 17
Slide 1
Objective of the class
• What is numpy?
• numpy performance test
• Introduction to numpy arrays
• Introduction to numpy function
• Dealing with Flat files using numpy
• Mathematical functions
• Statisticals function
• Operations with arrays
Introduction to Numpy
Slide 2
NumPy
NumPy is an extension to the Python programming language, adding support for
large, multi-dimensional arrays and matrices, along with a large library of high-level
mathematical functions to operate on these arrays
To install NumPy run:
python setup.py install
To perform an in-place build that can be run from the source folder run:
python setup.py build_ext --inplace
The NumPy build system uses distutils and numpy.distutils. setuptools is only
used when building via pip or with python setupegg.py.
Slide 3
Official Website: http://www.numpy.org
• NumPy is licensed under the BSD license, enabling reuse with few restrictions.
• NumPy replaces Numeric and Numarray
• Numpy was initially developed by Travis Oliphant
• There are 225+ Contributors to the project (github.com)
• NumPy 1.0 released October, 2006
• Numpy 1.14.0 is the lastest version of numpy
• There are more than 200K downloads/month from PyPI
NumPy
Slide 4
NumPy performance Test
Slide 5
Getting Started with Numpy
>>> # Importing Numpy module
>>> import numpy
>>> import numpy as np
IPython has a ‘pylab’ mode where it imports all of NumPy, Matplotlib,
and SciPy into the namespace for you as a convenience. It also enables
threading for showing plots
Slide 6
Getting Started with Numpy
• Arrays are the central feature of NumPy.
• Arrays in Python are similar to lists in Python, the only difference being the array
elements should be of the same type
import numpy as np
a = np.array([1,2,3], float) # Accept two arguments list and type
print a # Return array([ 1., 2., 3.])
print type(a) # Return <type 'numpy.ndarray'>
print a[2] # 3.0
print a.dtype # Print the element type
print a.itemsize # print bytes per element
print a.shape # print the shape of an array
print a.size # print the size of an array
Slide 7
a.nbytes # return the total bytes used by an array
a.ndim # provide the dimension of an array
a[0] = 10.5 # Modify array first index important decimal will
come if the array is float type else it will be hold only 10
a.fill(20) # Fill all the values by 20
a[1:3] # Slice the array
a[-2:] # Last two elements of an array
Getting Started with Numpy
Slide 8
Airthmatic Operation with numpy
Slide 9
import numpy as np
a = np.array([[1,2,3], [4,5,6]], int) # Accept two arguments list and type
>>> a.shape # Return (2, 3)
>>> a.ndim # return 2
>>> a[0][0] # Return 1
>>> a[0,0] # Return 1
>>> a[1,2] # Return 6
>>> a[1:] # Return array([[4, 5, 6]])
>>> a[1,:] # Return array([4, 5, 6])
>>> a[:2] # Return array([[1, 2, 3],[4, 5, 6]])
>>> a[:,2] # Return array([3, 6])
>>> a[:,1] # Return array([2, 5])
>>> a[:,0] # Return array([1, 4])
Multi-Dimensional Arrays
Slide 10
import numpy as np
a = np.array([[1,2,3], [4,5,6]], int) # Accept two arguments list and type
>>> b = a.reshape(3,2) # Return the new shape of array
>>> b.shape # Return (3,2)
>>> len(a) # Return length of a
>>> 2 in a # Check if 2 is available in a
>>> b.tolist() # Return [[1, 2], [3, 4], [5, 6]]
>>> list(b) # Return [array([1, 2]), array([3, 4]), array([5, 6])]
>>> c = b
>>> b[0][0] = 10
>>> c # Return array([[10, 2], [ 3, 4], [ 5, 6]])
Reshaping array
Slide 11
Slices Are References
>>> a = array((0,1,2,3,4))
# create a slice containing only the
# last element of a
>>> b = a[2:4]
>>> b
array([2, 3])
>>> b[0] = 10
# changing b changed a!
>>> a
array([ 0, 1, 10, 3, 4])
Slide 12
arange function and slicing
>>> a = np.arange(1,80, 2)
array([ 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33,
35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67,
69, 71, 73, 75, 77, 79])
>>> a[[1,5,10]] # Return values at index 1,5 and 10
>>> myIndexes = [4,5,6]
>>> a[myIndexes] # Return values at indexes 4,5,6..
>>> mask = a % 5 == 0 # Return boolean value at those indexes
>>> a[mask]
Out[46]: array([ 5, 15, 25, 35, 45, 55, 65, 75])
Slide 13
Where function
>>> where (a % 5 == 0)
# Returns - (array([ 2, 7, 12, 17, 22, 27, 32, 37], dtype=int64),)
>>> loc = where(a % 5 == 0)
>>> print a[loc]
[ 5 15 25 35 45 55 65 75]
Slide 14
Flatten Arrays
# Create a 2D array
>>> a = array([[0,1],
[2,3]])
# Flatten out elements to 1D
>>> b = a.flatten()
>>> b
array([0,1,2,3])
# Changing b does not change a
>>> b[0] = 10
>>> b
array([10,1,2,3])
>>> a
array([[0, 1],
[2, 3]])
Slide 15
>>> a = array([[0,1,2],
... [3,4,5]])
>>> a.shape
(2,3)
# Transpose swaps the order # of axes. For 2-D this # swaps rows and columns.
>>> a.transpose()
array([[0, 3], [1, 4],[2, 5]])
# The .T attribute is # equivalent to transpose().
>>> a.T
array([[0, 3],
[1, 4],
[2, 5]])
Transpose Arrays
Slide 16
csv = np.loadtxt(r'A:UPDATE PythonModule 11Programsconstituents-
financials.csv', skiprows=1, dtype=str, delimiter=",", usecols = (3,4,5))
Arrays from/to ASCII files
Slide 17
Other format supported by other similar package

More Related Content

What's hot

What's hot (20)

Data Analysis in Python-NumPy
Data Analysis in Python-NumPyData Analysis in Python-NumPy
Data Analysis in Python-NumPy
 
Arrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaArrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | Edureka
 
List in Python
List in PythonList in Python
List in Python
 
List,tuple,dictionary
List,tuple,dictionaryList,tuple,dictionary
List,tuple,dictionary
 
Introduction to numpy
Introduction to numpyIntroduction to numpy
Introduction to numpy
 
Introduction to pandas
Introduction to pandasIntroduction to pandas
Introduction to pandas
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
 
Pandas csv
Pandas csvPandas csv
Pandas csv
 
Pandas
PandasPandas
Pandas
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
NumPy
NumPyNumPy
NumPy
 
Data Analysis with Python Pandas
Data Analysis with Python PandasData Analysis with Python Pandas
Data Analysis with Python Pandas
 
Python dictionary
Python   dictionaryPython   dictionary
Python dictionary
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in python
 
Introduction to matplotlib
Introduction to matplotlibIntroduction to matplotlib
Introduction to matplotlib
 
Data types in python
Data types in pythonData types in python
Data types in python
 
Linked list
Linked listLinked list
Linked list
 
NumPy/SciPy Statistics
NumPy/SciPy StatisticsNumPy/SciPy Statistics
NumPy/SciPy Statistics
 
NumPy.pptx
NumPy.pptxNumPy.pptx
NumPy.pptx
 

Similar to Introduction to numpy Session 1

Effective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPyEffective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPyKimikazu Kato
 
Numerical tour in the Python eco-system: Python, NumPy, scikit-learn
Numerical tour in the Python eco-system: Python, NumPy, scikit-learnNumerical tour in the Python eco-system: Python, NumPy, scikit-learn
Numerical tour in the Python eco-system: Python, NumPy, scikit-learnArnaud Joly
 
Python_cheatsheet_numpy.pdf
Python_cheatsheet_numpy.pdfPython_cheatsheet_numpy.pdf
Python_cheatsheet_numpy.pdfAnonymousUser67
 
Numpy python cheat_sheet
Numpy python cheat_sheetNumpy python cheat_sheet
Numpy python cheat_sheetZahid Hasan
 
L 5 Numpy final ppt kirti.pptx
L 5 Numpy final ppt kirti.pptxL 5 Numpy final ppt kirti.pptx
L 5 Numpy final ppt kirti.pptxKirti Verma
 
NUMPY [Autosaved] .pptx
NUMPY [Autosaved]                    .pptxNUMPY [Autosaved]                    .pptx
NUMPY [Autosaved] .pptxcoolmanbalu123
 
Python Training v2
Python Training v2Python Training v2
Python Training v2ibaydan
 
CE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdfCE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdfUmarMustafa13
 
Essential numpy before you start your Machine Learning journey in python.pdf
Essential numpy before you start your Machine Learning journey in python.pdfEssential numpy before you start your Machine Learning journey in python.pdf
Essential numpy before you start your Machine Learning journey in python.pdfSmrati Kumar Katiyar
 
arrays-120712074248-phpapp01
arrays-120712074248-phpapp01arrays-120712074248-phpapp01
arrays-120712074248-phpapp01Abdul Samee
 
Introduction of MatLab
Introduction of MatLab Introduction of MatLab
Introduction of MatLab Imran Nawaz
 
Numpy tutorial(final) 20160303
Numpy tutorial(final) 20160303Numpy tutorial(final) 20160303
Numpy tutorial(final) 20160303Namgee Lee
 
Using-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptxUsing-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptxUadAccount
 

Similar to Introduction to numpy Session 1 (20)

Effective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPyEffective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPy
 
NumPy.pptx
NumPy.pptxNumPy.pptx
NumPy.pptx
 
Numerical tour in the Python eco-system: Python, NumPy, scikit-learn
Numerical tour in the Python eco-system: Python, NumPy, scikit-learnNumerical tour in the Python eco-system: Python, NumPy, scikit-learn
Numerical tour in the Python eco-system: Python, NumPy, scikit-learn
 
Slides
SlidesSlides
Slides
 
Python_cheatsheet_numpy.pdf
Python_cheatsheet_numpy.pdfPython_cheatsheet_numpy.pdf
Python_cheatsheet_numpy.pdf
 
Numpy python cheat_sheet
Numpy python cheat_sheetNumpy python cheat_sheet
Numpy python cheat_sheet
 
Numpy python cheat_sheet
Numpy python cheat_sheetNumpy python cheat_sheet
Numpy python cheat_sheet
 
L 5 Numpy final ppt kirti.pptx
L 5 Numpy final ppt kirti.pptxL 5 Numpy final ppt kirti.pptx
L 5 Numpy final ppt kirti.pptx
 
NUMPY [Autosaved] .pptx
NUMPY [Autosaved]                    .pptxNUMPY [Autosaved]                    .pptx
NUMPY [Autosaved] .pptx
 
Python Training v2
Python Training v2Python Training v2
Python Training v2
 
CE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdfCE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdf
 
14078956.ppt
14078956.ppt14078956.ppt
14078956.ppt
 
Essential numpy before you start your Machine Learning journey in python.pdf
Essential numpy before you start your Machine Learning journey in python.pdfEssential numpy before you start your Machine Learning journey in python.pdf
Essential numpy before you start your Machine Learning journey in python.pdf
 
arrays-120712074248-phpapp01
arrays-120712074248-phpapp01arrays-120712074248-phpapp01
arrays-120712074248-phpapp01
 
Numpy - Array.pdf
Numpy - Array.pdfNumpy - Array.pdf
Numpy - Array.pdf
 
07. Arrays
07. Arrays07. Arrays
07. Arrays
 
Introduction of MatLab
Introduction of MatLab Introduction of MatLab
Introduction of MatLab
 
Numpy tutorial(final) 20160303
Numpy tutorial(final) 20160303Numpy tutorial(final) 20160303
Numpy tutorial(final) 20160303
 
Python programming : Arrays
Python programming : ArraysPython programming : Arrays
Python programming : Arrays
 
Using-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptxUsing-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptx
 

Recently uploaded

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
 
While-For-loop in python used in college
While-For-loop in python used in collegeWhile-For-loop in python used in college
While-For-loop in python used in collegessuser7a7cd61
 
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝DelhiRS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhijennyeacort
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFAAndrei Kaleshka
 
Generative AI for Social Good at Open Data Science East 2024
Generative AI for Social Good at Open Data Science East 2024Generative AI for Social Good at Open Data Science East 2024
Generative AI for Social Good at Open Data Science East 2024Colleen Farrelly
 
Top 5 Best Data Analytics Courses In Queens
Top 5 Best Data Analytics Courses In QueensTop 5 Best Data Analytics Courses In Queens
Top 5 Best Data Analytics Courses In Queensdataanalyticsqueen03
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...Florian Roscheck
 
Predictive Analysis for Loan Default Presentation : Data Analysis Project PPT
Predictive Analysis for Loan Default  Presentation : Data Analysis Project PPTPredictive Analysis for Loan Default  Presentation : Data Analysis Project PPT
Predictive Analysis for Loan Default Presentation : Data Analysis Project PPTBoston Institute of Analytics
 
Heart Disease Classification Report: A Data Analysis Project
Heart Disease Classification Report: A Data Analysis ProjectHeart Disease Classification Report: A Data Analysis Project
Heart Disease Classification Report: A Data Analysis ProjectBoston Institute of Analytics
 
Identifying Appropriate Test Statistics Involving Population Mean
Identifying Appropriate Test Statistics Involving Population MeanIdentifying Appropriate Test Statistics Involving Population Mean
Identifying Appropriate Test Statistics Involving Population MeanMYRABACSAFRA2
 
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样vhwb25kk
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...dajasot375
 
NO1 Certified Black Magic Specialist Expert Amil baba in Lahore Islamabad Raw...
NO1 Certified Black Magic Specialist Expert Amil baba in Lahore Islamabad Raw...NO1 Certified Black Magic Specialist Expert Amil baba in Lahore Islamabad Raw...
NO1 Certified Black Magic Specialist Expert Amil baba in Lahore Islamabad Raw...Amil Baba Dawood bangali
 
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
 
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
 
2006_GasProcessing_HB (1).pdf HYDROCARBON PROCESSING
2006_GasProcessing_HB (1).pdf HYDROCARBON PROCESSING2006_GasProcessing_HB (1).pdf HYDROCARBON PROCESSING
2006_GasProcessing_HB (1).pdf HYDROCARBON PROCESSINGmarianagonzalez07
 
Semantic Shed - Squashing and Squeezing.pptx
Semantic Shed - Squashing and Squeezing.pptxSemantic Shed - Squashing and Squeezing.pptx
Semantic Shed - Squashing and Squeezing.pptxMike Bennett
 
Statistics, Data Analysis, and Decision Modeling, 5th edition by James R. Eva...
Statistics, Data Analysis, and Decision Modeling, 5th edition by James R. Eva...Statistics, Data Analysis, and Decision Modeling, 5th edition by James R. Eva...
Statistics, Data Analysis, and Decision Modeling, 5th edition by James R. Eva...ssuserf63bd7
 
Advanced Machine Learning for Business Professionals
Advanced Machine Learning for Business ProfessionalsAdvanced Machine Learning for Business Professionals
Advanced Machine Learning for Business ProfessionalsVICTOR MAESTRE RAMIREZ
 

Recently uploaded (20)

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
 
While-For-loop in python used in college
While-For-loop in python used in collegeWhile-For-loop in python used in college
While-For-loop in python used in college
 
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝DelhiRS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFA
 
Generative AI for Social Good at Open Data Science East 2024
Generative AI for Social Good at Open Data Science East 2024Generative AI for Social Good at Open Data Science East 2024
Generative AI for Social Good at Open Data Science East 2024
 
Top 5 Best Data Analytics Courses In Queens
Top 5 Best Data Analytics Courses In QueensTop 5 Best Data Analytics Courses In Queens
Top 5 Best Data Analytics Courses In Queens
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
 
Predictive Analysis for Loan Default Presentation : Data Analysis Project PPT
Predictive Analysis for Loan Default  Presentation : Data Analysis Project PPTPredictive Analysis for Loan Default  Presentation : Data Analysis Project PPT
Predictive Analysis for Loan Default Presentation : Data Analysis Project PPT
 
Heart Disease Classification Report: A Data Analysis Project
Heart Disease Classification Report: A Data Analysis ProjectHeart Disease Classification Report: A Data Analysis Project
Heart Disease Classification Report: A Data Analysis Project
 
Identifying Appropriate Test Statistics Involving Population Mean
Identifying Appropriate Test Statistics Involving Population MeanIdentifying Appropriate Test Statistics Involving Population Mean
Identifying Appropriate Test Statistics Involving Population Mean
 
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
 
Call Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort ServiceCall Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort Service
 
NO1 Certified Black Magic Specialist Expert Amil baba in Lahore Islamabad Raw...
NO1 Certified Black Magic Specialist Expert Amil baba in Lahore Islamabad Raw...NO1 Certified Black Magic Specialist Expert Amil baba in Lahore Islamabad Raw...
NO1 Certified Black Magic Specialist Expert Amil baba in Lahore Islamabad Raw...
 
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...
 
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)
 
2006_GasProcessing_HB (1).pdf HYDROCARBON PROCESSING
2006_GasProcessing_HB (1).pdf HYDROCARBON PROCESSING2006_GasProcessing_HB (1).pdf HYDROCARBON PROCESSING
2006_GasProcessing_HB (1).pdf HYDROCARBON PROCESSING
 
Semantic Shed - Squashing and Squeezing.pptx
Semantic Shed - Squashing and Squeezing.pptxSemantic Shed - Squashing and Squeezing.pptx
Semantic Shed - Squashing and Squeezing.pptx
 
Statistics, Data Analysis, and Decision Modeling, 5th edition by James R. Eva...
Statistics, Data Analysis, and Decision Modeling, 5th edition by James R. Eva...Statistics, Data Analysis, and Decision Modeling, 5th edition by James R. Eva...
Statistics, Data Analysis, and Decision Modeling, 5th edition by James R. Eva...
 
Advanced Machine Learning for Business Professionals
Advanced Machine Learning for Business ProfessionalsAdvanced Machine Learning for Business Professionals
Advanced Machine Learning for Business Professionals
 

Introduction to numpy Session 1

  • 1. Slide 1 Objective of the class • What is numpy? • numpy performance test • Introduction to numpy arrays • Introduction to numpy function • Dealing with Flat files using numpy • Mathematical functions • Statisticals function • Operations with arrays Introduction to Numpy
  • 2. Slide 2 NumPy NumPy is an extension to the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large library of high-level mathematical functions to operate on these arrays To install NumPy run: python setup.py install To perform an in-place build that can be run from the source folder run: python setup.py build_ext --inplace The NumPy build system uses distutils and numpy.distutils. setuptools is only used when building via pip or with python setupegg.py.
  • 3. Slide 3 Official Website: http://www.numpy.org • NumPy is licensed under the BSD license, enabling reuse with few restrictions. • NumPy replaces Numeric and Numarray • Numpy was initially developed by Travis Oliphant • There are 225+ Contributors to the project (github.com) • NumPy 1.0 released October, 2006 • Numpy 1.14.0 is the lastest version of numpy • There are more than 200K downloads/month from PyPI NumPy
  • 5. Slide 5 Getting Started with Numpy >>> # Importing Numpy module >>> import numpy >>> import numpy as np IPython has a ‘pylab’ mode where it imports all of NumPy, Matplotlib, and SciPy into the namespace for you as a convenience. It also enables threading for showing plots
  • 6. Slide 6 Getting Started with Numpy • Arrays are the central feature of NumPy. • Arrays in Python are similar to lists in Python, the only difference being the array elements should be of the same type import numpy as np a = np.array([1,2,3], float) # Accept two arguments list and type print a # Return array([ 1., 2., 3.]) print type(a) # Return <type 'numpy.ndarray'> print a[2] # 3.0 print a.dtype # Print the element type print a.itemsize # print bytes per element print a.shape # print the shape of an array print a.size # print the size of an array
  • 7. Slide 7 a.nbytes # return the total bytes used by an array a.ndim # provide the dimension of an array a[0] = 10.5 # Modify array first index important decimal will come if the array is float type else it will be hold only 10 a.fill(20) # Fill all the values by 20 a[1:3] # Slice the array a[-2:] # Last two elements of an array Getting Started with Numpy
  • 9. Slide 9 import numpy as np a = np.array([[1,2,3], [4,5,6]], int) # Accept two arguments list and type >>> a.shape # Return (2, 3) >>> a.ndim # return 2 >>> a[0][0] # Return 1 >>> a[0,0] # Return 1 >>> a[1,2] # Return 6 >>> a[1:] # Return array([[4, 5, 6]]) >>> a[1,:] # Return array([4, 5, 6]) >>> a[:2] # Return array([[1, 2, 3],[4, 5, 6]]) >>> a[:,2] # Return array([3, 6]) >>> a[:,1] # Return array([2, 5]) >>> a[:,0] # Return array([1, 4]) Multi-Dimensional Arrays
  • 10. Slide 10 import numpy as np a = np.array([[1,2,3], [4,5,6]], int) # Accept two arguments list and type >>> b = a.reshape(3,2) # Return the new shape of array >>> b.shape # Return (3,2) >>> len(a) # Return length of a >>> 2 in a # Check if 2 is available in a >>> b.tolist() # Return [[1, 2], [3, 4], [5, 6]] >>> list(b) # Return [array([1, 2]), array([3, 4]), array([5, 6])] >>> c = b >>> b[0][0] = 10 >>> c # Return array([[10, 2], [ 3, 4], [ 5, 6]]) Reshaping array
  • 11. Slide 11 Slices Are References >>> a = array((0,1,2,3,4)) # create a slice containing only the # last element of a >>> b = a[2:4] >>> b array([2, 3]) >>> b[0] = 10 # changing b changed a! >>> a array([ 0, 1, 10, 3, 4])
  • 12. Slide 12 arange function and slicing >>> a = np.arange(1,80, 2) array([ 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79]) >>> a[[1,5,10]] # Return values at index 1,5 and 10 >>> myIndexes = [4,5,6] >>> a[myIndexes] # Return values at indexes 4,5,6.. >>> mask = a % 5 == 0 # Return boolean value at those indexes >>> a[mask] Out[46]: array([ 5, 15, 25, 35, 45, 55, 65, 75])
  • 13. Slide 13 Where function >>> where (a % 5 == 0) # Returns - (array([ 2, 7, 12, 17, 22, 27, 32, 37], dtype=int64),) >>> loc = where(a % 5 == 0) >>> print a[loc] [ 5 15 25 35 45 55 65 75]
  • 14. Slide 14 Flatten Arrays # Create a 2D array >>> a = array([[0,1], [2,3]]) # Flatten out elements to 1D >>> b = a.flatten() >>> b array([0,1,2,3]) # Changing b does not change a >>> b[0] = 10 >>> b array([10,1,2,3]) >>> a array([[0, 1], [2, 3]])
  • 15. Slide 15 >>> a = array([[0,1,2], ... [3,4,5]]) >>> a.shape (2,3) # Transpose swaps the order # of axes. For 2-D this # swaps rows and columns. >>> a.transpose() array([[0, 3], [1, 4],[2, 5]]) # The .T attribute is # equivalent to transpose(). >>> a.T array([[0, 3], [1, 4], [2, 5]]) Transpose Arrays
  • 16. Slide 16 csv = np.loadtxt(r'A:UPDATE PythonModule 11Programsconstituents- financials.csv', skiprows=1, dtype=str, delimiter=",", usecols = (3,4,5)) Arrays from/to ASCII files
  • 17. Slide 17 Other format supported by other similar package