我試圖執行一些函數來從一組衛星圖像中獲取一些結果(在示例中我正在執行相似函數)。我首先打算同時遍歷所有像素,每個像素包含4個數字,然後根據這些數字計算每個像素的值,然後將其寫入數組,例如scipy.spatial.distance.correlation(pixels_0,pixels_1)。對兩個3D數組中的數據進行迭代python
我遇到的問題是當我運行這個循環時,我遇到了問題,將它保存到數組1000x1000,併爲每個像素賦予一個值。
array_0 = # some array with dimensions(1000, 1000, 4)
array_1 = # some array with dimensions(1000, 1000, 4)
result_array = []
for rows_0, rows_1 in itertools.izip(array_0, array_1):
for pixels_0, pixels_1 in itertools.izip(rows_0, rows_1):
results = some_function(pixels_0, pixels_1)
print results
>>> # successfully prints desired results
results_array.append(results)
>>> # unsuccessful in creating the desired array
我得到我想要得到印刷向下運行窗口中的結果,但我不知道怎麼把它恢復到一個數組,我可以以類似的莊園操縱。我的for循環的問題,或者這是一個簡單的問題,將其附加到數組?任何關於加速它的解釋也會很棒,因爲我對python和編程都很陌生。
a = np.random.rand(10, 10, 4)
b = np.random.rand(10, 10, 4)
def dotprod(T0, T1):
return np.dot(T0, T1)/(np.linalg.norm(T0)*np.linalg.norm(T1))
results =dotprod(a.flatten(), b.flatten())
results = results.reshape(a.shape)
這現在造成ValueError異常:新的數組的總大小必須保持不變, 和印刷第一結果值時,收到只有一個號碼。這是我自己構造不好的功能或我如何使用numpy的錯?
我喜歡矢量思維方式,因爲其中一個操作是找到兩個矢量之間的角度:我爲它寫了一個簡單函數def'dotprod(T0,T1):' 'return np.dot(T0,T1 )/(np.linalg.norm(T0)* np.linalg.norm(T1))'並替換'some_function',但輸出是單個數字而不是數組。這是關於重做的功能,而不是你通過數據集中的數字的方式? – nhawkins