banner



How To Change Values In An Array Python

NumPy Supercede Values

  1. NumPy Supervene upon Values With the numpy.clip() Function
  2. NumPy Replace Values With the numpy.minimum() and numpy.maximum() Functions
  3. NumPy Supervene upon Values With the Assortment Indexing Method in Python

This tutorial volition introduce how to replace values inside a NumPy array in Python.

NumPy Replace Values With the numpy.clip() Function

If we need to supplant all the greater values than a certain threshold in a NumPy array, we tin can utilise the numpy.clip() function. We tin specify the upper and the lower limits of an array using the numpy.clip() function. The numpy.prune() function returns an array where the elements less than the specified limit are replaced with the lowest limit. The elements greater than the specified limit are replaced with the greatest limit. The post-obit code example shows us how to supplant values within a NumPy assortment with the numpy.clip() function.

              import numpy as np  array = np.array([1,2,iii,4,5,5,6,7,8,8,9,9])  result = np.clip(array, 0, five) print(result)                          

Output:

              [1 2 three 4 five 5 5 5 5 5 5 5]                          

We replaced the values greater than 5 inside the NumPy array assortment with the np.clip() function in the above code. We offset created a NumPy array with the np.array() part. We and so clipped the assortment by specifying a limit from 0 to 5 within the np.clip() function and saved the consequence inside the result array.

NumPy Replace Values With the numpy.minimum() and numpy.maximum() Functions

We tin as well use the numpy.minimum() and the numpy.maximum() functions to supervene upon values in an assortment outside our specified limit. The numpy.maximum() part is used to replace the values less than the lower limit with the lower limit. And the numpy.minimum() function is used to replace values greater than the upper limit with the upper limit. The numpy.maximum() office takes the array and the lowest possible value as input parameters. The numpy.minimum() office takes the assortment and the greatest possible value as input parameters. See the following code example.

              import numpy equally np  assortment = np.assortment([1,2,three,4,5,5,6,seven,8,viii,9,9])  result1 = np.minimum(array, 5)  result2 = np.maximum(result1,0) print(result2)                          

Output:

              [1 2 three four 5 five 5 v v v v 5]                          

We replaced the values greater than 5 with 5 by using the np.minimum() function and the values less than 0 with 0 by using the np.maximum() role. We stored the result of these operations within the result2 assortment.

NumPy Supersede Values With the Array Indexing Method in Python

The simplest mode of achieving the same goal as the previous 2 methods is to use the assortment indexing in Python. Nosotros tin can easily supersede values greater than or less than a certain threshold with the array indexing method in NumPy. Rather than creating a new assortment like the previous 2 methods, this method modified the contents of our original array.

              import numpy as np  array = np.array([1,2,three,4,5,5,6,7,8,8,9,9])  array[array > 5] = 5 impress(assortment)                          

Output:

              [1 2 3 4 5 five 5 five 5 5 5 5]                          

We replaced all the values inside the assortment greater than five with v by using assortment[array > 5] = 5 in Python.

Write for us

DelftStack articles are written by software geeks similar y'all. If you as well would like to contribute to DelftStack past writing paid articles, you can check the write for usa folio.

Ezoic

Source: https://www.delftstack.com/howto/numpy/python-numpy-replace-values/

Posted by: helgesonafror1942.blogspot.com

0 Response to "How To Change Values In An Array Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel