欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

Python NumPy的使用

发布时间:2025/5/22 python 57 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Python NumPy的使用 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

NumPy

  • 学习目标:
        • NumPy的使用
  • 学习内容:
  • 使用步骤
    • Numpy 数组操作
        • 1、 数组的拼接
        • 2、 数组的分割
        • 3、 数组的行列交换
    • NumPy 统计函数
        • 1、 求和
        • 2、 求和函数
        • 3、 均值函数
        • 4、 中值函数
        • 5、 最大值与最小值函数
        • 6、 极值函数
        • 7、 标准差函数
        • 小结
          • 对数组中的 nan 填充均值
  • 总结

学习目标:

NumPy的使用

环境:

  • Anaconda 2.0.4
  • Python 3.7.10
    • numpy 1.20.2

NumPy安装:

pip install numPy

Python 引入 numpy 库

import numpy as np

学习内容:

Numpy 数组操作

  • 1、 数组的拼接
  • 2、 数组的分割
  • 3、 数组的行列交换

NumPy 统计函数

  • 1、求和
  • 2、均值
  • 3、中值
  • 4、最大值
  • 5、最小值
  • 6、极值
  • 7、标准差

使用步骤

Numpy 数组操作

1、 数组的拼接

函数描述
hstack水平堆叠序列中的数组(列方向)
vstack竖直堆叠序列中的数组(行方向)

代码如下(示例):

import numpy as npt1 = np.arange(12).reshape((2, 6)) # 创建一个 2行6列的数组 print(t1)t2 = np.arange(12, 24).reshape((2, 6)) print(t2) [[ 0 1 2 3 4 5][ 6 7 8 9 10 11]][[12 13 14 15 16 17][18 19 20 21 22 23]] t3 = np.vstack((t1, t2)) # 竖直拼接 print(t3) [[ 0 1 2 3 4 5][ 6 7 8 9 10 11][12 13 14 15 16 17][18 19 20 21 22 23]] t4 = np.hstack((t1, t2)) # 水平拼接 print(t4) [[ 0 1 2 3 4 5 12 13 14 15 16 17][ 6 7 8 9 10 11 18 19 20 21 22 23]]

2、 数组的分割

函数描述
split将一个数组分割为多个子数组
hsplit将一个数组水平分割为多个子数组(按列)
vsplit将一个数组垂直分割为多个子数组(按行)

numpy.split(ary, indices_or_sections, axis)

  • axis:设置沿着哪个方向进行切分,默认为 0,横向切分,即水平方向。为 1 时,纵向切分,即竖直方向。

代码如下(示例):

import numpy as npt1 = np.arange(12).reshape((2, 6)) t2 = np.arange(12, 24).reshape((2, 6)) t3 = np.vstack((t1, t2)) # 将上面2个数组进行竖直拼接 print(t3) t4 = np.hstack((t1, t2)) # 将t1 t2两个数组进行水平拼接 print(t4) print(np.split(t3, 2, axis=0)) # 使用np.split()函数将上面的数组进行横向分割 [array([[ 0, 1, 2, 3, 4, 5],[ 6, 7, 8, 9, 10, 11]]), array([[12, 13, 14, 15, 16, 17],[18, 19, 20, 21, 22, 23]])] print(np.split(t3, 2, axis=1)) # 使用np.split()函数将上面的数组进行纵向分割 [array([[ 0, 1, 2],[ 6, 7, 8],[12, 13, 14],[18, 19, 20]]), array([[ 3, 4, 5],[ 9, 10, 11],[15, 16, 17],[21, 22, 23]])]

注:axis 为 0 时在水平方向分割,axis 为 1 时在垂直方向分割。

print(np.hsplit(t4, 2)) # 用 np.hsplit()函数水平分割分割成2个数组 [array([[ 0, 1, 2, 3, 4, 5],[ 6, 7, 8, 9, 10, 11]]), array([[12, 13, 14, 15, 16, 17],[18, 19, 20, 21, 22, 23]])] print(np.vsplit(t3, 2)) # 用 np.vsplit()函数 垂直分隔成2个数组 [array([[ 0, 1, 2, 3, 4, 5],[ 6, 7, 8, 9, 10, 11]]), array([[12, 13, 14, 15, 16, 17],[18, 19, 20, 21, 22, 23]])] print(np.vsplit(t3, 4)) # 用 np.vsplit()函数 垂直分隔成4个数组 [array([[0, 1, 2, 3, 4, 5]]), array([[ 6, 7, 8, 9, 10, 11]]), array([[12, 13, 14, 15, 16, 17]]), array([[18, 19, 20, 21, 22, 23]])]

3、 数组的行列交换

代码如下(示例):

t = np.arange(12, 24).reshape(3, 4) print(t) [[ 0 1 2 3][ 4 5 6 7][ 8 9 10 11]] print("-------------行交换-------------") t[[1, 2], :] = t[[2, 1], :] # 行交换 第2行与第三行进行交换 print(t) [[ 0 1 2 3][ 8 9 10 11][ 4 5 6 7]] print("-------------列交换-------------") t = np.arange(0, 12).reshape(3, 4) t[:, [0, 2]] = t[:, [2, 0]] # 列交换 第1列与第3列进行交换,每次交换都会改变原数组 print(t) [[ 2 1 0 3][ 6 5 4 7][10 9 8 11]]

