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