• numpy函数总结


    numpy是python中常用的库

    import numpy as np

    1、numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)

    –>返回指定区域内的均匀分布的数字,默认包含端点。
    –>endpoint默认stop是最后一个点,选择为False是不包含。
    –>如果retstep是True,返回 (samples, step),step是间隔。

    numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)
    Return evenly spaced numbers over a specified interval. #返回指定区间内间隔均匀的数字
    Returns num evenly spaced samples, calculated over the interval [start, stop]. #返回num个间隔均匀的样本,计算间隔[开始,停止]
    The endpoint of the interval can optionally be excluded. #区间的端点可以选择排除

    -->np.linspace(2.0, 3.0, num=5)
    array([2.  , 2.25, 2.5 , 2.75, 3.  ])
    -->np.linspace(2.0, 3.0, num=5, endpoint=False)
    array([2. ,  2.2,  2.4,  2.6,  2.8])
    -->np.linspace(2.0, 3.0, num=5, retstep=True)
    (array([2.  ,  2.25,  2.5 ,  2.75,  3.  ]), 0.25)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2、numpy.digitize(x, bins, right=False)

    返回输入数组x中每个值对应bins的索引,right表示是否包含右边边界(左右边界只有一个包含,因此可以通过一个参数控制左右边界)
    Return the indices of the bins to which each value in input array belongs.
    注意!这个索引不是对应值,而是一个区间

    bins是上升数组时:
    right=False —> bins[i-1] <= x < bins[i]
    right=True —> bins[i-1] < x <= bins[i]
    bins是下降数组时:
    right=False —> bins[i-1] > x >= bins[i]
    right=True —> bins[i-1] >= x > bins[i]

    x = np.array([0.2, 6.4, 3.0, 1.6])
    bins = np.array([0.0, 1.0, 2.5, 4.0, 10.0])
    inds = np.digitize(x, bins)
    inds
    array([1, 4, 3, 2])
    
    • 1
    • 2
    • 3
    • 4
    • 5

    3、np.array()

    创建数组

    a = np.array([2,3,4])
    b = np.array([2.0,3.0,4.0])
    c = np.array([[1.0,2.0],[3.0,4.0]])
    d = np.array([[1,2],[3,4]],dtype=complex) # 指定数据类型
    print a, a.dtype
    print b, b.dtype
    print c, c.dtype
    print d, d.dtype
    
    [2 3 4] int32
    [ 2.  3.  4.] float64
    [[ 1.  2.]
     [ 3.  4.]] float64
    [[ 1.+0.j  2.+0.j]
     [ 3.+0.j  4.+0.j]] complex128
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    shape[0]读取矩阵第一维度的长度

    .shape可以快速读取矩阵的形状

    4、np.squeeze(tmp_ts)

    从数组的形状中删除单维度条目,即把shape中为1的维度去掉

    numpy.squeeze(a,axis = None)
    1)a表示输入的数组;
    2)axis用于指定需要删除的维度,但是指定的维度必须为单维度,否则将会报错;
    3)axis的取值可为None 或 int 或 tuple of ints, 可选。若axis为空,则删除所有单维度的条目;
    4)返回值:数组
    5) 不会修改原数组;

    np.squeeze()函数可以删除数组形状中的单维度条目,即把shape中为1的维度去掉,但是对非单维的维度不起作用。
    在这里插入图片描述

    在这里插入图片描述

    5、增加维度

    增加某一维度,只需要加入None即可

    >>> a = np.array([2,3,4])
    >>> a.shape   
    (3,)
    >>> a = a[:,None] 
    >>> a.shape 
    (3,1)
    >>> a
    array([[2],
           [3],
           [4]])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    6、data_path[-1]

    data_path[-1]表示最后一位

    7、np.prod函数

    numpy.prod(a, axis=None, dtype=None, out=None, keepdims=)
    返回给定轴上的数组元素的乘积。
    在这里插入图片描述

  • 相关阅读:
    家电软装记录
    如何处理接口调用的频率限制
    Linux中输出输入重定向
    嵌入式学习笔记(25)串口通信的基本原理
    信号与系统 --- 复指数函数(个人学习笔记)
    FPGA学习笔记——知识点总结
    通过Java Reflection实现编译时注解处理
    IB课程四大要领,一起来学习学习
    ProcessState实例化过程讲解
    导入Excel文件的各种常见方法
  • 原文地址:https://blog.csdn.net/weixin_41169280/article/details/128044463