w3resource

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

NumPy: Random Exercise-11 with Solution

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

Sample Solution:

Python Code :

# Importing the NumPy library as np
import numpy as np

# Generating an array 'x' with 15 random floats between 0 and 1 using np.random.random()
x = np.random.random(15)

# Displaying the original array 'x'
print("Original array:")
print(x)

# Finding the index of the maximum value in the array using x.argmax()
# Replacing the maximum value with -1 using the index obtained from x.argmax()
x[x.argmax()] = -1

# Displaying the modified array where the maximum value has been replaced by -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]

Explanation:

In the above exercise –

x = np.random.random(15): This line creates a 1D array x with 15 random floating-point values between 0 and 1 using the np.random.random() function.

x[x.argmax()] = -1: This line first finds the index of the maximum value in the array x using the argmax() method. Then, it replaces the maximum value in the array with -1 using array indexing.

print(x): This line prints the modified array x with the maximum value replaced by -1.

When you run the code, you'll get an output similar to this (note that the actual output will vary since the array is generated randomly):

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]

The output shows the modified array x with the maximum value replaced by -1.

Pictorial Presentation:

NumPy Random: Create random vector of size 15 and replace the maximum value by -1.

Python-Numpy Code Editor:

Previous: Write a NumPy program to check two random arrays are equal or not.
Next: Write a NumPy program to find point by point distances of a random vector with shape (10,2) representing coordinates.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.