一个带参数的解决方案,比绿色勾选的方式更易理解。只有最后一行对操作很重要。
import numpy
import random
n1 = 5
n2 = 5
r = 0.7
random.seed(1)
a = numpy.array([[0 if random.random() > r else 1 for _ in range(n1)]])
n3 = numpy.count_nonzero(a)
b = numpy.array([[random.randint(1,9) for _ in range(n3)] for _ in range(n2)])
c = numpy.zeros((n2, n1))
c[:, numpy.where(a)[1]] = b[:]
结果:
a = array([[1, 0, 0, 1, 1]])
b = array([[8, 8, 7],
[4, 2, 8],
[1, 7, 7],
[1, 8, 5],
[4, 2, 6]])
c = array([[8., 0., 0., 8., 7.],
[4., 0., 0., 2., 8.],
[1., 0., 0., 7., 7.],
[1., 0., 0., 8., 5.],
[4., 0., 0., 2., 6.]])
在这里,您的时间处理取决于n值:
![enter image description here](https://istack.dev59.com/xRDFE.webp)
使用以下代码:
import numpy
import random
import time
import matplotlib.pyplot as plt
n1 = 5
n2 = 5
r = 0.7
def main(n1, n2):
print()
print(f"{n1 = }")
print(f"{n2 = }")
random.seed(1)
a = numpy.array([[0 if random.random() > r else 1 for _ in range(n1)]])
n3 = numpy.count_nonzero(a)
b = numpy.array([[random.randint(1,9) for _ in range(n3)] for _ in range(n2)])
t0 = time.time()
c = numpy.zeros((n2, n1))
c[:, numpy.where(a)[1]] = b[:]
t = time.time() - t0
print(f"{t = }")
return t
t1 = [main(10**i, 10) for i in range(1, 8)]
t2 = [main(10, 10**i) for i in range(1, 8)]
plt.plot(t1, label="n1 time process evolution")
plt.plot(t2, label="n2 time process evolution")
plt.xlabel("n-values (log)")
plt.ylabel("Time processing (s)")
plt.title("Insert columns into a numpy array based on mask")
plt.legend()
plt.show()