[英]Appending to a numpy array
我有以下代碼:
result = np.zeros((samples,), dtype=[('time', '<f8'), ('data', '<f8', (datalen,))])
我想創建一個變量tempresult
來累積數據result
,並且一旦我累積了25000個樣本,我想對其執行一些操作。
所以我想做些類似的事情:
result = np.zeros((samples,), dtype=[('time', '<f8'), ('data', '<f8', (datalen,))])
tempresult.append(result)
if ( len(tempresult[0] > 25000 )):
# do something
我嘗試了答案代碼,但出現異常TypeError:類型升級無效
result = np.zeros((samples,), dtype=[('time', '<f8'), ('data', '<f8', (datalen,))])
if self.storeTimeStamp:
self.storeTimeStamp = False
self.timestamp = message.timestamp64
self.oldsamples = 0
for sample in range(0, samples):
sstep = self.timestamp + (self.oldsamples + sample) * step
result[sample] = (sstep, data[sample])
self.oldsamples = self.oldsamples + samples
# append
np.append(self.tempresult, result)
if len(self.tempresult) < 25000:
return
return [self.tempresult]
1)閱讀np.append
文檔。
np.append(self.tempresult, result)
是錯的。 np.append
返回一個新數組; 它的作用不像列表追加那樣。
2) np.append
是笨拙接口np.concatenate
。 如果你不懂concatenate
,就會被append
搞砸。
3)因為每次都會創建一個新數組,所以重復的連接速度很慢。 收集數組列表並在最后進行一個連接要快得多
4)使用復合dtype
,所有要concatenate
輸入必須具有相同的dtype
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.