NumPy的主要对象是同种元素的多维数组。这是一个所有的元素都是一种类型、通过一个正整数元组索引的元素表格(通常是元素是数字)。在NumPy中维度(dimensions)叫做轴(axes),轴的个数叫做秩(rank)。
例如,在3D空间一个点的坐标 [1, 2, 3] 是一个秩为1的数组,因为它只有一个轴。那个轴长度为3.又例如,在以下例子中,数组的秩为2(它有两个维度).第一个维度长度为2,第二个维度长度为3.
[[ 1., 0., 0.],
[ 0., 1., 2.]]
NumPy的数组类被称作 ndarray 。通常被称作数组。注意numpy.array和标准Python库类array.array并不相同,后者只处理一维数组和提供少量功能。更多重要ndarray对象属性有:
ndarray.ndim
数组轴的个数,在python的世界中,轴的个数被称作秩ndarray.shape
数组的维度。这是一个指示数组在每个维度上大小的整数元组。例如一个n排m列的矩阵,它的shape属性将是(n,m),简单说就是ndarray.shape = (行数,列数),这个元组的长度显然是秩,即维度或者ndim属性ndarray.size
数组元素的总个数,等于shape属性中元组元素的乘积。ndarray.dtype
一个用来描述数组中元素类型的对象,可以通过创造或指定dtype使用标准Python类型。另外NumPy提供它自己的数据类型。ndarray.itemsize
数组中每个元素的字节大小。例如,一个元素类型为float64的数组itemsiz属性值为8(=64/8),又如,一个元素类型为complex32的数组item属性为4(=32/8).ndarray.data
包含实际数组元素的缓冲区,通常我们不需要使用这个属性,因为我们总是通过索引来使用数组中的元素。
1.用numpy生成数组
In[74]: a = np.arange(5)
In[75]: a.dtype
Out[75]: dtype('int32')
In[76]: a.shape
Out[76]: (5,)
In[77]: a
Out[77]: array([0, 1, 2, 3, 4])
2.创建多维数组
In[78]: m = np.array([np.arange(2),np.arange(2)])
In[79]: print m
[[0 1]
[0 1]]
In[80]: print m.shape
(2, 2)
In[81]: print m.dtype
int32
In[82]: np.zeros(10)
Out[82]: array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
In[83]: np.zeros((2,3))
Out[83]:
array([[ 0., 0., 0.],
[ 0., 0., 0.]])
In[84]: np.empty((2,3,2))
Out[84]:
array([[[ 2.02566915e-322, 0.00000000e+000],
[ 0.00000000e+000, 0.00000000e+000],
[ 0.00000000e+000, 0.00000000e+000]],
[[ 0.00000000e+000, 0.00000000e+000],
[ 0.00000000e+000, 0.00000000e+000],
[ 0.00000000e+000, 0.00000000e+000]]])
In[85]: np.arange(15)
Out[85]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
3.选取数组元素
In[86]: a = np.array([[1,2],[3,4]])
In[87]: print a
[[1 2]
[3 4]]
In[88]: print a[0][0]
1
In[91]: print a[0]
[1 2]
In[92]: print a[:][1]
[3 4]
4 numpy 数据类型
In[3]: import numpy as np
In[4]: print np.float64(42)
42.0
In[5]: print np.int8(42)
42
In[6]: print np.bool(42)
True
In[7]: print np.bool(0)
False
In[8]: print np.bool(42.0)
True
In[9]: print np.float(True)
1.0
In[10]: print np.float(False)
0.0
In[12]: print np.arange(7,dtype = np.uint16)
[0 1 2 3 4 5 6]
In[13]: try:
... print np.int(42.0 + 1.j)
... except TypeError:
... print 'TypeError'
...
TypeError
5 数据类型转换
In[14]: arr = np.array([1,2,3,4,5])
In[15]: arr
Out[15]: array([1, 2, 3, 4, 5])
In[16]: arr.astype(np.int32)
Out[16]: array([1, 2, 3, 4, 5])
In[17]: arr = np.array([3.7,-1.2,-2.6,0.5,12.9,10.1])
In[18]: arr
Out[18]: array([ 3.7, -1.2, -2.6, 0.5, 12.9, 10.1])
In[19]: arr.astype(np.int32)
Out[19]: array([ 3, -1, -2, 0, 12, 10])
In[20]: arr.astype(np.float64)
Out[20]: array([ 3.7, -1.2, -2.6, 0.5, 12.9, 10.1])
In[21]: arr.dtype
Out[21]: dtype('float64')
In[22]: numeric_strings = np.array(['1.24','-9.7','42'])
In[23]: numeric_strings
Out[23]:
array(['1.24', '-9.7', '42'],
dtype='|S4')
In[24]: numeric_strings.astype(float)
Out[24]: array([ 1.24, -9.7 , 42. ])
6 数据类型对象
In[25]: a = np.array([[1,2],[3,4]])
In[26]: print a
[[1 2]
[3 4]]
In[27]: print a.dtype
int32
In[28]: print a.dtype.byteorder
=
In[29]: print a.dtype.itemsize
4
7 字符编码
In[30]: print np.arange(7,dtype = 'f')
[ 0. 1. 2. 3. 4. 5. 6.]
In[31]: print np.arange(7,dtype = 'D')
[ 0.+0.j 1.+0.j 2.+0.j 3.+0.j 4.+0.j 5.+0.j 6.+0.j]
In[32]: print np.dtype(float)
float64
In[33]: print np.dtype('f')
float32
In[34]: print np.dtype('f8')
float64
8 dtype类的属性
In[36]: t = np.dtype('Float64')
C:\Program Files (x86)\JetBrains\PyCharm 2016.2\helpers\pydev\pydevconsole.py:1: DeprecationWarning: Numeric-style type codes are deprecated and will result in an error in the future.
'''
In[37]: print t.char
d
In[38]: print t.type
<type 'numpy.float64'>
In[39]: print t.str
<f8
9 创建自定义数据类型
In[40]: t = np.dtype([('name',np.str_,40),('numitems',np.int32),('price',np.float32)])
In[41]: print t
[('name', 'S40'), ('numitems', '<i4'), ('price', '<f4')]
In[42]: print t['name']
|S40
In[43]: print t
[('name', 'S40'), ('numitems', '<i4'), ('price', '<f4')]
In[44]: itemz = np.array([('Meaning of life DVD',52,3.14),('Butter',13,2.72)],dtype = t)
In[45]: print itemz
[('Meaning of life DVD', 52, 3.1400001 ) ('Butter', 13, 2.72000003)]
In[46]: print itemz[1]
('Butter', 13, 2.72000003)
10 数组与标量的运算
In[47]: arr = np.array([[1.0,2.0,3.0],[4.0,5.0,6.0]])
In[48]: arr
Out[48]:
array([[ 1., 2., 3.],
[ 4., 5., 6.]])
In[49]: arr * arr
Out[49]:
array([[ 1., 4., 9.],
[ 16., 25., 36.]])
In[50]: arr - arr
Out[50]:
array([[ 0., 0., 0.],
[ 0., 0., 0.]])
In[51]: 1/arr
Out[51]:
array([[ 1. , 0.5 , 0.33333333],
[ 0.25 , 0.2 , 0.16666667]])
In[52]: arr ** 0.5
Out[52]:
array([[ 1. , 1.41421356, 1.73205081],
[ 2. , 2.23606798, 2.44948974]])