[英]How can I create np.ndarray from four arrays of different lengths without using loops?
我有 5 個 arrays,我需要創建兩個大的 arrays 形狀 ((1000,5000)) 用於回歸分析。 我目前正在使用以下代碼。
# data shapes are as follows
bbeta.shape = ((5000,2))
rand1.shape = ((1000))
rand2.shape = ((1000))
rand3.shape = ((1000))
depths.shape = ((1000))
# creating x and y for regression
for i in np.arange(0,1000,1):
for j in np.arange(0,5000,1):
y[i,j] = np.log((rand1[i] * (depths[i]/3500)**bbeta[j,0]))
x[i,j] = np.log(rand2[i] + (bbeta[j,1] * rand3[i]))
這段代碼需要大約 30 秒才能運行,我猜這很好,但我需要運行 1000 多次才能找到引導程序標准錯誤,這意味着我的代碼目前需要超過 8 小時。 我已經顯着減少了創建數據集的數量(來自 bbeta.shape = ((17801,2)),但這還不夠。
If I can stick with numpy, or at least if it's faster to convert from and back to numpy, as the rest of my code is written using numpy.
我想知道是否有人知道一種方法可以做與上述相同但更快的事情,因為我知道循環的計算效率不高。 我查看了stackoverflow,但找不到任何人回答這個問題(至少以我可以識別的方式幫助我)。
抱歉,如果有任何不清楚的地方 - 我有閱讀障礙,並且對 python 很陌生。 任何人都可以給我的任何幫助將不勝感激
您可以利用廣播並執行以下操作:
y2 = np.power((depths / 3500)[:, np.newaxis], bbeta[:, 0])
y2 = np.log(rand1[:, np.newaxis] * y2)
x2 = bbeta[:, 1] * rand3[:, np.newaxis]
x2 = np.log(rand2[:, np.newaxis] + x2)
print(np.allclose(y, y2))
print(np.allclose(x, x2))
Output
True
True
使用以下代碼生成測試數據:
bbeta = np.random.random((5000, 2))
rand1 = np.random.random((1000))
rand2 = np.random.random((1000))
rand3 = np.random.random((1000))
depths = np.random.random((1000))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.