Numpy常用函数学习
import numpy as np
data_one = np.array([1, 2, 3, 4])
data_sec = np.array([[1, 3, 2, 4], [4, 2, 3, 1]])
""" 创建数组 """
generate1 = 2 * np.eye(4) # 创建但单位矩阵
grt2 = 2 * np.zeros((4, 4)) # 创建0矩阵
grt3 = 3 * np.ones((4, 1, 3)) # 创建1矩阵
# 当shape参数为三元组时((4, 1, 3)) arg1:块数(高) arg2:行数(长) arg3:列数)宽
grt4 = np.array([i ** 3 for i in np.arange(0, 20, 1)]) # 列表推导式
grt5 = np.linspace(0, 10, 10, dtype=int) # 创建一个一维数组 start stop num(生成数据的个数)
grt6 = np.arange(0, 10, 1) # 按序列生数组
grt7 = np.power(np.linspace(0, 1, 10), 2) + 1 # 次方
""" 生成随机数 """
ran1 = np.random.rand(5) # 生成0-1之间的5个随机数
ran2 = np.random.randn(10) # 生成0-1之间的10个随机数(生成正态分布矩阵N-(0, 1))
ran3 = np.random.randint(10, 20, (3, 2)) # low, high, size, dtype
""" 拟合 """
poly = np.polyfit(np.linspace(0, 1, 11), np.linspace(0, 1, 11) ** 2, 2) # x, y, deg(拟合次数)
""" 数据增删 """
add1 = np.insert(np.array([1, 2, 3]), 0, 100) # arr, index, obj
add2 = np.append(np.array([1, 2, 3]), 100) # 尾部插入
sub1 = np.array([1, 2, 3])[0:-1] # 切片法尾部删除
sub2 = np.array([1, 2, 3])[1:0] # 切片发头部删除
sub3 = np.delete([1, 2, 3], 1)
""" 布尔 """
bool1 = np.intersect1d(grt5, grt6) # 求交集
bool2 = np.union1d(grt5, grt6) # 求并集
bool3 = np.setdiff1d([1, 2, 3, 4, 5], [4, 5, 6, 7]) # 求差集 在A但不在B
bool4 = np.all((np.array([1, 2, 3]) < 4) & (np.array([1, 2, 3]) > 0)) # 全部满足就返回True (将数组中的所有数据和数值相比较)
bool5 = np.any((np.array([1, 2, 3]) < 0) | (np.array([1, 2, 3]) > 4)) # 任意满足就返回True
bool6 = ([1, 2, 3] == [1, 2, 4])
bool7 = (np.array([1, 2, 3]) == np.array([1, 2, 4]))
""" 变形 """
def1 = np.reshape(grt2, [2, 8]) # [行, 列]
def2 = grt3.flatten() # 将维
def3 = grt6.tolist() # 以列表形式输出
def4 = np.reshape(grt4, [4, 5]).T # 转置(先reshape->T)
def5 = np.hstack(([1, 2, 3], [4, 5, 6])) # 数组拼接(行)
def6 = np.vstack(([1, 2, 3], [4, 5, 6])) # 数组拼接(列)
def7 = [1, 2, 3][::-1] # 切片数据翻转
def8 = np.array(([1, 2], [3, 4])).reshape(1, 4)[::-1, ::-1] # 行列翻转
""" 查找 """
find1 = data_one[1:3] # 切片 [start, stop, step]
find2 = np.where(data_sec > 2) # 返回输入数组中满足给定条件的元素的索引
find3 = data_sec[(data_sec > 1) | (data_sec < 3)] # 满足条件(并集)
find4 = data_sec[(data_sec > 1) & (data_sec < 3)] # 满足条件(交集)
find5 = np.extract(data_sec > 3, data_sec) # 提取满足的数据 (condition, arr)
find6 = np.argmax(data_one) # 查找最大值的下标
find7 = np.argmin(data_one) # 查找最小值的下标
""" 计算 """
calc1 = np.size(data_sec) # 计数
calc2 = np.sum(data_sec, axis=1) # 求和 axis = 0 水平求和, axis = 1 垂直求和
calc3 = np.mean(data_sec, axis=0) # 求行平均值
calc4 = np.average(data_sec, axis=1) # 求列平均值
calc5 = np.std(data_sec, axis=0) # 求标准差 (axis为设置时默认计算展平数组的标准差:1.去重 2.一维)
calc6 = np.amax(data_sec, axis=0) # 列最大值
calc7 = np.amin(data_sec, axis=1) # 行最小值
calc8 = np.array([1, 2]) * np.array([3, 4]) # 乘积
calc9 = np.diff([-10, 1, 2, 4, 8, 16]) # 依次做差(n-1减n),元素数量减一
calc10 = np.cumsum([-10, 1, 2, 4, 8, 16]) # 前到后累加, 元素数量不变
calc11 = np.fabs(np.array([-1, -2])) # 绝对值
calc12 = np.sqrt(12) # 开根
calc13 = np.square(12) # 平方
calc14 = np.log(12) # 对数 默认以e为底
calc15 = np.log10(12)
calc16 = np.ceil(12.521) # 向上取整
calc17 = np.floor(12.521) # 向下取整
calc18 = np.modf(np.linspace(0, 1, 10)) # 返回小数 + 整数部分
calc19 = np.sign(np.array([-1.1, 0, 1.1])) # 返回正负号 1(+),0,-1(-)
calc20 = np.maximum(2 * np.eye(4, 4), np.ones([4, 4])) # 返回比较后较大值的数组
calc21 = np.minimum(2 * np.eye(4, 4), np.ones([4, 4])) # 返回比较后较小值的数组
calc22 = np.add(data_one, 12) # 加法运算
calc23 = np.multiply(data_one, 2) # 乘法运算
calc24 = np.divide(data_one, 3) # 除法运算
""" 统计,排序 """
sta1 = np.sort([[3, 2, 1], [-2, 2, 0], [10, 30, 20]], axis=1) # 每行内的元素排序,升序 axis=0 列 axis=1 行
sta2 = -np.sort(-np.array([[3, 2, 1], [-2, 2, 0], [10, 30, 20]]), axis=1) # 每行内的元素排序,降序
sta3 = np.sort([[3, 2, 1], [-2, 2, 0], [10, 30, 20]], axis=0) # 每列内的元素排序,升序 axis=0 列 axis=1 行
sta4 = np.argsort([[3, 2, 1], [-2, 2, 0], [10, 30, 20]], axis=1) # 每行内的元素排序的序号(小到大,0 ~ n)
sta5 = np.argsort([[3, 2, 1], [-2, 2, 0], [10, 30, 20]], axis=0) # 每列内的元素排序的序号(小到大,0 ~ n)
""" 线代 向量空间 """
math1 = np.dot([[1, 2], [3, 4]], [[1, 2], [1, 2]]) # 矩阵乘
math2 = np.dot([1, 2, 3], [3, 4, 5]) # 点乘,内积
math3 = np.inner([1, 2], [3, 4]) # 点乘,内积
math4 = np.cross([0, 1, 0], [0, 0, 1]) # 叉乘
math5 = np.linalg.norm([1, 1, 1]) # 向量模(大小)
math6 = np.arccos(np.dot([1, 1], [1, 0])/(np.linalg.norm([1, 1])*np.linalg.norm([1, 0]))) # 向量夹角
math7 = np.linalg.det([[1, 2], [3, 4]]) # 行列式
math8 = np.linalg.solve([[1, 2], [3, 4]], math1) # 求解A-1C
math9 = np.linalg.inv([[1, 2], [3, 4]]) # 逆矩阵
math10 = np.transpose(data_sec) # 矩阵的转置
print(data_sec)
print(calc6)
数据参考:https://zhuanlan.zhihu.com/p/407316614