SlideShare une entreprise Scribd logo
1  sur  17
Télécharger pour lire hors ligne
2016.03.02
Numpy Tutorial
이남기 (beohemian@gmail.com)
Shape Manipulation (1)
u Changing the shape of an array
• An Array has a shape given by the number of elements along
each axis:
>>> a = np.floor(10*np.random.random((3,4)))
>>> a
array([[ 2., 8., 0., 6.],
[ 4., 5., 1., 1.],
[ 8., 9., 3., 6.]
])
>>> a.shape
(3, 4)
2
numpy.floor
• 소수점 버리고 반올림
Shape Manipulation (2)
u The shape of an array can be changed with various
commands:
>>> a.ravel() # flatten the array
array([ 2., 8., 0., 6., 4., 5., 1., 1., 8., 9., 3., 6.])
>>> a.shape = (6, 2)
>>> a.T
array([[ 2., 0., 4., 1., 8., 3.],
[ 8., 6., 5., 1., 9., 6.]])
3
2		8
0		6
4		5
1		1
8		9
3		6
2		0		4		1		8		3
8		6		5		1		9		6
2		8		0		6		4		5		1		1		8		9		3		6
a.shape = (6, 2) a.T
Shape Manipulation (3)
u Stacking together different arrays
4
>>> a = np.array([1,2,3])
>>> a
array([1,2,3])
>>> b = np.array([4,5,6])
>>> b
array([4,5,6])
>>> np.vstack((a,b))
array([[1,2,3],
[4,5,6]])
>>> np.hstack((a,b))
array([1,2,3,4,5,6])
Shape Manipulation (4)
u Stacking together different arrays
• The function column_stack stacks 1D arrays as columns into a 2D array. It is
equivalent to vstack only for 1D arrays:
5
>>> from numpy import newaxis
>>> np.column_stack((a,b)) # With 2D arrays
array([[ 8., 8., 1., 8.],
[ 0., 0., 0., 4.]])
1		2		3 4		5		6
a b 1		4
2		5
3		6
np.column_stack((a,b))
Shape Manipulation (5)
u Stacking together different arrays
• The function newaxis add an axis to N-dimensional array
6
>>> a = np.array( [[1,2]
[3,4]])
>>> a[0,newaxis]
array([[1,2]])
>>> a[1,newaxis]
array([[3,4]])
>>> a[:,newaxis,:]
array( [[[1,2]], #shape
[[3,4]]] #(2,1,2)
>>> a[newaxis,:,:]
array( [[[1,2], #shape
[3,4]]]) #(1,2,2)
a[:,newaxis,:]
= a[range(0,1), newaxis, range(0,1)
a
[[1,2],
[3,4]]
a[0]
[1,2]
a[1]
[3,4]
Adding
an axis
Adding
an axis
[[3,4]]
[[1,2]]
a[:,newaxis,:]
array( [ [[1,2]],
[[3,4]] ])
Shape Manipulation (6)
u Stacking together different arrays
• The function newaxis add an axis to N-dimensional array
7
>>> a = np.array( [[1,2]
[3,4]])
>>> a[0,newaxis]
array([[1,2]])
>>> a[1,newaxis]
array([[3,4]])
>>> a[:,newaxis,:]
array( [[[1,2]], #shape
[[3,4]]] #(2,1,2)
>>> a[newaxis,:,:]
array( [[[1,2], #shape
[3,4]]]) #(1,2,2)
a
[[1,2],
[3,4]]
shape
(2,2)
a
[[[1,2]],
[[3,4]]]
a[:,newaxis,:]
shape
(2,1,2)
Shape Manipulation (7)
u Stacking together different arrays
• The function column_stack stacks 1D arrays as columns into a 2D array. It is
equivalent to vstack only for 1D arrays:
8
>>> np.column_stack((a[:,newaxis],b[:,newaxis]))
array([[ 4., 2.],
[ 2., 8.]])
>>> np.vstack((a[:,newaxis],b[:,newaxis]))
# The behavior of vstack is different
array([[ 4.],
[ 2.],
[ 2.],
[ 8.]])
4
2
2
8
a[:,newaxis]
b[:,newaxis]
4
2
2
8
np.vstack
Shape Manipulation (8)
u Splitting one array into several smaller ones
9
>>> a = np.floor(10*np.random.random((2,12)))
>>> a
array([[ 9., 5., 6., 3., 6., 8., 0., 7., 9., 7., 2., 7.],
[ 1., 4., 9., 2., 2., 1., 0., 6., 2., 2., 4., 0.]])
>>> np.hsplit(a,3)
[array([[ 9., 5., 6., 3.],
[ 1., 4., 9., 2.]]), array([[ 6., 8., 0., 7.],
[ 2., 1., 0., 6.]]), array([[ 9., 7., 2., 7.],
[ 2., 2., 4., 0.]])]
9		5		6		3		6		8		0		7		9		7		2		7
1		4		9		2		2		1		0		6		2		2		4		0
9		5		6		3		
1		4		9		2		
6		8		0		7
2		1		0		6
9		7		2		7
2		2		4		0
Split a into 3
Shape Manipulation (9)
u Splitting one array into several smaller ones
10
>>> a = np.floor(10*np.random.random((2,12)))
>>> a
array([[ 9., 5., 6., 3., 6., 8., 0., 7., 9., 7., 2., 7.],
[ 1., 4., 9., 2., 2., 1., 0., 6., 2., 2., 4., 0.]])
>>> np.hsplit(a,(3,4))
[array([[ 9., 5., 6.],
[ 1., 4., 9.]]),
array([[ 3.],
[ 2.]]),
array([[ 6., 8., 0., 7., 9., 7., 2., 7.],
[ 2., 1., 0., 6., 2., 2., 4., 0.]])]
9		5		6		3		6		8		0		7		9		7		2		7
1		4		9		2		2		1		0		6		2		2		4		0
9		5		6	
1		4		9
3
2
6		8		0		7		9		7		2		7
2		1		0		6		2		2		4		0
# Split a after the third and the fourth column
Copies and views (1)
u No copy at all
• Simple assignments make no copy of array objects or of their data.
11
>>> a = np.arange(12)
>>> b = a
>>> b is a
True
>>> b.shape = 3,4
>>> a.shape
(3, 4) a
a.shape
b
b.shape
no new object is created
a and b are two names for the same ndarray object
ndarray
object
Copies and views (2)
u No copy at all
• Python passes mutable objects as references, so function calls make no copy.
12
>>> def f(x):
... print(id(x))
...
>>> id(a) # id is a unique identifier of an object
148293216
>>> f(a)
148293216
Copies and views (3)
u View or Shallow Copy
• Different array objects can share the same data. The view method creates a
new array object that looks at the same data.
13
a
a.view
c
c.viewc.base
a.base
ndarray
object1
ndarray
object1’
>>> c = a.view()
>>> c is a
False
>>> c.base is a
True
>>> c.flags.owndata
False
>>>
>>> c.shape = 2,6
>>> a.shape
(3, 4)
>>> c[0,4] = 1234
>>> a
array([[ 0, 1, 2, 3],
[1234, 5, 6, 7],
[ 8, 9, 10, 11]])
data
0		1		2		3
4		5		6		7
8		9		10		11
0		1		2		3		4		5
6		7		8		9		10		11
a c
Copies and views (4)
u View or Shallow Copy
• Different array objects can share the same data. The view method creates a
new array object that looks at the same data.
14
a
a.view
c
c.viewc.base
a.base
ndarray
object1
ndarray
object1’
ndarray.flags.owndata
• The array owns the memory it uses
or borrows it from another object.
data
>>> c = a.view()
>>> c is a
False
>>> c.base is a
True
>>> c.flags.owndata
False
>>>
>>> c.shape = 2,6
>>> a.shape
(3, 4)
>>> c[0,4] = 1234
>>> a
array([[ 0, 1, 2, 3],
[1234, 5, 6, 7],
[ 8, 9, 10, 11]])
Copies and views (5)
u View or Shallow Copy
• Different array objects can share the same data. The view method creates a
new array object that looks at the same data.
15
>>> c = a.view()
>>> c is a
False
>>> c.base is a
True
>>> c.flags.owndata
False
>>>
>>> c.shape = 2,6
>>> a.shape
(3, 4)
>>> c[0,4] = 1234
>>> a
array([[ 0, 1, 2, 3],
[1234, 5, 6, 7],
[ 8, 9, 10, 11]])
a
a.view
c
c.viewc.base
a.base
ndarray
object1
ndarray
object1’
data
0		1		2		3
4		5		6		7
8		9		10		11
0		1		2		3		4		5
6		7		8		9		10		11
a c
Copies and views (6)
u View or Shallow Copy
• Slicing an array returns a view of it:
16
>>> a
array([[ 0, 1, 2, 3],
[1234, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> s = a[ : , 1:3]
>>> s[:] = 10
>>> a
array([[ 0, 10, 10, 3],
[1234, 10, 10, 7],
[ 8, 10, 10, 11]])
Copies and views (7)
u Deep Copy
• The copy method makes a complete copy of the array and its data.
17
>>> d = a.copy()
>>> d is a
False
>>> d.base is a
False
>>> d[0,0] = 9999
>>> a
array([[ 0, 10, 10, 3],
[1234, 10, 10, 7],
[ 8, 10, 10, 11]])
a
a.shape
d
d.shape
ndarray
object
ndarray
object
# a new array object with new data is created
# d doesn't share anything with a

Contenu connexe

Tendances

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
 
Python matplotlib cheat_sheet
Python matplotlib cheat_sheetPython matplotlib cheat_sheet
Python matplotlib cheat_sheetNishant Upadhyay
 
Pandas pythonfordatascience
Pandas pythonfordatasciencePandas pythonfordatascience
Pandas pythonfordatascienceNishant Upadhyay
 
Python For Data Science Cheat Sheet
Python For Data Science Cheat SheetPython For Data Science Cheat Sheet
Python For Data Science Cheat SheetKarlijn Willems
 
Python 2.5 reference card (2009)
Python 2.5 reference card (2009)Python 2.5 reference card (2009)
Python 2.5 reference card (2009)gekiaruj
 
Cheat sheet python3
Cheat sheet python3Cheat sheet python3
Cheat sheet python3sxw2k
 
Cheat Sheet for Machine Learning in Python: Scikit-learn
Cheat Sheet for Machine Learning in Python: Scikit-learnCheat Sheet for Machine Learning in Python: Scikit-learn
Cheat Sheet for Machine Learning in Python: Scikit-learnKarlijn Willems
 
Python seaborn cheat_sheet
Python seaborn cheat_sheetPython seaborn cheat_sheet
Python seaborn cheat_sheetNishant Upadhyay
 
Recommendation System --Theory and Practice
Recommendation System --Theory and PracticeRecommendation System --Theory and Practice
Recommendation System --Theory and PracticeKimikazu Kato
 
Pandas Cheat Sheet
Pandas Cheat SheetPandas Cheat Sheet
Pandas Cheat SheetACASH1011
 
Scikit-learn Cheatsheet-Python
Scikit-learn Cheatsheet-PythonScikit-learn Cheatsheet-Python
Scikit-learn Cheatsheet-PythonDr. Volkan OBAN
 
Python Cheat Sheet
Python Cheat SheetPython Cheat Sheet
Python Cheat SheetGlowTouch
 
11 1. multi-dimensional array eng
11 1. multi-dimensional array eng11 1. multi-dimensional array eng
11 1. multi-dimensional array eng웅식 전
 
Mementopython3 english
Mementopython3 englishMementopython3 english
Mementopython3 englishssuser442080
 

Tendances (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
 
Python matplotlib cheat_sheet
Python matplotlib cheat_sheetPython matplotlib cheat_sheet
Python matplotlib cheat_sheet
 
Pandas pythonfordatascience
Pandas pythonfordatasciencePandas pythonfordatascience
Pandas pythonfordatascience
 
Numpy python cheat_sheet
Numpy python cheat_sheetNumpy python cheat_sheet
Numpy python cheat_sheet
 
Python For Data Science Cheat Sheet
Python For Data Science Cheat SheetPython For Data Science Cheat Sheet
Python For Data Science Cheat Sheet
 
Python 2.5 reference card (2009)
Python 2.5 reference card (2009)Python 2.5 reference card (2009)
Python 2.5 reference card (2009)
 
Cheat sheet python3
Cheat sheet python3Cheat sheet python3
Cheat sheet python3
 
Cheat Sheet for Machine Learning in Python: Scikit-learn
Cheat Sheet for Machine Learning in Python: Scikit-learnCheat Sheet for Machine Learning in Python: Scikit-learn
Cheat Sheet for Machine Learning in Python: Scikit-learn
 
Python seaborn cheat_sheet
Python seaborn cheat_sheetPython seaborn cheat_sheet
Python seaborn cheat_sheet
 
Recommendation System --Theory and Practice
Recommendation System --Theory and PracticeRecommendation System --Theory and Practice
Recommendation System --Theory and Practice
 
NumPy/SciPy Statistics
NumPy/SciPy StatisticsNumPy/SciPy Statistics
NumPy/SciPy Statistics
 
Python_ 3 CheatSheet
Python_ 3 CheatSheetPython_ 3 CheatSheet
Python_ 3 CheatSheet
 
Python bokeh cheat_sheet
Python bokeh cheat_sheet Python bokeh cheat_sheet
Python bokeh cheat_sheet
 
Pandas Cheat Sheet
Pandas Cheat SheetPandas Cheat Sheet
Pandas Cheat Sheet
 
Scikit-learn Cheatsheet-Python
Scikit-learn Cheatsheet-PythonScikit-learn Cheatsheet-Python
Scikit-learn Cheatsheet-Python
 
Python Cheat Sheet
Python Cheat SheetPython Cheat Sheet
Python Cheat Sheet
 
Python Scipy Numpy
Python Scipy NumpyPython Scipy Numpy
Python Scipy Numpy
 
11 1. multi-dimensional array eng
11 1. multi-dimensional array eng11 1. multi-dimensional array eng
11 1. multi-dimensional array eng
 
NUMPY
NUMPY NUMPY
NUMPY
 
Mementopython3 english
Mementopython3 englishMementopython3 english
Mementopython3 english
 

En vedette

NumPy and SciPy for Data Mining and Data Analysis Including iPython, SciKits,...
NumPy and SciPy for Data Mining and Data Analysis Including iPython, SciKits,...NumPy and SciPy for Data Mining and Data Analysis Including iPython, SciKits,...
NumPy and SciPy for Data Mining and Data Analysis Including iPython, SciKits,...Ryan Rosario
 
Desarollando aplicaciones web en python con pruebas
Desarollando aplicaciones web en python con pruebasDesarollando aplicaciones web en python con pruebas
Desarollando aplicaciones web en python con pruebasTatiana Al-Chueyr
 
About Our Recommender System
About Our Recommender SystemAbout Our Recommender System
About Our Recommender SystemKimikazu Kato
 
Python científico (introducción a numpy y matplotlib))
Python científico (introducción a numpy y matplotlib))Python científico (introducción a numpy y matplotlib))
Python científico (introducción a numpy y matplotlib))kikocorreoso
 
Introduction to parallel and distributed computation with spark
Introduction to parallel and distributed computation with sparkIntroduction to parallel and distributed computation with spark
Introduction to parallel and distributed computation with sparkAngelo Leto
 
Getting started with Apache Spark
Getting started with Apache SparkGetting started with Apache Spark
Getting started with Apache SparkHabib Ahmed Bhutto
 
Tavolo Ambiente - Fonderia delle Idee
Tavolo Ambiente - Fonderia delle IdeeTavolo Ambiente - Fonderia delle Idee
Tavolo Ambiente - Fonderia delle IdeeTommaso Ederoclite
 
Purchasing administrator kpi
Purchasing administrator kpiPurchasing administrator kpi
Purchasing administrator kpiketrisjom
 
Purchasing secretary kpi
Purchasing secretary kpiPurchasing secretary kpi
Purchasing secretary kpiketrisjom
 
.the seven habits of highly effective people
.the seven habits of highly effective people.the seven habits of highly effective people
.the seven habits of highly effective peopleshriyapen
 

En vedette (17)

Tutorial de numpy
Tutorial de numpyTutorial de numpy
Tutorial de numpy
 
NumPy and SciPy for Data Mining and Data Analysis Including iPython, SciKits,...
NumPy and SciPy for Data Mining and Data Analysis Including iPython, SciKits,...NumPy and SciPy for Data Mining and Data Analysis Including iPython, SciKits,...
NumPy and SciPy for Data Mining and Data Analysis Including iPython, SciKits,...
 
Python Tutorial
Python TutorialPython Tutorial
Python Tutorial
 
Desarollando aplicaciones web en python con pruebas
Desarollando aplicaciones web en python con pruebasDesarollando aplicaciones web en python con pruebas
Desarollando aplicaciones web en python con pruebas
 
About Our Recommender System
About Our Recommender SystemAbout Our Recommender System
About Our Recommender System
 
Python en Android
Python en AndroidPython en Android
Python en Android
 
Tutorial de matplotlib
Tutorial de matplotlibTutorial de matplotlib
Tutorial de matplotlib
 
SciPy India 2009
SciPy India 2009SciPy India 2009
SciPy India 2009
 
Scipy, numpy and friends
Scipy, numpy and friendsScipy, numpy and friends
Scipy, numpy and friends
 
Python científico (introducción a numpy y matplotlib))
Python científico (introducción a numpy y matplotlib))Python científico (introducción a numpy y matplotlib))
Python científico (introducción a numpy y matplotlib))
 
