Numpy thermometer encoding

后端 未结 4 1652
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-06 00:28

I am trying to use numpy optimized in-built functions to generate thermometer encoding. Thermometer encoding is basically generating n amount if 1\'s in a given len

4条回答
  •  长情又很酷
    2021-01-06 00:43

    not much different, listcomp inside an array creation function

    temps = [1,2,4,1]
    tlen = 8
    np.stack([np.pad(np.ones(t), (0, tlen-t), 'constant') for t in temps])
    
    Out[66]: 
    array([[ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
           [ 1.,  1.,  0.,  0.,  0.,  0.,  0.,  0.],
           [ 1.,  1.,  1.,  1.,  0.,  0.,  0.,  0.],
           [ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])
    

提交回复
热议问题