GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • w3resource
  • Front End
  • HTML
  • CSS
  • JavaScript
  • HTML5
  • Schema.org
  • php.js
  • Twitter Bootstrap
  • Responsive Web Design tutorial
  • Zurb Foundation 3 tutorials
  • Pure CSS
  • HTML5 Canvas
  • JavaScript Course

This view

  • Original page
  • New address

GeezerVison is showing www.w3resource.com — NumPy: Create random vector of size 15 and replace the maximum value by -1 - w3resource

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

Last update on August 30 2025 12:29:06 (UTC/GMT +8 hours)


11. Replace Maximum in Vector with -1

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.

For more Practice: Solve these Related Problems:

  • Write a function that finds the maximum value in a 1D array and replaces it with -1 using np.argmax.
  • Create a solution that replaces all occurrences of the maximum value in the array with -1.
  • Implement a method that verifies the new array by checking that the maximum value is no longer present.
  • Test the function on arrays containing negative, zero, and positive values to ensure correctness.

Go to:

  • NumPy Random Exercises Home ↩
  • NumPy Exercises Home ↩

PREV : Check Array Equality.
NEXT : Point-to-Point Distances in 10x2 Coordinates.

Python-Numpy Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

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.


  • Daily Coding Challenges & Projects
  • Weekly Trends and Language Statistics



You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.