Introduction to parallel and distributed computation with spark
Introduction to parallel and distributed computation with sparkIntroduction to parallel and distributed computation with spark
Introduction to parallel and distributed computation with spark
 
Getting started with Apache Spark
Getting started with Apache SparkGetting started with Apache Spark
Getting started with Apache Spark
 
Tavolo Ambiente - Fonderia delle Idee
Tavolo Ambiente - Fonderia delle IdeeTavolo Ambiente - Fonderia delle Idee
Tavolo Ambiente - Fonderia delle Idee
 
Purchasing administrator kpi
Purchasing administrator kpiPurchasing administrator kpi
Purchasing administrator kpi
 
Purchasing secretary kpi
Purchasing secretary kpiPurchasing secretary kpi
Purchasing secretary kpi
 
Diversidad linguistica
Diversidad linguistica Diversidad linguistica
Diversidad linguistica
 
.the seven habits of highly effective people
.the seven habits of highly effective people.the seven habits of highly effective people
.the seven habits of highly effective people
 

Similaire à Numpy tutorial(final) 20160303

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
 
Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arraysAseelhalees
 
Useful javascript
Useful javascriptUseful javascript
Useful javascriptLei Kang
 
[1062BPY12001] Data analysis with R / week 2
[1062BPY12001] Data analysis with R / week 2[1062BPY12001] Data analysis with R / week 2
[1062BPY12001] Data analysis with R / week 2Kevin Chun-Hsien Hsu
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in pythonhydpy
 
