1Numpy数组对象以及创建方式
ndarray是一个多维数组对象,由“实际数据”+“元数据”组成
创建一维数组
>>> a=np.arange(5)
>>> a.dtype
dtype('int32')
>>> a
array([0, 1, 2, 3, 4])
>>> a.shape
(5L,)
创建多维数组
>>> c=np.array([np.arange(4),np.arange(4),np.arange(4)])
>>> c
array([[0, 1, 2, 3],
[0, 1, 2, 3],
[0, 1, 2, 3]])
>>> c.shape
(3L, 4L)
2选取数组元素
首先创建一个2*2数组:
>>> a=np.array([[1,2],[3,4]])
>>> a
array([[1, 2],
[3, 4]])
开始选取:
>>> a[0,0]
1
>>> a[0,1]
2
3指定Numpy数据类型
>>> x=np.arange(5,dtype=int)
>>> x
array([0, 1, 2, 3, 4])
>>> x.dtype
dtype('int32')
4索引与切片
一维数组的索引与切片
>>> r=np.arange(10)
>>> r
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> r[0:4]
array([0, 1, 2, 3])
>>> r[:4]
array([0, 1, 2, 3])
>>> r[3:7]
array([3, 4, 5, 6])
>>> r[:7:2]
array([0, 2, 4, 6])
>>> r[::-1]
array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
多维数组的索引与切片
首先创建一个数组并改变其维度为3维
>>> x.shape
(24L,)
>>> x=np.arange(24).reshape(2,3,4)
>>> x.shape
(2L, 3L, 4L)
>>> x
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])
选取特定元素:
>>> x[0,0,0]
0
>>> x[0,1,1]
5
切片:
>>> x[:,0,0]
array([ 0, 12])
>>> x[0]
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> x[0,:,:]
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> x[0,...]
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> x[0,1]
array([4, 5, 6, 7])
间隔选取
>>> x[0,1]
array([4, 5, 6, 7])
>>> x[0,0,::2]
array([0, 2])
>>> x[0,1,::2]
array([4, 6])
>>> x[0,1,::1]
array([4, 5, 6, 7])
>>> x[0,1]
array([4, 5, 6, 7])
>>> x[0,::2,-1]
array([ 3, 11])
>>> x[0,::-1,-1]
array([11, 7, 3])
::2代表间隔为2(含第一个元素)
上下翻转:
>>> x[::-1]
array([[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]],
[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]]])
其实就是把列逆序重排了一下
5改变数组维度
(1)ravel:将ndarray展平成一维数组
>>> x
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])
>>> x.ravel
<built-in method ravel of numpy.ndarray object at 0x0000000003497490>
>>> x.ravel()
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23])
(2)flatten:作用与ravel类似,但是它会请求分配内存来保存结果
(3)用tuple来设置维度,功能同reshape:
>>> x.shape
(2L, 3L, 4L)
>>> x.shape=(3,8)
>>> x.shape
(3L, 8L)
>>> x
array([[ 0, 1, 2, 3, 4, 5, 6, 7],
[ 8, 9, 10, 11, 12, 13, 14, 15],
[16, 17, 18, 19, 20, 21, 22, 23]])
(4)transpose:转置
>>> x
array([[ 0, 1, 2, 3, 4, 5, 6, 7],
[ 8, 9, 10, 11, 12, 13, 14, 15],
[16, 17, 18, 19, 20, 21, 22, 23]])
>>> x.transpose()
array([[ 0, 8, 16],
[ 1, 9, 17],
[ 2, 10, 18],
[ 3, 11, 19],
[ 4, 12, 20],
[ 5, 13, 21],
[ 6, 14, 22],
[ 7, 15, 23]])
(5)resize:同reshape,但会直接修改所操作的数组
>>> x
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17],
[18, 19, 20],
[21, 22, 23]])
>>> x.resize(6,4)
>>> x
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]])
>>> x.shape
(6L, 4L)
6数组的组合
首先,我们来创建一个数组 :
>>> a=np.array(np.arange(9)).reshape(3,3)
>>> a
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
或者
>>> a=np.arange(9).reshape(3,3)
>>> a
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
两者都可以生成,whatever,我们已经创建完了数组a,下面来创建数组b
>>> b=2*a
>>> b
array([[ 0, 2, 4],
[ 6, 8, 10],
[12, 14, 16]])
(1)水平组合
hstack
>>> np.hstack((a,b))
array([[ 0, 1, 2, 0, 2, 4],
[ 3, 4, 5, 6, 8, 10],
[ 6, 7, 8, 12, 14, 16]])
concatenate
>>> np.concatenate((a,b),axis=1)
array([[ 0, 1, 2, 0, 2, 4],
[ 3, 4, 5, 6, 8, 10],
[ 6, 7, 8, 12, 14, 16]])
(2)垂直组合
vstack
>>> np.vstack((a,b))
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 0, 2, 4],
[ 6, 8, 10],
[12, 14, 16]])
concatenate
>>> np.concatenate((a,b),axis=0)
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 0, 2, 4],
[ 6, 8, 10],
[12, 14, 16]])
一般来说,axis=0在行(竖向)方向上处理,axis=1在列(横向)方向上处理
(3)深度组合
>>> np.dstack((a,b))
array([[[ 0, 0],
[ 1, 2],
[ 2, 4]],
[[ 3, 6],
[ 4, 8],
[ 5, 10]],
[[ 6, 12],
[ 7, 14],
[ 8, 16]]])
>>> np.dstack((a,b)).shape
(3L, 3L, 2L)
所谓深度组合,就是将一系列数组沿着纵轴方向进行层叠组合
(4)列组合
column_stack
>>> oned=np.arange(2)
>>> oned
array([0, 1])
>>> twice_oned=2*oned
>>> twice_oned
array([0, 2])
>>> np.column_stack((oned,twice_oned))
array([[0, 0],
[1, 2]])
所以对于二维数组,column_stack与hstack效果相同
(5)行组合
row_stack
>>> np.row_stack((oned,twice_oned))
array([[0, 1],
[0, 2]])
所以对于二维数组,row_stack与vstack效果相同
7数组的分割
(1)水平分割
hsplit
>>> np.hsplit(a,3)
[array([[0],
[3],
[6]]), array([[1],
[4],
[7]]), array([[2],
[5],
[8]])]
分割成了3个子数组,他们组成了一个列表
>>> len(np.hsplit(a,3))
3
>>> type(np.hsplit(a,3))
<type 'list'>
>>>
split,取axis=1
>>> a
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> np.split(a,3,axis=1)
[array([[0],
[3],
[6]]), array([[1],
[4],
[7]]), array([[2],
[5],
[8]])]
(2)垂直分割
vsplit
>>> a
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> np.vsplit(a,3)
[array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]])]
split,取axis=0
>>> np.split(a,3,axis=0)
[array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]])]
(3)深度分割
所谓深度分割,就是按照深度方向分割数组
首先创建一个3维数组
>>> c=np.arange(27).reshape(3,3,3)
>>> c.shape
(3L, 3L, 3L)
>>> c
array([[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]],
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23],
[24, 25, 26]]])
然后利用dsplit函数进行深度分割
>>> np.dsplit(c,3)
[array([[[ 0],
[ 3],
[ 6]],
[[ 9],
[12],
[15]],
[[18],
[21],
[24]]]), array([[[ 1],
[ 4],
[ 7]],
[[10],
[13],
[16]],
[[19],
[22],
[25]]]), array([[[ 2],
[ 5],
[ 8]],
[[11],
[14],
[17]],
[[20],
[23],
[26]]])]
分割成了1个列表,长度为3
>>> type(np.dsplit(c,3))
<type 'list'>
>>> len(np.dsplit(c,3))
3
8数组的属性
ndim:给出数组的维数或者数组轴的个数
size:给出数组元素的总个数
itemsize:给出数组中的元素在内存中所占用的字节数
nbytes:size*itemsize
T:与transpose一样
复数的虚部用j表示:
>>> b=np.array([1.j+1,2.j+3])
>>> b
array([ 1.+1.j, 3.+2.j])
real:给出复数数组的实部:
>>> b.real
array([ 1., 3.])
imag:给出复数数组的虚部:
>>> b.imag
array([ 1., 2.])
fliter属性将返回一个numpy.flatiter对象:
>>> z=np.arange(6).reshape(3,2)
>>> z
array([[0, 1],
[2, 3],
[4, 5]])
>>> f=z.flat
>>> f
<numpy.flatiter object at 0x00000000032BED00>
>>> for item in f:
print item
0
1
2
3
4
5
还可以直接用flatiter队对象获取一个数组元素:
>>> z.flat[2]
2
或者获取多个元素:
>>> z.flat[[0,1,2]]
array([0, 1, 2])
falt是一个可赋值的属性,赋值将导致整个数组数组的元素都被覆盖:
>>> z.flat=1
>>> z
array([[1, 1],
[1, 1],
[1, 1]])
或者:
>>> z.flat[[0,1,2,3]]=10
>>> z
array([[10, 10],
[10, 10],
[ 1, 1]])
9数组的转换
tolist函数:将Numpy转换为Python列表
>>> z
array([[10, 10],
[10, 10],
[ 1, 1]])
>>> z.tolist()
[[10, 10], [10, 10], [1, 1]]
>>> type( z.tolist())
<type 'list'>
>>> b
array([ 1.+1.j, 3.+2.j])
>>> b.tolist()
[(1+1j), (3+2j)]
>>> type(b.tolist())
<type 'list'>
astype函数可以在转换数组时指定数据类型:
>>> b
array([ 1.+1.j, 3.+2.j])
>>> b.astype(int)
Warning (from warnings module):
File "__main__", line 2
ComplexWarning: Casting complex values to real discards the imaginary part
array([1, 3])
>>> b.astype('complex')
array([ 1.+1.j, 3.+2.j])
>>> b.astype(complex)
array([ 1.+1.j, 3.+2.j])