2025年3月31日 星期一 乙巳(蛇)年 正月初一 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > Python

Numpy使用

时间:03-29来源:作者:点击数:37

Numpy

4.1numpy优势

1.numpy定义:

  • 是一个开源的Python科学计算库,用于快速处理任意维度的数组。
  • Numpy中存储对象ndarray

2.创建ndarray

  • np.array([ ])

3.numpy优势

  • 内存块风格 --一体式存储
  • 支持并行化运算
  • 效率高于纯python代码,内部释放GIL,底部使用c

4.2 N维数组-ndarray

1 ndarray的属性

  • 属性名字 属性解释
  • ndarray.shape 数组维度的元组
  • ndarray.ndim 数组维数
  • ndarray.size 数组中的元素数量
  • ndarray.itemsize 一个数组元素的长度(字节)
  • ndarray.dtype 数组元素的类型

2.ndarray形状

  • np.array()
  • 三维数组不好理解 ---excel中很多sheet

3.ndarray类型

  • bool
  • int
  • str

  • - 注意:若不指定,整数默认int64,小数默认float64

4.3 基本操作

  • 1.生成01的数组
  • np.ones()
  • np.ones.like()
  • 2.从现有数组生成
  • np.array -- 深拷贝
  • np.asarray --浅拷贝
  • 3.生成固定范围数组
  • np.linspace()
  • num -- 生成等间隔多少个
  • np.arange()
  • step --每间隔多少生成数据
  • np.logspace()
  • 生成以10N次幂数据
  • 4.生成随机数
  • 1.均匀分布生成
  • np.random.uniform()
  • - low
  • - heigh
  • - size

2.正态分布

  • 均值,方差
  • 均值 --图形左右位置
  • 方差 --图像是瘦还是胖
  • - 值越小,图像越瘦高,数据越集中
  • - 值越大,图像越矮胖,数据越分散

3.正态分布api

  • np.random.normal()
  • - low()
  • - heigh()
  • - size()

4.2数组的索引切片

  • 直接索引
  • 先对行进行索引,然后对列索引
  • 高维度数组索引从宏观到微观

形状修改

  • 1.对象.reshape
  • 不进行行列互换,不产生新的变量
  • 2.对象.resize
  • 进项行列互换,对原值进行修改
  • 3.对象.T
  • 进行行列互换

5.类型修改:

5.1 ndarray.astype(type)

  • 返回修改了类型之后的数组
  • stock_change.astype(np.int32)

5.2 ndarray.tostring([order])或者ndarray.tobytes([order])

  • 构造包含数组中原始数据字节的Python字节
  • arr = np.array([[[1, 2, 3], [4, 5, 6]], [[12, 3, 34], [5, 6, 7]]])
  • arr.tostring()

6 数组的去重

6.1 np.unique()

  • temp = np.array([[1, 2, 3, 4],[3, 4, 5, 6]])

7.ndarray运算

1 逻辑运算

  • # 生成10名同学,5门功课的数据
  • >>> score = np.random.randint(40, 100, (10, 5))
  • # 取出最后4名同学的成绩,用于逻辑判断
  • >>> test_score = score[6:, 0:5]
  • # 逻辑判断, 如果成绩大于60就标记为True 否则为False
  • >>> test_score > 60
  • array([[ True, True, True, False, True],
  • [ True, True, True, False, True],
  • [ True, True, False, False, True],
  • [False, True, True, True, True]])
  • # BOOL赋值, 将满足条件的设置为指定的值-布尔索引
  • >>> test_score[test_score > 60] = 1
  • >>> test_score
  • array([[ 1, 1, 1, 52, 1],
  • [ 1, 1, 1, 59, 1],
  • [ 1, 1, 44, 44, 1],
  • [59, 1, 1, 1, 1]])

2 通用判断函数

  • np.all()
  • # 判断前两名同学的成绩[0:2, :]是否全及格
  • >>> np.all(score[0:2, :] > 60)
  • False
  • np.any()
  • # 判断前两名同学的成绩[0:2, :]是否有大于90分的
  • >>> np.any(score[0:2, :] > 80)
  • True

3 np.where(三元运算符)

  • 通过使用np.where能够进行更加复杂的运算
  • np.where()
  • # 判断前四名学生,前四门课程中,成绩中大于60的置为1,否则为0
  • temp = score[:4, :4]
  • np.where(temp > 60, 1, 0)
  • 复合逻辑需要结合np.logical_and和np.logical_or使用
  • # 判断前四名学生,前四门课程中,成绩中大于60且小于90的换为1,否则为0
  • np.where(np.logical_and(temp > 60, temp < 90), 1, 0)
  • # 判断前四名学生,前四门课程中,成绩中大于90或小于60的换为1,否则为0
  • np.where(np.logical_or(temp > 90, temp < 60), 1, 0)

4.统计运算

  • 在数据挖掘/机器学习领域,统计指标的值也是我们分析问题的一种方式。常用的指标如下:
  • min(a, axis)
  • Return the minimum of an array or minimum along an axis.
  • max(a, axis])
  • Return the maximum of an array or maximum along an axis.
  • median(a, axis)
  • Compute the median along the specified axis.
  • mean(a, axis, dtype)
  • Compute the arithmetic mean along the specified axis.
  • std(a, axis, dtype)
  • Compute the standard deviation along the specified axis.
  • var(a, axis, dtype)
  • Compute the variance along the specified axis.
  • 进行统计的时候,axis 轴的取值并不一定,Numpy中不同的API轴的值都不一样,在这里,axis 0代表列, axis 1代表行去进行统计

8.数组之间运算

  • 1 数组与数的运算
  • arr = np.array([[1, 2, 3, 2, 1, 4], [5, 6, 1, 2, 3, 1]])
  • arr + 1
  • arr / 2
  • # 可以对比python列表的运算,看出区别
  • a = [1, 2, 3, 4, 5]
  • a * 3

2 数组与数组的运算

  • arr1 = np.array([[1, 2, 3, 2, 1, 4], [5, 6, 1, 2, 3, 1]])
  • arr2 = np.array([[1, 2, 3, 4], [3, 4, 5, 6]])
数组运算,满足广播机制

9.矩阵

其他知识略…,我也看不懂,api看懂了

矩阵api

  • np.matmul
  • np.dot
  • >>> a = np.array([[80, 86],
  • [82, 80],
  • [85, 78],
  • [90, 90],
  • [86, 82],
  • [82, 90],
  • [78, 80],
  • [92, 94]])
  • >>> b = np.array([[0.7], [0.3]])
  • >>> np.matmul(a, b)
  • array([[81.8],
  • [81.4],
  • [82.9],
  • [90. ],
  • [84.8],
  • [84.4],
  • [78.6],
  • [92.6]])
  • >>> np.dot(a,b)
  • array([[81.8],
  • [81.4],
  • [82.9],
  • [90. ],
  • [84.8],
  • [84.4],
  • [78.6],
  • [92.6]])

np.matmul和np.dot的区别:

  • 二者都是矩阵乘法。 np.matmul中禁止矩阵与标量的乘法。 在矢量乘矢量的內积运算中,np.matmul与np.dot没有区别。
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
上一篇:pandas使用 下一篇:猫眼字体反爬
推荐内容
相关内容
栏目更新
栏目热门