SlideShare a Scribd company logo
Scientific Computing with Python
- NumPy
2017/08/03 (Thus.)
WeiYuan
site: v123582.github.io
line: weiwei63
§ 全端⼯程師 + 資料科學家
略懂⼀點網站前後端開發技術,學過資料探勘與機器
學習的⽪⽑。平時熱愛參與技術社群聚會及貢獻開源
程式的樂趣。
Outline
§ About NumPy
§ Ndarray
§ Create a new Array
§ Property of Array
§ Operation of Array
§ Matrix
§ Advanced Usages
3
Outline
§ About NumPy
§ Ndarray
§ Create a new Array
§ Property of Array
§ Operation of Array
§ Matrix
§ Advanced Usages
4
the Ecosystem of Python
5Reference:	https://www.edureka.co/blog/why-you-should-choose-python-for-big-data
6Reference:	https://www.slideshare.net/gabrielspmoreira/python-for-data-science-python-brasil-11-2015
About NumPy
§ NumPy is the fundamental package for scientific computing
with Python. It contains among other things:
• 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
• be used as an efficient multi-dimensional container of generic data.
7
About NumPy
§ NumPy is the fundamental package for scientific computing
with Python. It contains among other things:
• 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
• be used as an efficient multi-dimensional container of generic data.
8
Try it!
§ #練習:Import the numpy package under the name np
9
Try it!
§ #練習:Print the numpy version and the configuration
10
Outline
§ About NumPy
§ Ndarray
§ Create a new Array
§ Property of Array
§ Operation of Array
§ Matrix
§ Advanced Usages
11
Ndarray
§ shape
§ ndim
§ dtype
§ size
§ itemsize
§ data
12
1
2
3
4
5
6
7
8
9
10
11
12
from numpy import *
a = array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]
])
Ndarray
§ shape
§ ndim
§ dtype
§ size
§ itemsize
§ data
13
1
2
3
4
5
6
7
8
9
10
11
12
from numpy import *
a = array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]
])
a.shape # (3, 5)
a.ndim # 2
a.dtype.name # 'int32’
a.size # 15
a.itemsize # 4
type(a) # numpy.ndarray
Outline
§ About NumPy
§ Ndarray
§ Create a new Array
§ Property of Array
§ Operation of Array
§ Matrix
§ Advanced Usages
14
Create a new Ndarray
§ One Dimension Array (1dnarray)
§ Multiple Dimension Array (ndarray)
§ Zeros, Ones, Empty
§ arange and linspace
§ random array
§ array from list/tuple
15
Create a new Ndarray
§ One Dimension Array (1dnarray)
16
1
2
3
4
5
6
7
8
9
import numpy as np
arr1 = np.array([0, 1, 2, 3, 4]) # array([0 1 2 3 4])
type(arr1) # <type 'numpy.ndarray'>
arr1.dtype # dtype('int64')
array( )
Create a new Ndarray
§ One Dimension Array (1dnarray)
17
1
2
3
4
5
6
7
8
9
import numpy as np
arr1 = np.array([0, 1, 2, 3, 4]) # array([0 1 2 3 4])
type(arr1) # <type 'numpy.ndarray'>
arr1.dtype # dtype('int64')
arr2 = np.array([1.2, 2.4, 3.6]) # array([1.2, 2.4, 3.6])
type(arr2) # <type 'numpy.ndarray'>
arr2.dtype # dtype('float64')
array( )
Create a new Ndarray
§ Question:How to assign data type for an array ?
18
Create a new Ndarray
§ Multiple Dimension Array (ndarray)
19
1
2
3
4
5
6
7
8
9
import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
# array([[1, 2, 3],
# [4, 5, 6]])
array( )
Create a new Ndarray
§ Question:How to change shape from 1-d array ?
20
Create a new Ndarray
§ Zeros, Ones, Empty
21
1
2
3
4
5
6
7
8
9
import numpy as np
zeros = np.zeros(5)
# array([ 0., 0., 0., 0., 0.])
zeros( )
Create a new Ndarray
§ Zeros, Ones, Empty
22
1
2
3
4
5
6
7
8
9
import numpy as np
zeros = np.ones(5)
# array([ 1., 1., 1., 1., 1.])
ones( )
Create a new Ndarray
§ Zeros, Ones, Empty
23
1
2
3
4
5
6
7
8
9
import numpy as np
zeros = np.empty(5)
# array([ 0., 0., 0., 0., 0.])
empty( )
Create a new Ndarray
§ arange and linspace
24
1
2
3
4
5
6
7
8
9
import numpy as np
arange = np.arange(5)
# array([0 1 2 3 4])
arange( )
Create a new Ndarray
§ arange and linspace
25
1
2
3
4
5
6
7
8
9
import numpy as np
linspace = np.linspace(0, 4, 5)
# array([ 0., 1., 2., 3., 4.])
linspace( )
Create a new Ndarray
§ random array
26
1
2
3
4
5
6
7
8
9
import numpy as np
linspace = np.random.randint(0, 2, size=4)
# array([ 0, 1, 1, 1])
random.randint( )
Try it!
§ #練習:Create a 3x3x3 array with random values
27
Try it!
§ #練習:Find indices of non-zero elements
28
Create a new Ndarray
§ array from list/tuple
29
1
2
3
4
5
6
7
8
9
import numpy as np
x = [1,2,3]
a = np.asarray(x)
x = (1,2,3)
a = np.asarray(x)
asarray( )
Try it!
§ #練習:Create a null vector of size 10
30
Try it!
§ #練習:Create a vector with values ranging from 10 to 49
31
Try it!
§ #練習:Create a 3x3 identity matrix
32
Outline
§ About NumPy
§ Ndarray
§ Create a new Array
§ Property of Array
§ Operation of Array
§ Matrix
§ Advanced Usages
33
Property of Ndarray
§ shape
§ ndim
§ dtype
§ size
§ itemsize
§ data
34
1
2
3
4
5
6
7
8
9
10
11
12
import numpy as np
a = np.array(
[[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
[26, 27, 28 ,29, 30],
[31, 32, 33, 34, 35]
])
Property of Ndarray
§ shape
§ ndim
§ dtype
§ size
§ itemsize
§ data
35
1
2
3
4
5
6
7
8
9
10
11
12
print(type(a))
print(a.shape)
print(a.ndim)
print(a.dtype)
print(a.size)
print(a.itemsize)
print(a.nbytes)
Try it!
§ #練習:How to find the memory size of any array
36
data type
37
§ Question:How to assign data type for an array ?
1. Set dtype with create the array
2. Change dtype function
1. Set dtype with create the array
38
1
2
3
4
5
6
7
8
9
x = numpy.array([1,2.6,3], dtype = numpy.int64)
print(x) #
print(x.dtype) #
x = numpy.array([1,2,3], dtype = numpy.float64)
print(x) #
print(x.dtype) #
array( )
2. Change dtype function
39
1
2
3
4
5
6
7
8
9
x = numpy.array([1,2.6,3], dtype = numpy.float64)
y = x.astype(numpy.int32)
print(y) # [1 2 3]
print(y.dtype)
z = y.astype(numpy.float64)
print(z) # [ 1. 2. 3.]
print(z.dtype)
astype( )
40
data shape
41
§ Question:How to change shape from 1-d array ?
1. Set multiple array with create the array
2. Assign new shape to shape property
3. Change shape function
1. Set multiple array with create the array
42
1
2
3
4
5
6
7
8
9
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
a.shape # (2, 3)
array( )
2. Assign new shape to shape property
43
1
2
3
4
5
6
7
8
9
a = np.array([[1,2,3],[4,5,6]])
a.shape = (3,2)
a # [[1, 2], [3, 4], [5, 6]]
array( )
3. Change shape function
44
1
2
3
4
5
6
7
8
9
a = np.array([[1,2,3],[4,5,6]])
b = a.reshape(3,2)
b # [[1, 2], [3, 4], [5, 6]]
reshape( )
3. Change shape function
45
1
2
3
4
5
6
7
8
9
a = np.array([[1,2,3],[4,5,6]])
b = a.reshape(3,2)
b # [[1, 2], [3, 4], [5, 6]]
a.resize(3,2)
a # [[1, 2], [3, 4], [5, 6]]
resize( )
Try it!
§ #練習:Create a 3x3 matrix with values ranging from 0 to 8
46
index and slicing
§ index
§ slicing
47
1
2
3
4
array[0] # 0
array[1] # 1
array[-1] # 4
1
2
3
4
5
array[1:3] # [1, 2]
array[:4] # [0, 1, 3]
array[3:] # [3, 4]
array[1:4:2] # [1, 3]
array[::-1] # [4, 3, 2, 1, 0]
([0,	1,	2,	3,	4])
index and slicing
§ index
§ slicing
48
1
2
3
4
array[1] # [0, 1]
array[1][0] # 0
array[1][1] # 1
array[2][0] # 2
1
2
3
4
5
array[0:2] # [[0, 1], [2, 3]]
array[:2] # [[0, 1], [2, 3]]
array[2:] # [[4, 5]]
(	[0,	1,	0,	1,	0],
[2,	3,	2,	3,	2],
[4,	5,	4,	5,	4]	)
index and slicing
§ slicing
49
1
2
3
4
array[0, 1:4] # [1, 0, 1]
array[[0, 0, 0], [1, 2, 3]]
array[1:3, 0] # [1, 3, 5]
array[[1, 2], [0, 0, 0]]
(	[0,	1,	0,	1,	0],
[2,	3,	2,	3,	2],
[4,	5,	4,	5,	4]	)
Try it!
§ #練習:Create a null vector of size 10 but the fifth value which
is 1
50
Try it!
§ #練習:Reverse a vector (first element becomes last)
51
Try it!
§ #練習:Create a 2d array with 1 on the border and 0 inside
52
Try it!
§ #練習:Create a 8x8 matrix and fill it with a checkerboard
pattern
53
Try it!
§ #練習:
54
1
2
3
4
5
6
7
8
9
import numpy as np
a = np.array([[1,2,3],[3,4,5],[4,5,6]])
# 第二列元素
# 第二行元素
# 除了第二列的元素
Try it!
§ #練習:
55
1
2
3
4
array[::2,::2]
array[:, 1]
Outline
§ About NumPy
§ Ndarray
§ Create a new Array
§ Property of Array
§ Operation of Array
§ Matrix
§ Advanced Usages
56
Basic Operators
57
sophisticated (broadcasting)
58
ufunc
59
staticstic
60
Outline
§ About NumPy
§ Ndarray
§ Create a new Array
§ Property of Array
§ Operation of Array
§ Matrix
§ Advanced Usages
61
2d-array
62
Matrix
63
2d-array vs Matrix
64
Outline
§ About NumPy
§ Ndarray
§ Create a new Array
§ Property of Array
§ Operation of Array
§ Matrix
§ Advanced Usages
65
Advanced Usages
§ Boolean indexing and Fancy indexing
§ Boolean masking
§ Incomplete Indexing
§ Where function
§ Customize dtype
66
Thanks for listening.
2017/08/03 (Thus.) Scientific Computing with Python – NumPy
Wei-Yuan Chang
v123582@gmail.com
v123582.github.io
Scientific Computing with Python - NumPy | WeiYuan
Scientific Computing with Python - NumPy | WeiYuan
Scientific Computing with Python - NumPy | WeiYuan
Scientific Computing with Python - NumPy | WeiYuan
Scientific Computing with Python - NumPy | WeiYuan
Scientific Computing with Python - NumPy | WeiYuan
Scientific Computing with Python - NumPy | WeiYuan
Scientific Computing with Python - NumPy | WeiYuan
Scientific Computing with Python - NumPy | WeiYuan
Scientific Computing with Python - NumPy | WeiYuan

More Related Content

PDF
Python NumPy Tutorial | NumPy Array | Edureka
PPTX
Introduction to numpy Session 1
PDF
Introduction to NumPy
PPTX
Pandas
PDF
Numpy tutorial
PDF
pandas: Powerful data analysis tools for Python
PDF
Python Programming
PPTX
Python: Modules and Packages
Python NumPy Tutorial | NumPy Array | Edureka
Introduction to numpy Session 1
Introduction to NumPy
Pandas
Numpy tutorial
pandas: Powerful data analysis tools for Python
Python Programming
Python: Modules and Packages

What's hot (20)

PDF
Python basic
PDF
Introduction to NumPy (PyData SV 2013)
PPTX
Machine learning with scikitlearn
PPTX
Python Data Structures and Algorithms.pptx
PPTX
NumPy.pptx
PPTX
NUMPY-2.pptx
PDF
Introduction to Python Pandas for Data Analytics
PPT
Introduction to design and analysis of algorithm
PDF
List , tuples, dictionaries and regular expressions in python
PDF
Python Variable Types, List, Tuple, Dictionary
PPTX
Passing an Array to a Function (ICT Programming)
PPTX
Introduction to numpy
PPTX
Python
PPTX
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
PDF
PPTX
Introduction to the basics of Python programming (part 1)
PPTX
Hierarchical clustering
PDF
Python set
PDF
Intoduction to numpy
KEY
NumPy/SciPy Statistics
Python basic
Introduction to NumPy (PyData SV 2013)
Machine learning with scikitlearn
Python Data Structures and Algorithms.pptx
NumPy.pptx
NUMPY-2.pptx
Introduction to Python Pandas for Data Analytics
Introduction to design and analysis of algorithm
List , tuples, dictionaries and regular expressions in python
Python Variable Types, List, Tuple, Dictionary
Passing an Array to a Function (ICT Programming)
Introduction to numpy
Python
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Introduction to the basics of Python programming (part 1)
Hierarchical clustering
Python set
Intoduction to numpy
NumPy/SciPy Statistics
Ad

Similar to Scientific Computing with Python - NumPy | WeiYuan (20)

PPTX
NumPy-python-27-9-24-we.pptxNumPy-python-27-9-24-we.pptx
PDF
Essential numpy before you start your Machine Learning journey in python.pdf
PPTX
UNIT-03_Numpy (1) python yeksodbbsisbsjsjsh
PPTX
Data Preprocessing Introduction for Machine Learning
PPTX
1.NumPy is a Python library used for wor
PPTX
NumPy.pptx
PPTX
Numpy in python, Array operations using numpy and so on
PPT
Introduction to Numpy Foundation Study GuideStudyGuide
PPTX
numpy code and examples with attributes.pptx
PPT
Python crash course libraries numpy-1, panda.ppt
PDF
numpy.pdf
PPTX
NUMPY [Autosaved] .pptx
PPTX
NUMPY LIBRARY study materials PPT 2.pptx
PPTX
Lecture 2 _Foundions foundions NumPyI.pptx
DOCX
Data Manipulation with Numpy and Pandas in PythonStarting with N
PPTX
numpydocococ34554367827839271966666.pptx
PPTX
Chapter 5-Numpy-Pandas.pptx python programming
PPT
14078956.ppt
PPTX
python-numwpyandpandas-170922144956.pptx
NumPy-python-27-9-24-we.pptxNumPy-python-27-9-24-we.pptx
Essential numpy before you start your Machine Learning journey in python.pdf
UNIT-03_Numpy (1) python yeksodbbsisbsjsjsh
Data Preprocessing Introduction for Machine Learning
1.NumPy is a Python library used for wor
NumPy.pptx
Numpy in python, Array operations using numpy and so on
Introduction to Numpy Foundation Study GuideStudyGuide
numpy code and examples with attributes.pptx
Python crash course libraries numpy-1, panda.ppt
numpy.pdf
NUMPY [Autosaved] .pptx
NUMPY LIBRARY study materials PPT 2.pptx
Lecture 2 _Foundions foundions NumPyI.pptx
Data Manipulation with Numpy and Pandas in PythonStarting with N
numpydocococ34554367827839271966666.pptx
Chapter 5-Numpy-Pandas.pptx python programming
14078956.ppt
python-numwpyandpandas-170922144956.pptx
Ad

More from Wei-Yuan Chang (20)

PDF
Python Fundamentals - Basic
PDF
Data Analysis with Python - Pandas | WeiYuan
PDF
Data Crawler using Python (I) | WeiYuan
PDF
Learning to Use Git | WeiYuan
PDF
Basic Web Development | WeiYuan
PDF
資料視覺化 - D3 的第一堂課 | WeiYuan
PDF
JavaScript Beginner Tutorial | WeiYuan
PDF
Python fundamentals - basic | WeiYuan
PDF
Introduce to PredictionIO
PDF
Analysis and Classification of Respiratory Health Risks with Respect to Air P...
PDF
Forecasting Fine Grained Air Quality Based on Big Data
PDF
On the Coverage of Science in the Media a Big Data Study on the Impact of th...
PDF
On the Ground Validation of Online Diagnosis with Twitter and Medical Records
PDF
Effective Event Identification in Social Media
PDF
Eears (earthquake alert and report system) a real time decision support syst...
PDF
Fine Grained Location Extraction from Tweets with Temporal Awareness
PPTX
Practical Lessons from Predicting Clicks on Ads at Facebook
PDF
How many folders do you really need ? Classifying email into a handful of cat...
PDF
Extending faceted search to the general web
PDF
Discovering human places of interest from multimodal mobile phone data
Python Fundamentals - Basic
Data Analysis with Python - Pandas | WeiYuan
Data Crawler using Python (I) | WeiYuan
Learning to Use Git | WeiYuan
Basic Web Development | WeiYuan
資料視覺化 - D3 的第一堂課 | WeiYuan
JavaScript Beginner Tutorial | WeiYuan
Python fundamentals - basic | WeiYuan
Introduce to PredictionIO
Analysis and Classification of Respiratory Health Risks with Respect to Air P...
Forecasting Fine Grained Air Quality Based on Big Data
On the Coverage of Science in the Media a Big Data Study on the Impact of th...
On the Ground Validation of Online Diagnosis with Twitter and Medical Records
Effective Event Identification in Social Media
Eears (earthquake alert and report system) a real time decision support syst...
Fine Grained Location Extraction from Tweets with Temporal Awareness
Practical Lessons from Predicting Clicks on Ads at Facebook
How many folders do you really need ? Classifying email into a handful of cat...
Extending faceted search to the general web
Discovering human places of interest from multimodal mobile phone data

Recently uploaded (20)

PPTX
Data_Analytics_and_PowerBI_Presentation.pptx
PPTX
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
PPTX
Computer network topology notes for revision
PPTX
oil_refinery_comprehensive_20250804084928 (1).pptx
PDF
Foundation of Data Science unit number two notes
PPT
Miokarditis (Inflamasi pada Otot Jantung)
PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
PPTX
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
PPTX
STUDY DESIGN details- Lt Col Maksud (21).pptx
PPTX
05. PRACTICAL GUIDE TO MICROSOFT EXCEL.pptx
PPTX
CEE 2 REPORT G7.pptxbdbshjdgsgjgsjfiuhsd
PPTX
Supervised vs unsupervised machine learning algorithms
PDF
TRAFFIC-MANAGEMENT-AND-ACCIDENT-INVESTIGATION-WITH-DRIVING-PDF-FILE.pdf
PPTX
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
PPT
Reliability_Chapter_ presentation 1221.5784
PPTX
IBA_Chapter_11_Slides_Final_Accessible.pptx
PDF
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
PDF
Fluorescence-microscope_Botany_detailed content
PDF
Galatica Smart Energy Infrastructure Startup Pitch Deck
PPTX
Database Infoormation System (DBIS).pptx
Data_Analytics_and_PowerBI_Presentation.pptx
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
Computer network topology notes for revision
oil_refinery_comprehensive_20250804084928 (1).pptx
Foundation of Data Science unit number two notes
Miokarditis (Inflamasi pada Otot Jantung)
Acceptance and paychological effects of mandatory extra coach I classes.pptx
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
STUDY DESIGN details- Lt Col Maksud (21).pptx
05. PRACTICAL GUIDE TO MICROSOFT EXCEL.pptx
CEE 2 REPORT G7.pptxbdbshjdgsgjgsjfiuhsd
Supervised vs unsupervised machine learning algorithms
TRAFFIC-MANAGEMENT-AND-ACCIDENT-INVESTIGATION-WITH-DRIVING-PDF-FILE.pdf
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
Reliability_Chapter_ presentation 1221.5784
IBA_Chapter_11_Slides_Final_Accessible.pptx
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
Fluorescence-microscope_Botany_detailed content
Galatica Smart Energy Infrastructure Startup Pitch Deck
Database Infoormation System (DBIS).pptx

Scientific Computing with Python - NumPy | WeiYuan

  • 1. Scientific Computing with Python - NumPy 2017/08/03 (Thus.) WeiYuan
  • 2. site: v123582.github.io line: weiwei63 § 全端⼯程師 + 資料科學家 略懂⼀點網站前後端開發技術,學過資料探勘與機器 學習的⽪⽑。平時熱愛參與技術社群聚會及貢獻開源 程式的樂趣。
  • 3. Outline § About NumPy § Ndarray § Create a new Array § Property of Array § Operation of Array § Matrix § Advanced Usages 3
  • 4. Outline § About NumPy § Ndarray § Create a new Array § Property of Array § Operation of Array § Matrix § Advanced Usages 4
  • 5. the Ecosystem of Python 5Reference: https://www.edureka.co/blog/why-you-should-choose-python-for-big-data
  • 7. About NumPy § NumPy is the fundamental package for scientific computing with Python. It contains among other things: • 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 • be used as an efficient multi-dimensional container of generic data. 7
  • 8. About NumPy § NumPy is the fundamental package for scientific computing with Python. It contains among other things: • 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 • be used as an efficient multi-dimensional container of generic data. 8
  • 9. Try it! § #練習:Import the numpy package under the name np 9
  • 10. Try it! § #練習:Print the numpy version and the configuration 10
  • 11. Outline § About NumPy § Ndarray § Create a new Array § Property of Array § Operation of Array § Matrix § Advanced Usages 11
  • 12. Ndarray § shape § ndim § dtype § size § itemsize § data 12 1 2 3 4 5 6 7 8 9 10 11 12 from numpy import * a = array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14] ])
  • 13. Ndarray § shape § ndim § dtype § size § itemsize § data 13 1 2 3 4 5 6 7 8 9 10 11 12 from numpy import * a = array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14] ]) a.shape # (3, 5) a.ndim # 2 a.dtype.name # 'int32’ a.size # 15 a.itemsize # 4 type(a) # numpy.ndarray
  • 14. Outline § About NumPy § Ndarray § Create a new Array § Property of Array § Operation of Array § Matrix § Advanced Usages 14
  • 15. Create a new Ndarray § One Dimension Array (1dnarray) § Multiple Dimension Array (ndarray) § Zeros, Ones, Empty § arange and linspace § random array § array from list/tuple 15
  • 16. Create a new Ndarray § One Dimension Array (1dnarray) 16 1 2 3 4 5 6 7 8 9 import numpy as np arr1 = np.array([0, 1, 2, 3, 4]) # array([0 1 2 3 4]) type(arr1) # <type 'numpy.ndarray'> arr1.dtype # dtype('int64') array( )
  • 17. Create a new Ndarray § One Dimension Array (1dnarray) 17 1 2 3 4 5 6 7 8 9 import numpy as np arr1 = np.array([0, 1, 2, 3, 4]) # array([0 1 2 3 4]) type(arr1) # <type 'numpy.ndarray'> arr1.dtype # dtype('int64') arr2 = np.array([1.2, 2.4, 3.6]) # array([1.2, 2.4, 3.6]) type(arr2) # <type 'numpy.ndarray'> arr2.dtype # dtype('float64') array( )
  • 18. Create a new Ndarray § Question:How to assign data type for an array ? 18
  • 19. Create a new Ndarray § Multiple Dimension Array (ndarray) 19 1 2 3 4 5 6 7 8 9 import numpy as np a = np.array([[1, 2, 3], [4, 5, 6]]) # array([[1, 2, 3], # [4, 5, 6]]) array( )
  • 20. Create a new Ndarray § Question:How to change shape from 1-d array ? 20
  • 21. Create a new Ndarray § Zeros, Ones, Empty 21 1 2 3 4 5 6 7 8 9 import numpy as np zeros = np.zeros(5) # array([ 0., 0., 0., 0., 0.]) zeros( )
  • 22. Create a new Ndarray § Zeros, Ones, Empty 22 1 2 3 4 5 6 7 8 9 import numpy as np zeros = np.ones(5) # array([ 1., 1., 1., 1., 1.]) ones( )
  • 23. Create a new Ndarray § Zeros, Ones, Empty 23 1 2 3 4 5 6 7 8 9 import numpy as np zeros = np.empty(5) # array([ 0., 0., 0., 0., 0.]) empty( )
  • 24. Create a new Ndarray § arange and linspace 24 1 2 3 4 5 6 7 8 9 import numpy as np arange = np.arange(5) # array([0 1 2 3 4]) arange( )
  • 25. Create a new Ndarray § arange and linspace 25 1 2 3 4 5 6 7 8 9 import numpy as np linspace = np.linspace(0, 4, 5) # array([ 0., 1., 2., 3., 4.]) linspace( )
  • 26. Create a new Ndarray § random array 26 1 2 3 4 5 6 7 8 9 import numpy as np linspace = np.random.randint(0, 2, size=4) # array([ 0, 1, 1, 1]) random.randint( )
  • 27. Try it! § #練習:Create a 3x3x3 array with random values 27
  • 28. Try it! § #練習:Find indices of non-zero elements 28
  • 29. Create a new Ndarray § array from list/tuple 29 1 2 3 4 5 6 7 8 9 import numpy as np x = [1,2,3] a = np.asarray(x) x = (1,2,3) a = np.asarray(x) asarray( )
  • 30. Try it! § #練習:Create a null vector of size 10 30
  • 31. Try it! § #練習:Create a vector with values ranging from 10 to 49 31
  • 32. Try it! § #練習:Create a 3x3 identity matrix 32
  • 33. Outline § About NumPy § Ndarray § Create a new Array § Property of Array § Operation of Array § Matrix § Advanced Usages 33
  • 34. Property of Ndarray § shape § ndim § dtype § size § itemsize § data 34 1 2 3 4 5 6 7 8 9 10 11 12 import numpy as np a = np.array( [[11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25], [26, 27, 28 ,29, 30], [31, 32, 33, 34, 35] ])
  • 35. Property of Ndarray § shape § ndim § dtype § size § itemsize § data 35 1 2 3 4 5 6 7 8 9 10 11 12 print(type(a)) print(a.shape) print(a.ndim) print(a.dtype) print(a.size) print(a.itemsize) print(a.nbytes)
  • 36. Try it! § #練習:How to find the memory size of any array 36
  • 37. data type 37 § Question:How to assign data type for an array ? 1. Set dtype with create the array 2. Change dtype function
  • 38. 1. Set dtype with create the array 38 1 2 3 4 5 6 7 8 9 x = numpy.array([1,2.6,3], dtype = numpy.int64) print(x) # print(x.dtype) # x = numpy.array([1,2,3], dtype = numpy.float64) print(x) # print(x.dtype) # array( )
  • 39. 2. Change dtype function 39 1 2 3 4 5 6 7 8 9 x = numpy.array([1,2.6,3], dtype = numpy.float64) y = x.astype(numpy.int32) print(y) # [1 2 3] print(y.dtype) z = y.astype(numpy.float64) print(z) # [ 1. 2. 3.] print(z.dtype) astype( )
  • 40. 40
  • 41. data shape 41 § Question:How to change shape from 1-d array ? 1. Set multiple array with create the array 2. Assign new shape to shape property 3. Change shape function
  • 42. 1. Set multiple array with create the array 42 1 2 3 4 5 6 7 8 9 import numpy as np a = np.array([[1,2,3],[4,5,6]]) a.shape # (2, 3) array( )
  • 43. 2. Assign new shape to shape property 43 1 2 3 4 5 6 7 8 9 a = np.array([[1,2,3],[4,5,6]]) a.shape = (3,2) a # [[1, 2], [3, 4], [5, 6]] array( )
  • 44. 3. Change shape function 44 1 2 3 4 5 6 7 8 9 a = np.array([[1,2,3],[4,5,6]]) b = a.reshape(3,2) b # [[1, 2], [3, 4], [5, 6]] reshape( )
  • 45. 3. Change shape function 45 1 2 3 4 5 6 7 8 9 a = np.array([[1,2,3],[4,5,6]]) b = a.reshape(3,2) b # [[1, 2], [3, 4], [5, 6]] a.resize(3,2) a # [[1, 2], [3, 4], [5, 6]] resize( )
  • 46. Try it! § #練習:Create a 3x3 matrix with values ranging from 0 to 8 46
  • 47. index and slicing § index § slicing 47 1 2 3 4 array[0] # 0 array[1] # 1 array[-1] # 4 1 2 3 4 5 array[1:3] # [1, 2] array[:4] # [0, 1, 3] array[3:] # [3, 4] array[1:4:2] # [1, 3] array[::-1] # [4, 3, 2, 1, 0] ([0, 1, 2, 3, 4])
  • 48. index and slicing § index § slicing 48 1 2 3 4 array[1] # [0, 1] array[1][0] # 0 array[1][1] # 1 array[2][0] # 2 1 2 3 4 5 array[0:2] # [[0, 1], [2, 3]] array[:2] # [[0, 1], [2, 3]] array[2:] # [[4, 5]] ( [0, 1, 0, 1, 0], [2, 3, 2, 3, 2], [4, 5, 4, 5, 4] )
  • 49. index and slicing § slicing 49 1 2 3 4 array[0, 1:4] # [1, 0, 1] array[[0, 0, 0], [1, 2, 3]] array[1:3, 0] # [1, 3, 5] array[[1, 2], [0, 0, 0]] ( [0, 1, 0, 1, 0], [2, 3, 2, 3, 2], [4, 5, 4, 5, 4] )
  • 50. Try it! § #練習:Create a null vector of size 10 but the fifth value which is 1 50
  • 51. Try it! § #練習:Reverse a vector (first element becomes last) 51
  • 52. Try it! § #練習:Create a 2d array with 1 on the border and 0 inside 52
  • 53. Try it! § #練習:Create a 8x8 matrix and fill it with a checkerboard pattern 53
  • 54. Try it! § #練習: 54 1 2 3 4 5 6 7 8 9 import numpy as np a = np.array([[1,2,3],[3,4,5],[4,5,6]]) # 第二列元素 # 第二行元素 # 除了第二列的元素
  • 56. Outline § About NumPy § Ndarray § Create a new Array § Property of Array § Operation of Array § Matrix § Advanced Usages 56
  • 61. Outline § About NumPy § Ndarray § Create a new Array § Property of Array § Operation of Array § Matrix § Advanced Usages 61
  • 65. Outline § About NumPy § Ndarray § Create a new Array § Property of Array § Operation of Array § Matrix § Advanced Usages 65
  • 66. Advanced Usages § Boolean indexing and Fancy indexing § Boolean masking § Incomplete Indexing § Where function § Customize dtype 66
  • 67. Thanks for listening. 2017/08/03 (Thus.) Scientific Computing with Python – NumPy Wei-Yuan Chang v123582@gmail.com v123582.github.io