SlideShare una empresa de Scribd logo
1 de 17
Descargar para leer sin conexión
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

Más contenido relacionado

La actualidad más candente

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
 
11 1. multi-dimensional array eng
11 1. multi-dimensional array eng11 1. multi-dimensional array eng
11 1. multi-dimensional array eng
웅식 전
 

La actualidad más candente (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
 

Destacado

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
Tatiana Al-Chueyr
 
Purchasing administrator kpi
Purchasing administrator kpiPurchasing administrator kpi
Purchasing administrator kpi
ketrisjom
 
Purchasing secretary kpi
Purchasing secretary kpiPurchasing secretary kpi
Purchasing secretary kpi
ketrisjom
 
.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
shriyapen
 

Destacado (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
 

Similar a Numpy tutorial(final) 20160303

Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arrays
Aseelhalees
 
Useful javascript
Useful javascriptUseful javascript
Useful javascript
Lei Kang
 
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
akshatraj875
 

Similar a 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
 

Último

notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
MsecMca
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
 

Último (20)

The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
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
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
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
 

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