Q:

Write a NumPy program to place a specified element in specified time randomly in a specified 2D array

0

Write a NumPy program to place a specified element in specified time randomly in a specified 2D array.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import numpy as np
n = 4
i = 3
e = 10
array_nums1 = np.zeros((n,n))
print("Original array:")
print(array_nums1)
np.put(array_nums1, np.random.choice(range(n*n), i, replace=False), e)
print("\nPlace a specified element in specified time randomly:")
print(array_nums1)

Sample Output:

Original array:
[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]

Place a specified element in specified time randomly:
[[10.  0.  0.  0.]
 [10.  0.  0.  0.]
 [ 0.  0.  0. 10.]
 [ 0.  0.  0.  0.]]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now