Numpy

  • ndarray的优势—为什么快?
    • 存储空间连续,访问速度快
    • 可以并行执行-Numpy底层使用C语言编写,内部解除了GIL(全局解释器锁),其对数组的操作速度不受Python解释器的限制,所以,其效率远高于纯Python代码。
    • numpy基于矩阵,矩阵可以分块计算,所以可以实现并行

  • ndarray的属性
    • Shape—形状—注意:返回的是元组
    • ndim—维度的个数—就是shape的长度
    • size—元素个数=shape的各个值想乘
    • dtype—元素的类型

  • ndarray的形状
    1552472-20190906203843877-1631037057.png

生成数组的方法

  • 生成0和1的数组
    • np.ones(shape)
    • np.ones_like(array)—根据给定数组生成一个形状一样的数组
    • np.zeros
    • np.zeros_like
  • 从现有数组生成
    • np.array--深拷贝
    • np.asarray--浅拷贝
  • 生成固定范围的数组
    • np.linspace—start,stop,num 在star到stop之间等区间的选取num个数,注意:一定可以去到stop
    • np.arange(start,stop,step) 以start未开始,每隔step取一个值 stop肯定取不到(左闭右开) 等于python的range
    • np.logspace(start,stop,num) 以10为低的指数值,等比
      1552472-20190906203958081-693847702.png
  • 生成随机数组

    • 均匀分布
      • np.random.rand—0-1之间均匀分布随机数
      • np.random.uniform(low,high,size)—指定最小值和最大值
        从一个均匀分布[low,high)中随机采样,注意定义域是左闭右开,即包含low,不包含high.
    • 正态分布
      • np.random.randn—默认均值为0 方差1
      • np.random.normal(mean,std,size)
      • 两个参数:均值 方差
        • 均值——决定的分布的位置
        • 方差——决定图像的胖瘦
          • 方差大—分布越散,图像越矮胖
          • 方差小—分布越稠密,图像越高瘦

  • 数组的索引、切片[掌握]

    • 注意每个轴的下标
    # 三维,一维
    a1 = np.array([ [[1,2,3],[4,5,6]], [[12,3,34],[5,6,7]]])
    # 返回结果
    array([[[ 1,  2,  3],
          [ 4,  5,  6]],
    
         [[12,  3, 34],
          [ 5,  6,  7]]])
    # 索引、切片
    >>> a1[0, 0, 1]   # 输出: 2

  • 形状修改[掌握]

    • 对象.reshape—返回新的数组
    • 对象.resize---直接修改原数组
    • 对象.T

1552472-20190906204102577-283520364.png


  • 类型修改[知道]

    • 数组对象.astype(指定类型)
    • 数组对象.tostring() tobytes()

  • 数组的去重[知道]

    • np.unique(数组) —#返回的是一维数组
    temp = np.array([[1, 2, 3, 4],[3, 4, 5, 6]])
    >>> np.unique(temp)
    array([1, 2, 3, 4, 5, 6])

ndarray运算[重点]

逻辑运算

  • arr>0.5 ——得到的是一个布尔数组,shape跟arr一样
  • arr[arr>0.5]—访问到满足条件的元素,并可以对其进行赋值操作 arr[arr>0.5]=1

通用判断函数

  • np.all(arr)#全部是True才是True
  • np.any(arr)#全部是False才是False
  • 用法:结合逻辑运算一起进行
    • np.all(data>0.5)判断是不是全部>0.5
    • np.any(data>0.5)判断是不是至少一个>0.5

np.where(三元运算符)

  • np.where(data>0.5,1,0)
    1552472-20190906205226354-320576415.png

  • np.logical_and---np.where(np.logical_and(temp > 0.5, temp < 1), 1, 0)
  • np.logical_or


统计运算

  • np.sum np.min np.max np.median np.mean np.var np.std ——都是将一组值变成一个值
  • axis 代表的就是你要沿着那个轴进行统计

1552472-20190906205439374-794707469.png

在数据挖掘/机器学习领域,统计指标的值也是我们分析问题的一种方式。常用的指标如下:

min(a[, axis, out, keepdims])
Return the minimum of an array or minimum along an axis.
max(a[, axis, out, keepdims])
Return the maximum of an array or maximum along an axis.
median(a[, axis, out, overwrite_input, keepdims])
Compute the median along the specified axis.
mean(a[, axis, dtype, out, keepdims])
Compute the arithmetic mean along the specified axis.
std(a[, axis, dtype, out, ddof, keepdims])
Compute the standard deviation along the specified axis.
var(a[, axis, dtype, out, ddof, keepdims])
Compute the variance along the specified axis.

数学:矩阵

1552472-20190906205742318-1323349056.png

矩阵和向量

  • 矩阵
    有行有列
  • 向量—看成只有1列的矩阵----一定是列向量

加法和标量乘法

  • 加法---两个矩阵对应元素相加
  • 标量乘法 —标量×矩阵中每一个元素

矩阵乘法

  • 左矩阵×右矩阵——注意:左矩阵列数=右矩阵行数
    1552472-20190906212058368-542754109.png

矩阵乘法的性质

  • 不满足交换律
  • 满足结合律
  • 单位矩阵一定是方阵—对角线元素值为1,其他位置为0
    1552472-20190906212329359-296915573.png

逆(了解)、转置

  • 逆矩阵—等价于标量中的倒数
    • 注意:矩阵有逆矩阵的前提是方阵
  • 转置--行列互换

广播(Broadcast)
https://www.runoob.com/numpy/numpy-broadcast.html

矩阵运算

  • np.matmul(A,B)—实现矩阵A × 矩阵B
  • np.dot(A,B)—跟前者一样 ---常用这个
    • 不同点 dot支持标量乘法 matmul不支持

转载于:https://www.cnblogs.com/oklizz/p/11478140.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值