- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Clip (limit) the values in a Numpy array
To clip (limit) the values in an array, use the np.ma.clip() method in Python Numpy. Given an interval, values outside the interval are clipped to the interval edges. For example, if an interval of [0, 1] is specified, values smaller than 0 become 0, and values larger than 1 become 1. Equivalent to but faster than np.minimum(a_max, np.maximum(a, a_min)).
The out is where the results will be placed in this array. It may be the input array for in-place clipping. out must be of the right shape to hold the output. Its type is preserved.
The function returns an array with the elements of a, but where values < a_min are replaced with a_min, and those > a_max with a_max.
Steps
At first, import the required library −
import numpy as np
Create an array with int elements using the numpy.array() method −
arr = np.array([25, 32, 38, 47, 53, 66, 73, 79, 88, 95, 108]) print("Array...
", arr)
Create a masked array and mask some of them as invalid −
maskArr = ma.masked_array(arr, mask =[0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0]) print("
Our Masked Array...
", maskArr)
Get the type of the masked array −
print("
Our Masked Array type...
", maskArr.dtype)
Get the dimensions of the Masked Array −
print("
Our Masked Array Dimensions...
",maskArr.ndim)
Get the shape of the Masked Array −
print("
Our Masked Array Shape...
",maskArr.shape)
Get the number of elements of the Masked Array −
print("
Number of elements in the Masked Array...
",maskArr.size)
To clip (limit) the values in an array, use the np.ma.clip() method in Python Numpy −
print("
Result..
.", np.ma.clip(maskArr, 50, 80 ))
Example
import numpy as np import numpy.ma as ma # Create an array with int elements using the numpy.array() method arr = np.array([25, 32, 38, 47, 53, 66, 73, 79, 88, 95, 108]) print("Array...
", arr) # Create a masked array and mask some of them as invalid maskArr = ma.masked_array(arr, mask =[0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0]) print("
Our Masked Array...
", maskArr) # Get the type of the masked array print("
Our Masked Array type...
", maskArr.dtype) # Get the dimensions of the Masked Array print("
Our Masked Array Dimensions...
",maskArr.ndim) # Get the shape of the Masked Array print("
Our Masked Array Shape...
",maskArr.shape) # Get the number of elements of the Masked Array print("
Number of elements in the Masked Array...
",maskArr.size) # To clip (limit) the values in an array, use the np.ma.clip() method in Python Numpy print("
Result..
.", np.ma.clip(maskArr, 50, 80 ))
Output
Array... [ 25 32 38 47 53 66 73 79 88 95 108] Our Masked Array... [25 -- 38 47 -- 66 73 79 88 -- 108] Our Masked Array type... int64 Our Masked Array Dimensions... 1 Our Masked Array Shape... (11,) Number of elements in the Masked Array... 11 Result.. . [50 -- 50 50 -- 66 73 79 80 -- 80]
- Related Articles
- Clip (limit) the values in an array and place the result in another array in Numpy
- Return an array formed from the elements of a masked array but clip the range in NumPy
- Use an index array to construct a new array from a set of choices with clip mode in Numpy
- Encode string array values in Numpy
- Set storage-indexed locations to corresponding values and clip out-of-bounds indices to range in Numpy
- Append values to the end of a masked array in Numpy
- Test array values for finiteness in Numpy
- Test array values for NaN in Numpy
- Return array of indices of the maximum values from a masked array in NumPy
- Return array of indices of the minimum values from a masked array in NumPy
- Test array values for NaT (not a time) in Numpy
- Checking if all array values are smaller than a limit using JavaScript
- Python – Limit the values to keys in a Dictionary List
- Return the fractional and integral parts of array values in Numpy
- Copy values from one array to another in Numpy