2.Exploration with CAS-I.Lab2.pptx
2.Exploration with CAS-I.Lab2.pptx2.Exploration with CAS-I.Lab2.pptx
2.Exploration with CAS-I.Lab2.pptxakshatraj875
 
Chapter 3 Built-in Data Structures, Functions, and Files .pptx
Chapter 3 Built-in Data Structures, Functions, and Files .pptxChapter 3 Built-in Data Structures, Functions, and Files .pptx
Chapter 3 Built-in Data Structures, Functions, and Files .pptxSovannDoeur
 
An overview of Python 2.7
An overview of Python 2.7An overview of Python 2.7
An overview of Python 2.7decoupled
 
CE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdfCE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdfUmarMustafa13
 
python-cheatsheets.pdf
python-cheatsheets.pdfpython-cheatsheets.pdf
python-cheatsheets.pdfKalyan969491
 
python-cheatsheets that will be for coders
python-cheatsheets that will be for coderspython-cheatsheets that will be for coders
python-cheatsheets that will be for coderssarafbisesh
 

Similaire à Numpy tutorial(final) 20160303 (20)

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
 
Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arrays
 
Useful javascript
Useful javascriptUseful javascript
Useful javascript
 
[1062BPY12001] Data analysis with R / week 2
[1062BPY12001] Data analysis with R / week 2[1062BPY12001] Data analysis with R / week 2
[1062BPY12001] Data analysis with R / week 2
 
