Change language

# Numpy MaskedArray.argsort () Function | python

| | |

In many cases, datasets can be incomplete or corrupted due to the presence of incorrect data. For example, a sensor may not write data or write an invalid value. The ` numpy.ma ` module provides a convenient way to solve this problem by introducing masked arrays. Masked Arrays — these are arrays that can contain missing or invalid entries.
`numpy.MaskedArray.argsort()` returns ndarray of indices that sort the array along the specified axis. Masked values ​​are pre-filled to fill_value.

Syntax: ` numpy.MaskedArray.argsort (axis = None, kind = ’quicksort’, order = None, endwith = True, fill_value = None) `

Parameters:
axis: [None, integer] Axis along which to sort ... If None, the default, the flattened array is used.
kind: [’quicksort’, ’mergesort’, ’heapsort’] Sorting algorithm. Default is ’quicksort’.
order: [list, optional] When a is an array with fields defined, this argument specifies which fields to compare first, second, etc.
endwith: [True, False, optional] Whether missing values ​​(if any) should be treated as the largest values ​​(True) or the smallest values ​​(False) When the array contains unmasked values ​​at the same extremes of the datatype, the ordering of these values ​​and the masked values ​​are undefined.
fill_value: [var, optional] Value used to fill in the masked values. If None, the output of minimum_fill_value (self._data) is used instead.

Return: [ndarray, int] Array of indices that sort a along the specified axis.

Code # 1:

 ` # Python program explaining ` ` # numpy.MaskedArray.argsort () method `   ` # import numy as geek ` ` # and numpy.ma module as ma ` ` import ` ` numpy as geek ` ` import ` ` numpy.ma as ma `   ` # create input array ` ` in_arr ` ` = ` ` geek.array ([` ` 4 ` `, ` ` 2 ` `, ` ` 3 ` `, ` ` - ` ` 1 ` `, ` ` 5 ` `]) ` ` print ` ` (` ` "Input array:" ` `, in_arr) `   ` # We now create a masked array ` ` # invalidating the third entry. ` ` mask_arr ` ` = ` ` ma.masked_array (in_arr, mask ` ` = ` ` [` ` 0 ` `, ` ` 0 ` `, ` ` 1 ` `, ` ` 0 ` `, ` ` 0 ` `]) ` ` print ` ` (` ` "Masked array:" ` `, mask_arr) `   ` # using MaskedArray.argsort methods to mask the array ` ` out_arr ` ` = ` ` mask_arr.argsort () ` ` print ` ` (` `" output array of indices: "` `, out_arr) `

Output:

` Input array: [4 2 3 -1 5] Masked array: [4 2 - -1 5] output array of indices: [3 1 0 4 2] `

Code # 2:

` `

` # Python program explaining # numpy.MaskedArray.argsort () method   # import numy as geek # and numpy.ma module as ma import numpy as geek import numpy. ma as ma   # create input array in_arr = geek.array ([ 5 , - 5 , 0 , - 10 , 2 ]) print ( "Input array:" , in_arr)   # Now we create a masked array # making the first third entry invalid. mask_arr = ma.masked_array (in_arr, mask = [ 1 , 0 , 1 , 0 , 0 ]) print ( "Masked array:" , mask_arr)   # using MaskedArray.argminmethods to mask the array # and fill the masked space by 1 out_arr = mask_arr.argsort (fill_value = 1 ) print ( "output array of indices:" , out_arr) `

` ` Exit:

` Input array: [5 -5 0 -10 2] Masked array: [- -5 - -10 2] output array of indices: [3 1 0 2 4] `

## Shop

Learn programming in R: courses

\$

Best Python online courses for 2022

\$

Best laptop for Fortnite

\$

Best laptop for Excel

\$

Best laptop for Solidworks

\$

Best laptop for Roblox

\$

Best computer for crypto mining

\$

Best laptop for Sims 4

\$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

NUMPYNUMPY

glob exclude pattern

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

NUMPYNUMPY

Python CSV error: line contains NULL byte

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

## Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries