常用numpy整理(不期更新)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35323001/article/details/84167375

常用的提取矩阵特定行:
>>> x
array([[6.93528128e-310, 4.68328808e-310, 1.58101007e-322],
       [1.58101007e-322, 4.68328790e-310, 0.00000000e+000],
       [3.16202013e-322, 1.58101007e-322, 4.68328789e-310],
       [6.93528128e-310, 0.00000000e+000, 1.63041663e-322]])
>>> idx
array([ True,  True, False,  True])
>>> x = x[idx,:]
>>> x
array([[6.93528128e-310, 4.68328808e-310, 1.58101007e-322],
       [1.58101007e-322, 4.68328790e-310, 0.00000000e+000],
       [6.93528128e-310, 0.00000000e+000, 1.63041663e-322]])


>>> np.tile([0,0],(3,1))
array([[0, 0],
       [0, 0],
       [0, 0]])


>>> x = np.random.rand(3,1)
>>> x
array([[0.63202497],
       [0.88707272],
       [0.83418   ]])
>>> s = np.concatenate([x, -x],axis = 1)
>>> s
array([[ 0.63202497, -0.63202497],
       [ 0.88707272, -0.88707272],
       [ 0.83418   , -0.83418   ]])
>>> s = np.concatenate([x, -x],axis = 0)
>>> s
array([[ 0.63202497],
       [ 0.88707272],
       [ 0.83418   ],
       [-0.63202497],
       [-0.88707272],
       [-0.83418   ]])


>>> x = np.arange(10)
>>> x
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.clip(x,3,7)
array([3, 3, 3, 3, 4, 5, 6, 7, 7, 7])


>>> x = np.arange(10)
>>> x
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> s = np.random.shuffle(x)  # 无返回值
>>> s
>>> x
array([1, 5, 8, 0, 9, 4, 2, 7, 6, 3])
>>> y = np.arange(10)
>>> y
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> s = np.random.permutation(y)  # 返回打乱后的索引
>>> s
array([5, 7, 2, 8, 0, 4, 9, 1, 6, 3])
>>> y
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])


# 给np.array增加维度
>>> x = np.arange(10)
>>> x
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> x.ndim
1
>>> x = x[None, :]
>>> x
array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
>>> x.ndim
2

猜你喜欢

转载自blog.csdn.net/qq_35323001/article/details/84167375
今日推荐