NumPy中数据类型包含array(数组)、matrix(矩阵)和character(字符串数组)。array类旨在成为用于多种数值计算的通用n维数组,matrix类则专门用于促进线性代数计算,而chararray类的存在是为了向后兼容Numarray(不建议用于新开发)。
本文主要介绍array类及其子类matrix的基本使用方法。
#导入numpy库
import numpy as np
#查看numpy库版本
np.__version__
一、array:一个具有矢量运算和复杂广播能力的快速且节省空间的多维数组,返回值为ndarray类型,ndarray里的数学运算默认为点乘
创建ndarray :两种方式,使用列表创建和使用函数创建
1 使用np.array由python list创建
data = [1, 2, 3]
nd = np.array(data) # array([1, 2, 3])
2 使用np的routine函数创建(部分常用)
ndarray的属性
ndarray基本操作
1 索引:使用同列表
nd = np.random.randint(0,100, size = (4,4))
nd[1,1] # 使用同nd[1][1]
# 可以根据索引修改数据
nd[2,3] = 20
2 切片:使用同列表
#一维
nd[start:end:step]
#二维
nd[row_start:row_end:step,col_start:col_end:step]
#二维数组赋值:如果赋值的数据不足,numpy会自动填充,例如:本来是2,2的数据,现在只填充一个数据,numpy会把这一个数据赋值给这四个数据,这种方式叫广播机制
nd[row_start:row_end,col_start:col_end] = np.random.randint(start,end,size = (1,1))
#三维
nd[row_start:row_end:step,col_start:col_end:step,index] # x行y列的第index个数据
3 变形
nd1 = nd.reshape(new_shape)
# 重塑形状的数据的个数必须和原来的数据保持一致,比如:原来是3*4,重塑形状的数量也必须为12(如2*6)
data = np.array([[1,1],[2,2],[3,3]]) # (3,2)
data.reshape(-1).shape # (6,)
data.reshape(-1,1).shape # (6, 1)
data.reshape(1,-1).shape # (1, 6)
4 级联
1)多个数组之间进行级联
需要注意:参数为列表(一定要加中括号或小括号);维度必须相同;形状必须相符;级联的方向默认是shape这个tuple的第一个值所代表的维度方向,可通过axis参数改变级联的方向(axis为0以行为轴,axis为1以列为轴)
nd1 = np.random.randint(0,10,size = (4,6))
nd2 = np.random.randint(50,100, size = (2,6))
np.concatenate([nd1,nd2], axis = 0) # 以行进行级联 保证列必须相同
nd1 = np.random.randint(0,10,size = (4,6))
nd2 = np.random.randint(50,100, size = (4,3))
np.concatenate([nd1,nd2], axis = 1) # 以列进行级联 保证行必须相同
2)同一个数组进行水平级联与垂直级联(进行维度的变更)
nd = np.random.randint(0,10,size = (10,1))
np.hstack(nd) # 把列变成行,看并且维度降维了
np.vstack(nd) # 把行变成列, 并且维度增加
X_test = np.c_[aa.ravel(), bb.ravel()] # c_水平叠加两个数组(把两个一维数组,分别依据元素的位置进行合并)
5 切分
nd = np.random.randint(0,100,size = (5,6))
'''
array([[42, 99, 41, 0, 23, 46],
[22, 43, 34, 32, 10, 73],
[80, 70, 83, 94, 72, 49],
[87, 58, 30, 37, 7, 33],
[73, 10, 27, 81, 21, 35]])
'''
np.vsplit(nd, [1,2,3])
'''
[array([[42, 99, 41, 0, 23, 46]]),
array([[22, 43, 34, 32, 10, 73]]),
array([[80, 70, 83, 94, 72, 49]]),
array([[87, 58, 30, 37, 7, 33],
[73, 10, 27, 81, 21, 35]])]
'''
np.hsplit(nd, [1,3,5])
'''
[array([[42],
[22],
[80],
[87],
[73]]), array([[99, 41],
[43, 34],
[70, 83],
[58, 30],
[10, 27]]), array([[ 0, 23],
[32, 10],
[94, 72],
[37, 7],
[81, 21]]), array([[46],
[73],
[49],
[33],
[35]])]
'''
np.split(nd, [2],axis = 1) # axis = 0 , 同vsplit ; axis = 1 , 同hsplit
'''
[array([[42, 99],
[22, 43],
[80, 70],
[87, 58],
[73, 10]]), array([[41, 0, 23, 46],
[34, 32, 10, 73],
[83, 94, 72, 49],
[30, 37, 7, 33],
[27, 81, 21, 35]])]
'''
6 副本
1)浅拷贝:所有赋值运算不会为ndarray的任何元素创建副本。对赋值后的对象的操作也对原来的对象生效。
nd_copy = nd
2)深拷贝:对深拷贝后的对象的操作不会对原来的对象生效。
nd_copy = nd.copy()
ndarray常用聚合函数
ndarray的矩阵操作
1 基本矩阵操作
1)算术运算符
nd = np.random.randint(0,10,size = (5,5))
np.add(nd, 3) # 同nd + 3
np.subtract(nd, 3) # 同nd - 3
np.multiply(nd, 3) # 同nd * 3
np.divide(nd, 3) # 同nd / 3
2)矩阵函数
a = np.array([4,5,6,7])
b = np.array([2,3,4])
aa,bb = np.meshgrid(a,b)
'''
array([[4, 5, 6, 7],
[4, 5, 6, 7],
[4, 5, 6, 7]])
'''
'''
array([[2, 2, 2, 2],
[3, 3, 3, 3],
[4, 4, 4, 4]])
'''
aa.ravel() # array([4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7])
bb.ravel() # array([2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4])
nd1 = np.random.randint(0,10,size = (2,3))
nd2 = np.random.randint(1,10,size = (3,2))
np.dot(nd1,nd2)
2 广播机制
a = np.arange(3).reshape((3,1)) # 3行1列
b = np.arange(3) # 1行3列
a + b # 3行3列
ndarray的排序
def sort_nd(nd):
for i in range(nd.size):
for j in range(i, nd.size):
if nd[i] > nd[j]:
nd[i],nd[j] = nd[j], nd[i]
return nd
def sort_nd2(nd):
for i in range(nd.size):
index_min = np.argmin(nd[i:])+ i
#当 i = 0 index_min = 9,
#当 i = 1 index_min = 9
#当 i = 2 index_min = 5
nd[i], nd[index_min] = nd[index_min], nd[i]
return nd
1 快速排序
2 部分排序
二、matrix:matrix对象继承自ndarray,因此,它们具有相同的ndarrays属性和方法。其返回值为matrices类型
创建matrix
参数data可以是字符串
a=np.mat('1 2 3; 4 5 3') # "mat"是NumPy中"matrix"的别名。
'''
matrix([[1, 2, 3],
[4, 5, 3]])
'''
嵌套序列
np.mat([[1,5,10],[2,3,4]])
'''
matrix([[ 1, 5, 10],
[ 2, 3, 4]])
'''
数组
mat = np.mat(np.random.rand(2,2))
'''
matrix([[0.85493952, 0.43864614],
[0.82225678, 0.84431019]])
'''
matrix对象的属性