Numpy基础学习(一)
Numpy基础学习
本文主要内容来自《Python数据分析基础教程 NumPy学习指南》第二版,建议直接阅读该书。
import numpy as np
#创建一维数组
a = np.arange(5)
print(a)#[0 1 2 3 4]
print(a.dtype)#int64
print(a.shape)#(5,);shape属性返回一个元组(tuple),元组中的每个元素代表一个维度上的大小。
#创建多维数组
m = np.array([np.arange(3),np.arange(3),np.arange(3)])
print(m)#[[0 1 2][0 1 2] [0 1 2]]
print(m.shape)#(3 3 3)
#选取数组元素
#嵌套的列表
print(m[0,0])#0
#Numpy数据类型及转化函数
#float16,float32,float64或float,inti,int8,int16,int32,int64,uint8,uint16,uint32,uint64,,bool,complex64,complex128或complex
#复数是不能转换成整数,浮点数,会触发TypeError;但浮点数可以转换为复数列如,complex(1.0)
#单个元素在内存中占用的字节数
print(a.dtype.itemsize)#8
#字符编码 整数i,无符号整数u,单精度浮点数f,双精度浮点数d,布尔值b,复数D,字符串str,unicode字符串U,void(空)V
f = np.arange(7,dtype='f')
print(f)#[0. 1. 2. 3. 4. 5. 6.]
s = f.astype('str')
print(s)#['0.0' '1.0' '2.0' '3.0' '4.0' '5.0' '6.0']
#自定义数据类型数组
t = np.dtype([('name','str',40),('numitems', 'int32'),('price','float32')])
print(t)
itemz = np.array([('M',42,3.14),('b',13,2.72)],dtype=t)
print(itemz[1])#('b', 13, 2.72)
#一维数组的索引和切片
a = np.arange(9)
#用下标3-7来选取元素3-6
b = a[3:7]
print(a,b)#[0 1 2 3 4 5 6 7 8] [3 4 5 6]
#用下标0-7,以2为步长选取元素
print(a[:7:2])#[0 2 4 6]
#用负数下标翻转数组
print(a[::-1])#[8 7 6 5 4 3 2 1 0]
#多维数组的切片和索引
b = np.arange(24).reshape(2,3,4)
print(b)
'''
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
用三维数组b,来表示楼层,行,列
'''
#选取所有楼层的第一行,第一列
print(b[:,0,0])#[ 0 12]
#选取第一层
print(b[0])
#或
print(b[0,:,:])
#或
print(b[0,...])
'''[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
'''
#将第一楼层第2排的进行切片
print(b[0,1])#[4 5 6 7]
print(b[0,1,::2])#[4 6]
print(b[0,1,::3])#[4 7]
print(b[0,1,:2])#[4 5]
#不指定楼层,获取第2列的房间
print(b[...,1])
print(b[:,1])
'''[[ 4 5 6 7]
[16 17 18 19]]
'''
#获取第一层楼,所有位于第二列的房间
print(b[0,:,1])#[1 5 9]
#获取第一层楼,所有位于最后一列的房间
print(b[0,:,-1])#[ 3 7 11]
#反向选取第一层楼最后一列的所有房间
print(b[0,::-1,-1])#[11 7 3]
print(b[0,::2,-1])#[ 3 11]
#改变数组的维度
#使用ravel函数展平数组,不会保存结果,返回数组的一个视图
print(b.ravel())#[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
print(b)
''' [[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
'''
#使用flatten函数展平数组,会保存结果
print(b.flatten())
print(b)
#用元组设置维度,reshape设置维度,resize(会直接修改所操作的数组)
b.shape = (6,4)
print(b)
'''[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]
'''
print(b.reshape(6,4))
'''[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]
'''
# print(b.resize(2,12))
# print(b)
'''
[[ 0 1 2 3 4 5 6 7 8 9 10 11]
[12 13 14 15 16 17 18 19 20 21 22 23]]
'''
#transpose 转置矩阵在线性代数中,操作很常见
# print(b.transpose())
'''[[ 0 4 8 12 16 20]
[ 1 5 9 13 17 21]
[ 2 6 10 14 18 22]
[ 3 7 11 15 19 23]]
'''
#数组的组合,包括水平组合,垂直组合和深度组合方式,将使用vstack,dstack,hstack column_stack,row_stack 以及concatenate函数完成数组的组合
#创建数组
a = np.arange(9).reshape(3,3)
b = 2 * a
# print(a,b)
'''
[[0 1 2]
[3 4 5]
[6 7 8]]
[[ 0 2 4]
[ 6 8 10]
[12 14 16]]
'''
#水平组合hstack,concatenate
print(np.hstack((a,b)))
print(np.concatenate((a,b),axis=1))
'''
[[ 0 1 2 0 2 4]
[ 3 4 5 6 8 10]
[ 6 7 8 12 14 16]]
'''
#垂直组合
print(np.vstack((a,b)))
print(np.concatenate((a,b),axis=0))
'''
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 0 2 4]
[ 6 8 10]
[12 14 16]]
'''
#深度组合,将一系列数组沿纵轴方向层叠组合
print(np.dstack((a,b)))
'''
[[[ 0 0]
[ 1 2]
[ 2 4]]
[[ 3 6]
[ 4 8]
[ 5 10]]
[[ 6 12]
[ 7 14]
[ 8 16]]]
'''
#列组合column_stack对于一维数组按列的方向组合,二维数组和hstack 效果一样。
oned = np.arange(2)
twice = 2*oned
print(np.column_stack((oned,twice)))
'''
[[0 0]
[1 2]]
'''
print(np.column_stack((a,b)))
'''
[[ 0 1 2 0 2 4]
[ 3 4 5 6 8 10]
[ 6 7 8 12 14 16]]
'''
#row_stack对于一维数组行组合直接叠加,二维数组和vstack效果相同。
print(np.row_stack((oned,twice)))
'''
[[0 1]
[0 2]]
'''
#数组的分割hsplit,vsplit,dsplit,split
print(a)
'''
[[0 1 2]
[3 4 5]
[6 7 8]]
'''
print(np.hsplit(a,3))
print(np.split(a,3,axis=1))
'''
[array([[0],
[3],
[6]]),
array([[1],
[4],
[7]]),
array([[2],
[5],
[8]])]
'''
#垂直分割
print(np.vsplit(a,3))
print(np.split(a,3,axis=0))
'''
[array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]])]
'''
#深度分割dsplit函数将深度方向分割数组。
c = np.arange(27).reshape(3,3,3)
print(c)
'''
[[[ 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]]]
'''
print(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]]])]
'''
#数组的属性
#ndim数组的维数,或数组的轴的个数
print(b.ndim)#2
#size 元素的总个数
print(b.size)#9
#元素在内存中所占的字节数
print(b.itemsize)#8
#数组占的存储空间
print(b.nbytes)#72
#T属性的效果和transpose函数一样
#real 输出复数数组的实部
#imag 输出数组的虚部
#flat返回一个numpy.flatiter对象
#数组的转换tolist()
print(b.tolist())#[[0, 2, 4], [6, 8, 10], [12, 14, 16]]
#astype转换数组时指定数据类型
print(b.astype(int))
'''
[[ 0 2 4]
[ 6 8 10]
[12 14 16]]
'''