14078956.ppt
14078956.ppt14078956.ppt
14078956.ppt
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in python
 
2.Exploration with CAS-I.Lab2.pptx
2.Exploration with CAS-I.Lab2.pptx2.Exploration with CAS-I.Lab2.pptx
2.Exploration with CAS-I.Lab2.pptx
 
Chapter 3 Built-in Data Structures, Functions, and Files .pptx
Chapter 3 Built-in Data Structures, Functions, and Files .pptxChapter 3 Built-in Data Structures, Functions, and Files .pptx
Chapter 3 Built-in Data Structures, Functions, and Files .pptx
 
Matlab-1.pptx
Matlab-1.pptxMatlab-1.pptx
Matlab-1.pptx
 
Arrays
ArraysArrays
Arrays
 
Unit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTUREUnit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTURE
 
MATLAB ARRAYS
MATLAB ARRAYSMATLAB ARRAYS
MATLAB ARRAYS
 
An overview of Python 2.7
An overview of Python 2.7An overview of Python 2.7
An overview of Python 2.7
 
A tour of Python
A tour of PythonA tour of Python
A tour of Python
 
CE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdfCE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdf
 
C Language Lecture 10
C Language Lecture 10C Language Lecture 10
C Language Lecture 10
 
Python lecture 05
Python lecture 05Python lecture 05
Python lecture 05
 
