主要目標:假設我有一個多維陣列。我也有一個0-1的索引集,對應于每一行的每一列。例如,如果我的陣列是[[3,6,7,8], [1,32,45,7]],我將有一個索引集作為[[1,0,1,1], [0,0,1,1]]。我想把我的陣列的每一行復制n次。然后,我想隨機增加對應索引等于1的每個元素。
import time
import random
import numpy as np
def foo(arr, upper_bound, index_set, first_set_size, sec_set_size, limit)。
iter=0
my_array = np.zeros((first_set_size*sec_set_size, limit)) #每一行被復制|sec_set_size|次。
它=0。
for i in range(first_set_size)。
for j in range(sec_set_size) 。
my_array[it] = arr[i] #復制相應行的元素。
for k in range(limit)。
if index_set[i][k]==1: #update the elements whose indices are one.
temp = arr[i][k] #get the current value.
my_array[it][k] =temp random.randint(1,upper_bound-temp) #I use fastrand.pcg32bounded here. 更新值。
it =1。
return my_array
upper_bound =50
limit=1000
first_set_size=100
sec_set_size = 50 50
arr = np.random.randint(25, size=(first_set_size, limit)) #創建一個包含整數的陣列。
index_set= np.array([random.randint(0,1) for j in range( limit )] for i in range(first_set_size)]) #每個元素的索引都是1或0。
start_time = time.time() #衡量函式所花費的時間。
result = foo(arr, upper_bound,index_set, first_set_size, sec_set_size, limit)
print("time taken: %s"/span> % (time.time() - start_time))
一旦我增加了限制并設定了大小,代碼就需要幾分鐘。有什么方法可以讓我更快/更有效地執行這個操作嗎?我已經在這方面花了不少時間,但是無法提高我的執行速度。
編輯。 假設我的初始陣列是:
[[11 23 24 17 0]
[1 23 12 19 5]
[20 15 1 17 17]
[3 8 7 0 24] ]
另外,我的索引集是這樣的;
我的索引集是這樣的
[[1 0 0 0 1]
[1 0 1 0 0]
[1 1 1 1]
[0 1 0 1 1] ]
如果sec_set_size=5,我想取每一行的副本,并增加每個元素的值,如果它們的索引是1的話。
最終的結果應該是這樣的;
[[39. 23。 24. 17. 44.]
[50. 23. 24. 17. 27.]
[42. 23. 24. 17. 24.]
[45. 23. 24. 17. 11.]
[49. 23. 24. 17. 43.]
[23. 23. 44. 19. 5.]
[10. 23. 37. 19. 5.]
[14. 23. 29. 19. 5.]
[12. 23. 22. 19. 5.]
[5. 23. 15. 19. 5.]
[36. 45. 26. 37. 17.]
[24. 40. 35. 38. 17.]
[34. 20. 24. 31. 17.]
[27. 16. 9. 20. 17.]
[37. 37. 6. 37. 17.]
[3. 50. 7. 46. 47.]
[3. 13. 7. 37. 44.]
[3. 23. 7. 32. 29.]
[3. 10. 7. 22. 41.]
[3. 22. 7. 32. 41.] ]
uj5u.com熱心網友回復:
Numpy是關于矢量化的。如果你正在使用python回圈,你可能做錯了。
首先,所有的亂數生成器都是矢量化的:
index_set = np.random.randint(2, size=(first_set_size, limit), dtype=bool)
你在上面那行做得很正確。
接下來,要多次復制行,你可以使用np.repeat:
my_array = np.repeat(arr, sec_set_size, axis=0)
請注意,你根本不需要first_set_size。它與arr.shape[0]是多余的。你可以用你的布爾掩碼做同樣的事情來使形狀匹配:
index_set = np.repeater(index_set, sec_set_size, axis=0)
現在你可以用適當數量的隨機生成的元素來更新被index_set屏蔽的my_array的選項:
my_array[index_set] = np.random.randint(1, upper_bound - my_array[index_set] )
你的整個程式減少到大約四行(非常快),加上一些初始化:
def foo(arr, upper_bound, index_set, sec_set_size, limit)。
my_array = np.repeat(arr, sec_set_size, axis=0)
index_set = np.repeat(index_set, sec_set_size, axis=0)
my_array[index_set] = np.random.randint(1, upper_bound - my_array[index_set] )
return my_array
upper_bound = 50
limit=1000
first_set_size=100
sec_set_size = 50 50
arr = np.random.randint(25, size=(first_set_size, limit)) #創建一個包含整數的陣列。
index_set = np.random.randint(2, size=(first_set_size, limit), dtype=bool)
start_time = time.time() #measure the time taken by the function。
result = foo(arr, upper_bound, index_set, sec_set_size, limit)
print(f "耗時。{time.time() - start_time}")
你可能想嘗試使用索引而不是布爾掩碼。這將使索引的效率更高,因為非零元素的數量不需要重新計算兩次,但另一方面,設定的成本更高一些:
def foo(arr, upper_bound, index_set, sec_set_size, limit)。
my_array = np.repeat(arr, sec_set_size, axis=0)
r, c = np.where(index_set)
r = (sec_set_size * r[:, None] np.range(sec_set_size)).ravel()
c = np.repeat(c, sec_set_size)
my_array[r, c] = np.random.randint(1, upper_bound - my_array[r, c])
return my_array
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/309423.html
標籤:
