numpy学习

Numpy 学习记录

np.where()用法

  1. np.where(condition, x, y),满足条件(condition),输出x,不满足输出y。
    1
    2
    3
    4
    5
    6
    >>> np.where([[True,False], [True,True]],
    [[1,2], [3,4]],
    [[9,8], [7,6]])

    输出:array([[1, 8],
    [3, 4]])

那么第一列是在1和9之间选择,为True所以选1。 第二列在2和8之间选择,因为是False所以选8,后面的以此类推

  1. np.where(condition)
    1
    2
    3
    4
    5
    6
    7
    8
    >>> a = np.array([2,4,6,8,10])
    >>> np.where(a > 5) # 返回索引
    (array([2, 3, 4]),)
    >>> a[np.where(a > 5)] # 等价于 a[a>5]
    array([ 6, 8, 10])

    >>> np.where([[0, 1], [1, 0]])
    (array([0, 1]), array([1, 0])) # 输出的为两个1的坐标

np.zero_like()

np.zeros_like(W);其维度与矩阵W一致,并为其初始化为全0;这个函数方便的构造了新矩阵,无需参数指定shape大小;

np.unique()

https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.unique.html
去重,并返回小到大排序后的数组

0%