Numpy 基础

Numpy 基础

参考https://www.jianshu.com/p/83c8ef18a1e8

import numpy as np

简单创建数组

# 创建简单列表
a = [1, 2, 3, 4]
# 将列表转换为数组
b = np.array(a)

print(a, "	", b)

print("
数组元素个数:	",b.size)
print("数组形状:	", b.shape)
print("数组维度:	", b.ndim)
print("数组元素类型:	", b.dtype)

[1, 2, 3, 4] [1 2 3 4]

数组元素个数: 4
数组形状: (4,)
数组维度: 1
数组元素类型: int32

快速创建N维数组的api函数

# 创建10行10列的数值为浮点1的矩阵
array_one = np.ones([5, 5])
# 创建10行10列的数值为浮点0的矩阵
array_zero = np.zeros([5, 5])

从现有的数据创建数组:

  • array(深拷贝)
  • asarray(浅拷贝)

创建随机数组

  • 均匀分布
    • np.random.rand(10, 10)创建指定形状(示例为10行10列)的数组(范围在0至1之间)
    • np.random.uniform(0, 100)创建指定范围内的一个数
    • np.random.randint(0, 100) 创建指定范围内的一个整数
  • 正态分布
    给定均值/标准差/维度的正态分布 np.random.normal(1.75, 0.1, (2, 3))

数组的索引、切片

# 正态生成4行5列的二维数组
arr = np.random.normal(1.75, 0.1, (4, 5))
print(arr)
# 截取第1至2行的第2至3列(从第0行算起)
after_arr = arr[1:3, 2:4]
print(after_arr)
[[1.70402304 1.6514506  1.63572257 1.81760737 1.91454784]
 [1.65294415 1.82911738 1.78747379 1.82805398 1.76698167]
 [1.91056499 1.86022006 1.89047126 1.62899365 1.88996188]
 [1.77414812 1.630505   1.81022207 1.78061747 1.65964094]]
[[1.78747379 1.82805398]
 [1.89047126 1.62899365]]

改变数组形状(要求前后元素个数匹配)

print("reshape函数的使用!")
one_20 = np.ones([20])
print("-->1行20列<--")
print (one_20)

one_4_5 = one_20.reshape([4, 5])
print("-->4行5列<--")
print (one_4_5)
reshape函数的使用!
-->1行20列<--
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
-->4行5列<--
[[1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1.]]

Numpy 计算

条件运算

stus_score = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]])
stus_score > 80
array([[False,  True],
       [ True,  True],
       [ True, False],
       [ True,  True],
       [False,  True]])
# 三目运算。 小于80的替换为0,不小于80的替换为90
stus_score = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]])
np.where(stus_score < 80, 0, 90)
array([[90, 90],
       [90, 90],
       [90,  0],
       [90, 90],
       [ 0, 90]])

统计运算

指定轴最大值amax(参数1: 数组; 参数2: axis=0/1; 0表示列1表示行)

stus_score = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]])
# 求每一列的最大值(0表示列)
print("每一列的最大值为:")
result = np.amax(stus_score, axis=0)
print(result)

print("每一行的最大值为:")
result = np.amax(stus_score, axis=1)
print(result)
每一列的最大值为:
[86 88]
每一行的最大值为:
[88 82 84 86 81]

指定轴最小值amin

stus_score = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]])
# 求每一行的最小值(0表示列)
print("每一列的最小值为:")
result = np.amin(stus_score, axis=0)
print(result)

# 求每一行的最小值(1表示行)
print("每一行的最小值为:")
result = np.amin(stus_score, axis=1)
print(result)
每一列的最小值为:
[75 75]
每一行的最小值为:
[80 81 75 83 75]

指定轴平均值mean

stus_score = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]])
# 求每一行的平均值(0表示列)
print("每一列的平均值:")
result = np.mean(stus_score, axis=0)
print(result)

# 求每一行的平均值(1表示行)
print("每一行的平均值:")
result = np.mean(stus_score, axis=1)
print(result)
每一列的平均值:
[81.4 81.6]
每一行的平均值:
[84.  81.5 79.5 84.5 78. ]

方差std

stus_score = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]])
# 求每一行的方差(0表示列)
print("每一列的方差:")
result = np.std(stus_score, axis=0)
print(result)

# 求每一行的方差(1表示行)
print("每一行的方差:")
result = np.std(stus_score, axis=1)
print(result)
每一列的方差:
[3.77359245 4.1761226 ]
每一行的方差:
[4.  0.5 4.5 1.5 3. ]

数组运算

stus_score = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]])
print("加分前:")
print(stus_score)

# 为所有平时成绩都加5分
stus_score[:, 0] = stus_score[:, 0]+5
print("加分后:")
print(stus_score)

# 加减乘除类似
加分前:
[[80 88]
 [82 81]
 [84 75]
 [86 83]
 [75 81]]
加分后:
[[85 88]
 [87 81]
 [89 75]
 [91 83]
 [80 81]]

矩阵运算 np.dot()(非常重要)

  • 计算规则
    (M行, N列) * (N行, Z列) = (M行, Z列)
stus_score = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]])
# 平时成绩占40% 期末成绩占60%, 计算结果
q = np.array([[0.4], [0.6]])
result = np.dot(stus_score, q)
print("最终结果为:")
print(result)
最终结果为:
[[84.8]
 [81.4]
 [78.6]
 [84.2]
 [78.6]]
  • 矩阵拼接
  • 矩阵垂直拼接
print("v1为:")
v1 = [[0, 1, 2, 3, 4, 5],
      [6, 7, 8, 9, 10, 11]]
print(v1)
print("v2为:")
v2 = [[12, 13, 14, 15, 16, 17], 
      [18, 19, 20, 21, 22, 23]]
print(v2)
# 垂直拼接
result = np.vstack((v1, v2))
print("v1和v2垂直拼接的结果为")
print(result)
v1为:
[[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11]]
v2为:
[[12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23]]
v1和v2垂直拼接的结果为
[[ 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("v1为:")
v1 = [[0, 1, 2, 3, 4, 5],
      [6, 7, 8, 9, 10, 11]]
print(v1)
print("v2为:")
v2 = [[12, 13, 14, 15, 16, 17], 
      [18, 19, 20, 21, 22, 23]]
print(v2)
# 垂直拼接
result = np.hstack((v1, v2))
print("v1和v2水平拼接的结果为")
print(result)
v1为:
[[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11]]
v2为:
[[12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23]]
v1和v2水平拼接的结果为
[[ 0  1  2  3  4  5 12 13 14 15 16 17]
 [ 6  7  8  9 10 11 18 19 20 21 22 23]]
原文地址:https://www.cnblogs.com/wuxie0ne/p/10672491.html