我有這個風力數據集,它包含m/s的風速,我想計算時間序列中非零數據的週期。np.where()不返回numpy python中的預期索引
非零數據的每個週期都會計爲一次「天氣事件」。
我也想知道那些事件在數據集(即指數)內的位置。要做到這一點
的一種方法是計算各組非零數據的前第一個0的系列,以確定事件的數量,然後通過一個將每個索引值,以獲得事件的位置。
# create mock data.
d=np.zeros([209])
d1=np.random.randn(189)
d2=np.zeros(9)
d3=np.random.randn(281)
d4=np.zeros(27)
d5=np.random.randn(21)
d6=np.zeros(155)
d7=np.random.randn(58)
mock_data=np.concatenate((d,d1,d2,d3,d4,d5,d6,d7),axis=0)
indices=np.squeeze(np.array(np.where(mock_data!=0))) # Returns the position vector of every non-zero in the record.
# create a vector to store the positions of the previous zero before each SAW event.
dummy=np.zeros(0)
for i in range(len(indices)):
dummy=np.append(dummy,indices[i]-1)
dummy=dummy.astype(int)
dummy=np.int64(dummy)
zerovalue=np.squeeze(np.array(np.where(mock_data[dummy]==0))) # Should return the position of the previous zero before each SAW event.
# Add 1 to each value in zerovalue
new_index=np.zeros(0)
for i in range(len(zerovalue)):
new_index=np.append(date_index,zerovalue[i]+1)
但是,我遇到了問題np.where()
沒有返回我期待的指數。它不是返回指示非零數據組的第一個值在哪裏的索引,而是返回看似隨機的索引。
例如,第一個索引應該是209,但我得到0.任何幫助,非常感謝。
你總是第一個索引越來越0?你有什麼其他價值? –