美文网首页
numpy,scipy相关

numpy,scipy相关

作者: Dorrrris | 来源:发表于2019-11-19 10:39 被阅读0次
    1. np.ones_like
      Return an array of ones with the same shape and type as a given array.
      类似的还有np.zeros_like; np.empty_like
    2. np..array
      data[0:3]:是指从下标为0的元素开始 取3-0个
      data[2:6]:是指从下标为2的元素开始 取6-2个
    >>> data = np.array([1, 9, 7, 4, 5, 6])
    >>> data[0:3]
    array([1,9,7])
    >>> data[2:6]
    array([7, 4, 5, 6])
    
    1. scipy.sparse.csr_matrix
      采取按行压缩的办法, 将原始的矩阵用三个数组进行表示。
      也可以是 data,(row,col)这样 在新的matrix当中指定位置赋值为data中的值。我只做过binarize情况的。比如建立click history matrix的时候。
    >>> indptr = np.array([0, 2, 3, 6])
    >>> indices = np.array([0, 2, 2, 0, 1, 2])
    >>> data = np.array([1, 2, 3, 4, 5, 6])
    >>> csr_matrix((data, indices, indptr), shape=(3, 3)).toarray()
    array([[1, 0, 2],
           [0, 0, 3],
           [4, 5, 6]])
    
    1. np.ceil
      函数返回输入值的上限
    >>> n = np.array([-1.7, -2.5, -0.2, 0.6, 1.2, 2.7, 11])
    >>> ceil = np.ceil(n)
    [ -1.  -2.  -0.   1.   2.   3.  11.]
    
    1. 查看list的维度
      必须转化为numpy array的形式,然后用x.shape查看
    >>> aa=[[1, 4], [2, 5], [3, 6]]
    >>> aa_a=np.array(aa)
    >>> print(aa_a.shape)
    (4, 3)
    
    1. np.all, np.any
      用来检查矩阵是否为0矩阵,是否为0元素
    >>> a2
    array([[0, 2, 3, 4],
           [2, 3, 4, 5],
           [3, 4, 5, 6],
           [4, 5, 6, 7]])
    #判断矩阵中是否有0元素
    >>> np.any(a2 == 0)
    #判断是否是0矩阵
    True
    >>> np.all(a2 == 0)
    False
    
    1. 关于0/0 RuntimeWarning: invalid value encountered in true_divide
    import numpy as np
    np.seterr(divide='ignore', invalid='ignore')
    
    1. 遇到array当中有nan值,导致运算出现问题
      想法是 把nan填充为一个具体的值。参考https://blog.csdn.net/lanchunhui/article/details/80399681

    相关文章

      网友评论

          本文标题:numpy,scipy相关

          本文链接:https://www.haomeiwen.com/subject/hstwictx.html