numpy小技巧

问题1: 选取二维数组中的若干行与列的交叉点

例如:

import numpy as np

a = np.arange(20).reshape((5,4))
# array([[ 0,  1,  2,  3],
#        [ 4,  5,  6,  7],
#        [ 8,  9, 10, 11],
#        [12, 13, 14, 15],
#        [16, 17, 18, 19]])
# select certain rows(0, 1, 3) AND certain columns(0, 2)

解答见 https://stackoverflow.com/questions/22927181/selecting-specific-rows-and-columns-from-numpy-array

Using ix_ one can quickly construct index arrays that will index the cross product. a[np.ix_([1,3],[2,5])] returns the array [[a[1,2] a[1,5]], [a[3,2] a[3,5]]].

>>> a = np.arange(20).reshape((5,4))
>>> a[np.ix_([0,1,3], [0,2])]
array([[ 0,  2],
       [ 4,  6],
       [12, 14]])

问题2: One Hot encodings

所谓'One Hot encodings', 是将多类问题的向量变化为0-1矩阵:


image.png

定义以下函数即可:

import numpy as np
def convert_to_one_hot(Y, C):
    """
    Y是一个numpy.array, C是分类的种数
    """
    Y = np.eye(C)[Y.reshape(-1)].T
    return Y

y = np.array([[1, 2, 3, 0, 2, 1]])
print(y.shape)
print(y.reshape(-1).shape)
C = 4
print(convert_to_one_hot(y, C))

np.eye(C)是构造一个对角线为1的对角矩阵, Y.reshape(-1)把Y压缩成向量[numpy中向量shape是(n,), 矩阵shape是(1, n)],np.eye(C)[Y.reshape(-1)]的意思是取对角矩阵的相应行, 最后.T做转置, 就获得了下面的结果:

(1, 6)
(6,)
[[ 0.  0.  0.  1.  0.  0.]
 [ 1.  0.  0.  0.  0.  1.]
 [ 0.  1.  0.  0.  1.  0.]
 [ 0.  0.  1.  0.  0.  0.]]

参考文献
[1] https://stackoverflow.com/questions/22927181/selecting-specific-rows-and-columns-from-numpy-array
[2] https://docs.scipy.org/doc/numpy-1.13.0/user/basics.indexing.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 先决条件 在阅读这个教程之前,你多少需要知道点python。如果你想从新回忆下,请看看Python Tutoria...
    舒map阅读 2,599评论 1 13
  • NumPy是Python中关于科学计算的一个类库,在这里简单介绍一下。 来源:https://docs.scipy...
    灰太狼_black阅读 1,250评论 0 5
  • 来源:NumPy Tutorial - TutorialsPoint 译者:飞龙 协议:CC BY-NC-SA 4...
    布客飞龙阅读 32,974评论 6 98
  • 汤之《盘铭》曰:“苟日新,日日新,又日新。”《康诰》曰:“作新民”。《诗》曰:“周虽旧邦,其命惟新。”是...
    钱江潮369阅读 412评论 0 1
  • 熊,上图是我想送给你的话。当然,也同样送给你们~ 这次,我讲述的是一个我刚认识一个月的男生。 他叫熊。 我认为对于...
    __Diana__阅读 617评论 0 1