Numeric Python
是Python的一种开源数值计算扩展,核心是ndarray对象,n维数组
- 一个强大的N维数组对象Array
- 比较成熟的(广播)函数库
- 用于整合C/C++和Fortran代码的工具包
- 实用的线性代数、傅里叶变换和随机数生成函数
- numpy和稀疏矩阵运算包是scipy配合使用更加强大
https://www.numpy.org.cn/user/
创建numpy:np.array(列表)
numpy常规函数
数学运算
nd.mean() 等价于 np.mean(nd)
nd.sum(0) 计算轴0(列方向)上的和
nd.mean(axis=1)# 计算轴1上的平均值
nd.argmax()# 求最大值
ndarray属性
x = np.arange(10)
# 0 ... 9
np.arange(10, 30, 5)
# 10-30,步长为5
np.zeros((3,4))
# 3行4列全部初始值为0
np.ones((2,3,4), dtype = np.int32)
X = np.arange(15).reshape(3,5)
# 变为3行5列
x.ndim
# 返回数组维度
x.shape
# 返回数组行列数,若维度为1则返回元素个数
x.size
# 返回数组元素个数
类型转换
vector = numpy.array(["1","2","3"])
print(vector.dtype)# S型
print(vector)
vector = vector.astype(float)
print(vector.dtype)# float64型
print(vector)
ndarray索引、切片
X[0][0]
# 二维数组第0行第0个元素
X[2, 2]
# 第二行第二个(建议)
X[0:3]
X[:3]
# 前三行
X[-2:]
# 倒数两行
X[:2, :3]
# 前两行、前三列
X[:2][:3]
# 首先解析X[:2]即前两行,再取X[:2]的前两个元素
X[:2, ::2]
# 前两行,步长为2
X[::-1, ::-1]
# 反转
X[0] == X[0, :]
# 取第一行
X[:, 0]
# 取第一列
uint8,无符号的int8,没有负号,0 ~ 255
int8 2^8 = 256 :从-128 ~ 127
from PIL import Image
image = Image.open('./1.jpeg')
image
image_data = np.array(image)
# 图片数据是ndarray
# 彩色图片是三维,高度、宽度、像素
image_data
# 行、列、像素
image_data.shape
image_data.max()
# 255
image_data.min()
# 0
Image.fromarray(image_data[::5,::5])
# 步长为5取数据,图像变小
# 红绿蓝 0 1 2
# 绿红蓝 1 0 2
# 蓝绿红 2 1 0
# 变蓝
newimage = image_data[:,:,::-1]
Image.fromarray(newimage)
# 变绿
Image.fromarray(image_data[:,:,[1,0,2]])
Image.fromarray(image_data[::5,::5])
reshape :
x.reshape(2, 5)
# 修改维度,参数是一个tuple
x.reshape(2, -1)
# 变为2行,-1不管
x.reshape(-1, 2)
# 变为两列,不管多少行
例:x.shape: (209,64,64,3)
x_flatten = x.reshape(x.shape[0], -1).T
x_flatten.shape: (12288,209)
# 高度0 宽度1 像素2
np.transpose(image_data, axes = (1,0,2))
级联(合并操作):
x = np.array([1, 2, 3])
y = np,array([3, 2, 1])
np.concatenate([x, y]) # 1×6
z = np.array([66, 66, 66]) # 这是一个向量1×3
np.concatenate([x, y, z]) # 1×9
# all the input arrays must have same number of dimensions
# concatenate只能拼接维度一样的,1×3和2×3会报错,但1×3和1×8可以利用列拼接。
A = np.array([[1, 2, 3],
[4, 5, 6]]) 2×3
np.concatenate([A, A])
# 默认沿着行拼接,即4×3
array([[1, 2, 3],
[4, 5, 6],
[1, 2, 3],
[4, 5, 6]])
np.concatenate([A, A], axis=1)
# 沿着列拼接,即2×6
array([[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6]])
A2 = np.concatenate([A, z.reshape(1, -1)])
import numpy as np
x = np.array([[1,2,3],
[8,8,8]])
y = np.array([[9,9,9],
[5,5,5],
[4,4,4]])
new = np.vstack([x,y])
new
array([[1, 2, 3],
[8, 8, 8],
[9, 9, 9],
[5, 5, 5],
[4, 4, 4]])
# 即使维度不同,vstack可以竖直拼接(维度增加),axis=0
B = np.full((3,2),100)
B
new1 = np.hstack([y,B])
new1
array([[ 9, 9, 9, 100, 100],
[ 5, 5, 5, 100, 100],
[ 4, 4, 4, 100, 100]])
# hstack 水平拼接(列数增加),axis = 1
切分:
np.split()
参数二:indices_or_sections,可以说int、也可以是一维的array
np.vsplit(水平切分)分成上下两块
np.hsplit(竖直切分)分成左右两块
副本
所有赋值运算不会为ndarray的任何元素创建副本,对赋值后的对象的操作也对原来的对象生效。
聚合操作
nd.sum()
nd.sum(axis=1)# 对行求和
nd.sum(axis=0)# 对列求和
nd.mean() # 平均值
nd.mean(axis = 1) # 计算每行的平均值,列没了
nd.mean(axis = 0) # 计算每列的平均值,行没了
nd.prod() # 累乘
nd.std()
nd.var()
nd.median() # 中位数,如果是多维数组,则先要转换为一维数组
nd.max()
nd.argmax() # 返回max值的位置索引
nd.min()
nd.argmin()
nd.sort()
nd.power() # 幂运算
nd.argwhere(nd > 50) # 条件筛选
......
numpy中的ravel()、flatten()、squeeze()都有将多维数组转换为一维数组的功能,区别:
ravel():如果没有必要,不会产生源数据的副本
flatten():返回源数据的副本
squeeze():只能对维数为1的维度降维
reshape(-1)也可以“拉平”多维数组