w3resource logo
Python Exercises

Python: Create random vector of size 15 and replace the maximum value by -1

Python NumPy: Random Exercise-11 with Solution

Write a Python program to create random vector of size 15 and replace the maximum value by -1.

Sample Solution :-

Python Code :

import numpy as np
x = np.random.random(15)
print("Original array:")
print(x)
x[x.argmax()] = -1
print("Maximum value replaced by -1:")
print(x)

Sample Output:

Original array:                                                        
[ 0.04921181  0.83545304  0.4394982   0.81889845  0.8022234   0.46176053                                                                      
  0.95785815  0.86968759  0.35100099  0.00107607  0.4330148   0.56632168                                                                      
  0.57764716  0.09226267  0.01710047]                                  
Maximum value replaced by -1:                                          
[ 0.04921181  0.83545304  0.4394982   0.81889845  0.8022234   0.46176053                                                                      
 -1.          0.86968759  0.35100099  0.00107607  0.4330148   0.56632168                                                                      
  0.57764716  0.09226267  0.01710047]

Python Code Editor:

import numpy as np
x = np.random.random(15)
print("Original array:")
print(x)
x[x.argmax()] = -1
print("Maximum value replaced by -1:")
print(x)

Improve this sample solution and post your code through Disqus