Numpy:将单个元素广播到 len n 数组

假设我有一个数据框。

此数据框具有字符串内容。我需要执行以下操作:

for i in range(0,100000):
   label = raw.values[i,3]
   stackable = np.asarray([label for i in range(40)]).reshape((-1,1))
   data.append(np.hstack(some_other_data,stackable))

这是我正在尝试做的一个小例子,它在 python 中需要很长时间。使用 numpy 执行此操作的“正确”方法是什么?


肥皂起泡泡
浏览 150回答 2
2回答

浮云间

full应该是在数组中“复制”字符串的快速(错误)方式:In [84]: np.full(4, 'label')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Out[84]: array(['label', 'label', 'label', 'label'], dtype='<U5')In [85]: alist = []&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;In [86]: labels = ['one','two','three']&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;In [87]: for i in range(3):&nbsp;&nbsp; &nbsp; ...:&nbsp; &nbsp; &nbsp;temp=np.full(3,labels[i])&nbsp;&nbsp; &nbsp; ...:&nbsp; &nbsp; &nbsp;alist.append(temp)&nbsp;&nbsp; &nbsp; ...:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;In [88]: alist&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Out[88]:&nbsp;[array(['one', 'one', 'one'], dtype='<U3'),&nbsp;array(['two', 'two', 'two'], dtype='<U3'),&nbsp;array(['three', 'three', 'three'], dtype='<U5')]In [89]: np.array(alist)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Out[89]:&nbsp;array([['one', 'one', 'one'],&nbsp; &nbsp; &nbsp; &nbsp;['two', 'two', 'two'],&nbsp; &nbsp; &nbsp; &nbsp;['three', 'three', 'three']], dtype='<U5')或列表理解:In [91]: np.array([np.full(3,l,'U7') for l in labels])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Out[91]:&nbsp;array([['one', 'one', 'one'],&nbsp; &nbsp; &nbsp; &nbsp;['two', 'two', 'two'],&nbsp; &nbsp; &nbsp; &nbsp;['three', 'three', 'three']], dtype='<U7')

守候你守候我

更换stackable = np.asarray([label for i in range(40)]).reshape((-1,1))和stackable = np.asarray([label]*40).reshape((-1,1))在我的机器上获得 10-20% 的性能提升。可能还有更多可以做的事情,但是如果不了解更多关于data和就很难说some_other_data。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python