python-cheatsheets.pdf
python-cheatsheets.pdfpython-cheatsheets.pdf
python-cheatsheets.pdf
 
python-cheatsheets that will be for coders
python-cheatsheets that will be for coderspython-cheatsheets that will be for coders
python-cheatsheets that will be for coders
 

Dernier

"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"mphochane1998
 
kiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadkiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadhamedmustafa094
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network DevicesChandrakantDivate1
 
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...Health
 
Air Compressor reciprocating single stage
Air Compressor reciprocating single stageAir Compressor reciprocating single stage
Air Compressor reciprocating single stageAbc194748
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdfKamal Acharya
 
2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projectssmsksolar
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxSCMS School of Architecture
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueBhangaleSonal
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdfKamal Acharya
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Arindam Chakraborty, Ph.D., P.E. (CA, TX)
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARKOUSTAV SARKAR
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...HenryBriggs2
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaOmar Fathy
 
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...soginsider
 

Dernier (20)

FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
 
kiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadkiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal load
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
 
Air Compressor reciprocating single stage
Air Compressor reciprocating single stageAir Compressor reciprocating single stage
Air Compressor reciprocating single stage
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
 

Numpy tutorial(final) 20160303

  • 2. Shape Manipulation (1) u Changing the shape of an array • An Array has a shape given by the number of elements along each axis: >>> a = np.floor(10*np.random.random((3,4))) >>> a array([[ 2., 8., 0., 6.], [ 4., 5., 1., 1.], [ 8., 9., 3., 6.] ]) >>> a.shape (3, 4) 2 numpy.floor • 소수점 버리고 반올림
  • 3. Shape Manipulation (2) u The shape of an array can be changed with various commands: >>> a.ravel() # flatten the array array([ 2., 8., 0., 6., 4., 5., 1., 1., 8., 9., 3., 6.]) >>> a.shape = (6, 2) >>> a.T array([[ 2., 0., 4., 1., 8., 3.], [ 8., 6., 5., 1., 9., 6.]]) 3 2 8 0 6 4 5 1 1 8 9 3 6 2 0 4 1 8 3 8 6 5 1 9 6 2 8 0 6 4 5 1 1 8 9 3 6 a.shape = (6, 2) a.T
  • 4. Shape Manipulation (3) u Stacking together different arrays 4 >>> a = np.array([1,2,3]) >>> a array([1,2,3]) >>> b = np.array([4,5,6]) >>> b array([4,5,6]) >>> np.vstack((a,b)) array([[1,2,3], [4,5,6]]) >>> np.hstack((a,b)) array([1,2,3,4,5,6])
  • 5. Shape Manipulation (4) u Stacking together different arrays • The function column_stack stacks 1D arrays as columns into a 2D array. It is equivalent to vstack only for 1D arrays: 5 >>> from numpy import newaxis >>> np.column_stack((a,b)) # With 2D arrays array([[ 8., 8., 1., 8.], [ 0., 0., 0., 4.]]) 1 2 3 4 5 6 a b 1 4 2 5 3 6 np.column_stack((a,b))
  • 6. Shape Manipulation (5) u Stacking together different arrays • The function newaxis add an axis to N-dimensional array 6 >>> a = np.array( [[1,2] [3,4]]) >>> a[0,newaxis] array([[1,2]]) >>> a[1,newaxis] array([[3,4]]) >>> a[:,newaxis,:] array( [[[1,2]], #shape [[3,4]]] #(2,1,2) >>> a[newaxis,:,:] array( [[[1,2], #shape [3,4]]]) #(1,2,2) a[:,newaxis,:] = a[range(0,1), newaxis, range(0,1) a [[1,2], [3,4]] a[0] [1,2] a[1] [3,4] Adding an axis Adding an axis [[3,4]] [[1,2]] a[:,newaxis,:] array( [ [[1,2]], [[3,4]] ])
  • 7. Shape Manipulation (6) u Stacking together different arrays • The function newaxis add an axis to N-dimensional array 7 >>> a = np.array( [[1,2] [3,4]]) >>> a[0,newaxis] array([[1,2]]) >>> a[1,newaxis] array([[3,4]]) >>> a[:,newaxis,:] array( [[[1,2]], #shape [[3,4]]] #(2,1,2) >>> a[newaxis,:,:] array( [[[1,2], #shape [3,4]]]) #(1,2,2) a [[1,2], [3,4]] shape (2,2) a [[[1,2]], [[3,4]]] a[:,newaxis,:] shape (2,1,2)
  • 8. Shape Manipulation (7) u Stacking together different arrays • The function column_stack stacks 1D arrays as columns into a 2D array. It is equivalent to vstack only for 1D arrays: 8 >>> np.column_stack((a[:,newaxis],b[:,newaxis])) array([[ 4., 2.], [ 2., 8.]]) >>> np.vstack((a[:,newaxis],b[:,newaxis])) # The behavior of vstack is different array([[ 4.], [ 2.], [ 2.], [ 8.]]) 4 2 2 8 a[:,newaxis] b[:,newaxis] 4 2 2 8 np.vstack
  • 9. Shape Manipulation (8) u Splitting one array into several smaller ones 9 >>> a = np.floor(10*np.random.random((2,12))) >>> a array([[ 9., 5., 6., 3., 6., 8., 0., 7., 9., 7., 2., 7.], [ 1., 4., 9., 2., 2., 1., 0., 6., 2., 2., 4., 0.]]) >>> np.hsplit(a,3) [array([[ 9., 5., 6., 3.], [ 1., 4., 9., 2.]]), array([[ 6., 8., 0., 7.], [ 2., 1., 0., 6.]]), array([[ 9., 7., 2., 7.], [ 2., 2., 4., 0.]])] 9 5 6 3 6 8 0 7 9 7 2 7 1 4 9 2 2 1 0 6 2 2 4 0 9 5 6 3 1 4 9 2 6 8 0 7 2 1 0 6 9 7 2 7 2 2 4 0 Split a into 3
  • 10. Shape Manipulation (9) u Splitting one array into several smaller ones 10 >>> a = np.floor(10*np.random.random((2,12))) >>> a array([[ 9., 5., 6., 3., 6., 8., 0., 7., 9., 7., 2., 7.], [ 1., 4., 9., 2., 2., 1., 0., 6., 2., 2., 4., 0.]]) >>> np.hsplit(a,(3,4)) [array([[ 9., 5., 6.], [ 1., 4., 9.]]), array([[ 3.], [ 2.]]), array([[ 6., 8., 0., 7., 9., 7., 2., 7.], [ 2., 1., 0., 6., 2., 2., 4., 0.]])] 9 5 6 3 6 8 0 7 9 7 2 7 1 4 9 2 2 1 0 6 2 2 4 0 9 5 6 1 4 9 3 2 6 8 0 7 9 7 2 7 2 1 0 6 2 2 4 0 # Split a after the third and the fourth column
  • 11. Copies and views (1) u No copy at all • Simple assignments make no copy of array objects or of their data. 11 >>> a = np.arange(12) >>> b = a >>> b is a True >>> b.shape = 3,4 >>> a.shape (3, 4) a a.shape b b.shape no new object is created a and b are two names for the same ndarray object ndarray object
  • 12. Copies and views (2) u No copy at all • Python passes mutable objects as references, so function calls make no copy. 12 >>> def f(x): ... print(id(x)) ... >>> id(a) # id is a unique identifier of an object 148293216 >>> f(a) 148293216
  • 13. Copies and views (3) u View or Shallow Copy • Different array objects can share the same data. The view method creates a new array object that looks at the same data. 13 a a.view c c.viewc.base a.base ndarray object1 ndarray object1’ >>> c = a.view() >>> c is a False >>> c.base is a True >>> c.flags.owndata False >>> >>> c.shape = 2,6 >>> a.shape (3, 4) >>> c[0,4] = 1234 >>> a array([[ 0, 1, 2, 3], [1234, 5, 6, 7], [ 8, 9, 10, 11]]) data 0 1 2 3 4 5 6 7 8 9 10 11 0 1 2 3 4 5 6 7 8 9 10 11 a c
  • 14. Copies and views (4) u View or Shallow Copy • Different array objects can share the same data. The view method creates a new array object that looks at the same data. 14 a a.view c c.viewc.base a.base ndarray object1 ndarray object1’ ndarray.flags.owndata • The array owns the memory it uses or borrows it from another object. data >>> c = a.view() >>> c is a False >>> c.base is a True >>> c.flags.owndata False >>> >>> c.shape = 2,6 >>> a.shape (3, 4) >>> c[0,4] = 1234 >>> a array([[ 0, 1, 2, 3], [1234, 5, 6, 7], [ 8, 9, 10, 11]])
  • 15. Copies and views (5) u View or Shallow Copy • Different array objects can share the same data. The view method creates a new array object that looks at the same data. 15 >>> c = a.view() >>> c is a False >>> c.base is a True >>> c.flags.owndata False >>> >>> c.shape = 2,6 >>> a.shape (3, 4) >>> c[0,4] = 1234 >>> a array([[ 0, 1, 2, 3], [1234, 5, 6, 7], [ 8, 9, 10, 11]]) a a.view c c.viewc.base a.base ndarray object1 ndarray object1’ data 0 1 2 3 4 5 6 7 8 9 10 11 0 1 2 3 4 5 6 7 8 9 10 11 a c
  • 16. Copies and views (6) u View or Shallow Copy • Slicing an array returns a view of it: 16 >>> a array([[ 0, 1, 2, 3], [1234, 5, 6, 7], [ 8, 9, 10, 11]]) >>> s = a[ : , 1:3] >>> s[:] = 10 >>> a array([[ 0, 10, 10, 3], [1234, 10, 10, 7], [ 8, 10, 10, 11]])
  • 17. Copies and views (7) u Deep Copy • The copy method makes a complete copy of the array and its data. 17 >>> d = a.copy() >>> d is a False >>> d.base is a False >>> d[0,0] = 9999 >>> a array([[ 0, 10, 10, 3], [1234, 10, 10, 7], [ 8, 10, 10, 11]]) a a.shape d d.shape ndarray object ndarray object # a new array object with new data is created # d doesn't share anything with a