[英]Numpy: Fastest way to change 10% of all matrix elements
我有一個大小為mxn
的矩陣M
。 目前,如果我想更改流程中所有矩陣元素的 10%,我會執行以下操作:
M = np.ones((m, n))
for _ in range(999999999):
M = M + (np.random.random(M.shape) < 0.1) * np.random.random(M.shape)
# do stuff with M
然而,這種方法在多次迭代和大型矩陣時真的很慢,並且當我真的只需要0.1*m*n
時需要m*n
隨機數。
如何更快地執行上述操作?
如果您的用例可以容忍大約 10% 的值,您可以使用隨機索引更改這些值:
import numpy as np
a = np.ones((10,10))
a[np.random.rand(*a.shape)>=0.9] = 0.
a
array([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[0., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 0., 1., 1., 1., 1., 1.],
[1., 1., 1., 0., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 0., 0., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 0., 1., 1., 0., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 0., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 0., 1.]])
如您所見,您將更改大約 10 個值。
我給了這個破解,並想出了一種在我的筆記本電腦上執行速度更快的方法。 不過我沒有去 1B 迭代!
最佳表演者:
%timeit advancedSlice()
9.56 s ± 12.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
def advancedSlice():
M = np.arange(12000000.0).reshape(1200,10000)
rows = M.shape[0]
cols = M.shape[1]
sampleSize = M.size//10
for _ in range(100):
M[np.random.randint(rows, size=sampleSize), np.random.randint(cols, size=sampleSize),] += np.random.random(sampleSize)
return M
最佳表演者第二名:使用邁克爾的方法
%timeit advancedSlice2()
22.8 s ± 11.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
def advancedSlice2():
M = np.arange(12000000.0).reshape(1200,10000)
for _ in range(100):
randIndecies =np.random.rand(*M.shape)>=0.9
M[randIndecies] += np.random.random(randIndecies[randIndecies==True].size)
return M
原始代碼:
%timeit randMask()
33.2 s ± 211 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
def randMask():
M = np.arange(12000000.0).reshape(1200,10000)
for _ in range(100):
M = M + (np.random.random(M.shape) < 0.1) * np.random.random(M.shape)
return M
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.