zoukankan      html  css  js  c++  java
  • numpy总结

    介绍

    numpy是一个功能强大的python库。机器学习中,需要对矩阵进行各种数值计算,numpy对其提供非常好的库,用于简单和快速计算。

    常用函数库

    1. 数组属性

      ndarray.ndim:秩,即轴的数量或维度的数量
      ndarray.shape:数组的维度,对于矩阵,n 行 m 列
      ndarray.size :数组元素的总个数,相当于 .shape 中 n*m 的值
      ndarray.dtype:ndarray 对象的元素类型

    2. numpy.array(object, dtype = None, copy = True, order = None, subok = False, ndmin = 0)

      功能:创建一个 ndarray(numpy数组)
      object:数组或嵌套的数列
      dtype:数组元素的数据类型,可选
      ndmin:指定生成数组的最小维度

    3. numpy.zeros(shape, dtype = float, order = 'C')

      功能:创建制定大小以0填充的numpy数组

    4. axis=0,表示沿着第 0 轴进行操作,即对每一列进行操作;axis=1,表示沿着第1轴进行操作,即对每一行进行操作。

    5. numpy.arange(start, stop, step, dtype)

      功能:根据 start 与 stop 指定的范围以及 step 设定的步长,生成一个 ndarray。
      左闭右开(遵循python特性)

    6. numpy运算函数

      numpy.sqrt/num/mean/power等等。

    7. numpy.mat(data, dtype=None)

      功能:将data转换为矩阵。numpy矩阵和numpy数组仅在一维是不一样,高维基本一样。一维矩阵[[1,2]]其shape为[1,2],对应一维数组为[1,2]其shape为[2],即一维

    8. np.random

      功能:随机数
      np.random.rand(x, y) 返回0~1随机值,数组大小为xy
      np.random.uniform(x, y) 返回区间[x, y) 随机值,注意与上一个区别!
      np.random.randn(x, y) 返回x
      y大小数组,每个随机数遵循标准正态分布
      np.random.randint(low, high, size) 返回[low, high)区间随机整数

    9. np.tile

      功能:将数组按指定维度方向复制
      np.tile(data, 2) 将数组data沿一维复制为原来的2倍
      np.tile(data, (2,1)) 将数组data沿一维复制为原来的1倍,沿2维复制为原来的2倍

    10. np.argsort

      功能:将数组按指定维度方向排序,返回的是数组值从小到大的索引值
      numpy.argsort(a, axis=-1, kind='quicksort', order=None)

    11. np,isnan(data)

      判断data中是否含有nan
      np.isnan(data).sum() 找出nan的个数
      np.where(np.isnan(data)) 定位nan位置

    12. np.argwhere(a>5)
      定位符合条件的元素位置

    array与matrix区别

    1. matrix必须输二维的,而array可以是任意维度;所以matrix是array的一个小分支

    2. matrix的优势:两个matrix相乘可直接用*符号,及代表矩阵相乘,无需array中的dot()

    numpy中特殊查询

    print((a==b).all())   #判断两个array是否完全相同
    
    print(np.argwhere(a!=b))   #找出a、b两个array不相同的元素位置
    
    print(np.nonzero(a))   #找出a数组中非零元素索引
    
    np.any(a == 0)  #找出数组a中是否含有0
    
    np.all(a == 0) #找出数组a是否为0矩阵
    
  • 相关阅读:
    Intent
    What should we do next in general after collecting relevant data
    NOTE FOR Secure Friend Discovery in Mobile Social Networks
    missing pcap.h
    after building Android Source code
    plot point(one column)
    When talking to someone else, don't infer that is has been talked with others at first. It may bring repulsion to the person who is talking with you.
    进程基本知识
    Python input和raw_input的区别
    强制 code review:reviewboard+svn 的方案
  • 原文地址:https://www.cnblogs.com/wujingqiao/p/10477456.html
Copyright © 2011-2022 走看看