NumPy 统计函数

函数描述
sum求和
mean返回数组中元素的算术平均值
np.median中值
max最大值
min最小值
ptp极值
std标准差

注:标准差是一组数据平均值分散程度的一种度量。一个较大的标准差,代表大部分数值和其平均值之间差异较大;一个较小的标准差,代表这些数值较接近平均值反映出数据的波动稳定情况,越大表示波动越大,越不稳定。

1、 求和

代码如下(示例):

t1 = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, np.nan, 15], [16, 17, 18, 19, 20]]) t2 = np.arange(12).reshape((4, 3)) # 创建 4行 3列的数组 print(t1) print(t2) [[ 0. 2. 3. 4. 5.][ 0. 7. 8. 9. 10.][ 0. 12. 13. nan 15.][ 0. 17. 18. 19. 20.]][[ 0 1 2][ 3 4 5][ 6 7 8][ 9 10 11]] print(np.sum(t2)) # 计算t2数组所有的和 66 print(np.sum(t2, axis=0)) # 计算t2数组每一行相加的结果 [18 22 26] print(np.sum(t2, axis=1)) # 计算t2数组每一列相加的结果 [ 3 12 21 30] print(np.sum(t1, axis=0)) # 计算t1数组每一行相加的结果 [ 0. 38. 42. nan 50.] print(np.sum(t1, axis=1)) # 计算t1数组每一列相加的结果 [14. 34. nan 74.]

注意:nan和任何值计算都为nan

2、 求和函数

# numpy中常用统计函数 t2 = np.arange(12).reshape((4, 3)) print(t2.sum(axis=0)) # 求和 [18 22 26]

3、 均值函数

print(t2.mean(axis=0)) # 均值 [4.5 5.5 6.5]

4、 中值函数

print(np.median(t2)) # 中值 print(np.median(t2, axis=0)) # 中值 5.5 [4.5 5.5 6.5]

5、 最大值与最小值函数

print(t2.max(axis=0)) # 最大值 print(t2.min(axis=0)) # 最小值 [ 9 10 11] [0 1 2]

6、 极值函数

print(np.ptp(t2)) # 极值, 即最大值和最小值之差 print(np.ptp(t2, axis=0)) 11 [9 9 9]

7、 标准差函数

print(t2.std(axis=0)) # 标准差 [3.35410197 3.35410197 3.35410197]

小结

对数组中的 nan 填充均值
# coding=utf-8 import numpy as np# 为每一列的nan填充均值 def fill_column_ndarray(t1):for i in range(t1.shape[1]): # 遍历每一列temp_col = t1[:, i] # 当前的一列nan_num = np.count_nonzero(temp_col != temp_col)if nan_num != 0: # 不为0,说明当前这一列中有nantemp_not_nan_col = temp_col[temp_col == temp_col] # 当前一列不为nan的array# 选中当前为nan的位置,把值赋给为不为nan的均值temp_col[np.isnan(temp_col)] = temp_not_nan_col.mean()return t1# 为每一行的nan填充均值 def fill_row_ndarray(t2):for i in range(t1.shape[0]): # 遍历每一行temp_row = t1[i, :] # 当前的一行nan_num = np.count_nonzero(temp_row != temp_row)if nan_num != 0: # 不为0,说明当前这一列中有nantemp_not_nan_row = temp_row[temp_row == temp_row] # 当前一行不为nan的array# 选中当前为nan的位置,把值赋给为不为nan的均值temp_row[np.isnan(temp_row)] = temp_not_nan_row.mean()return t1if __name__ == '__main__':t1 = np.arange(12).reshape((3, 4)).astype("float")t1[1, 0:] = np.nan # 给数组第二行赋值print("原数组:")print(t1)t1 = fill_column_ndarray(t1)print("竖向填充均值:")print(t1)print("*" * 25)t2 = np.arange(12).reshape((3, 4)).astype("float")t2[:, 2] = np.nan # 给数组的第三列赋值print("原数组:")print(t2)t2 = fill_row_ndarray(t2)print("横向填充均值:")print(t2) 原数组: [[ 0. 1. 2. 3.][nan nan nan nan][ 8. 9. 10. 11.]] 竖向填充均值: [[ 0. 1. 2. 3.][ 4. 5. 6. 7.][ 8. 9. 10. 11.]] ************************* 原数组: [[ 0. 1. nan 3.][ 4. 5. nan 7.][ 8. 9. nan 11.]] 横向填充均值: [[ 0. 1. 2. 3.][ 4. 5. 6. 7.][ 8. 9. 10. 11.]]

总结

1.Numpy 中包含了一些函数用于处理数组,大概可分为以下几类:

  • 修改数组形状
  • 翻转数组
  • 修改数组维度
  • 连接数组
  • 分割数组
  • 数组元素的添加与删除

2.nan和任何值计算都为nan。
3.axis 为 0 时在水平方向,axis 为 1 时在垂直方向。

总结

以上是生活随笔为你收集整理的Python NumPy的使用的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。