在許多情況下,數據集可能不完整或被無效數據汙染。例如,傳感器可能無法記錄數據或記錄了無效值。的numpy.ma
模塊通過引入掩碼數組提供了解決此問題的便捷方法。掩碼數組是可能缺少條目或無效條目的數組。
numpy.MaskedArray.argmin()函數返回沿給定軸的最小值的索引數組。掩碼的值將被視為具有值fill_value。
用法: numpy.MaskedArray.argmin(axis=None, fill_value=None, out=None)
參數:
axis :[None,integer]如果為None,則索引將進入展平的數組,否則沿指定的軸。
fill_value :[var,可選]用於填充掩碼值的值。
out :[ndarray,可選]將結果存儲到的位置。
->如果提供,則必須具有廣播輸入的形狀。
->如果未提供或沒有,則返回新分配的數組。
Return :[index_array]除非指定out,否則將返回新的integer_array,在這種情況下,將返回對out的引用。
代碼1:
# Python program explaining
# numpy.MaskedArray.argmin() method
# importing numpy as geek
# and numpy.ma module as ma
import numpy as geek
import numpy.ma as ma
# creating input array
in_arr = geek.array([1, 2, 3, -1, 5])
print ("Input array:", in_arr)
# Now we are creating a masked array
# by making third entry as invalid.
mask_arr = ma.masked_array(in_arr, mask =[0, 0, 1, 0, 0])
print ("Masked array:", mask_arr)
# applying MaskedArray.argmin methods to mask array
out_arr = mask_arr.argmin()
print ("Index of min element in masked array:", out_arr)
輸出:
Input array: [ 1 2 3 -1 5] Masked array: [1 2 -- -1 5] Index of min element in masked array: 3
代碼2:
# Python program explaining
# numpy.MaskedArray.argmin() method
# importing numpy as geek
# and numpy.ma module as ma
import numpy as geek
import numpy.ma as ma
# creating input array
in_arr = geek.array([10, 20, 30, -10, 50])
print ("Input array:", in_arr)
# Now we are creating a masked array
# by making first third entry as invalid.
mask_arr = ma.masked_array(in_arr, mask =[1, 0, 1, 0, 0])
print ("Masked array:", mask_arr)
# applying MaskedArray.argminmethods to mask array
# and filling the masked location by -100
out_arr = mask_arr.argmin(fill_value = 100)
print ("Index of min element in masked array:", out_arr)
輸出:
Input array: [ 10 20 30 -10 50] Masked array: [-- 20 -- -10 50] Index of min element in masked array: 0
相關用法
- Python numpy.cov()用法及代碼示例
- Python numpy.copyto()用法及代碼示例
- Python Numpy MaskedArray.any()用法及代碼示例
- Python Numpy MaskedArray.var()用法及代碼示例
- Python Numpy MaskedArray.sum()用法及代碼示例
- Python numpy.issubsctype()用法及代碼示例
- Python Numpy MaskedArray.std()用法及代碼示例
- Python Numpy MaskedArray.mean()用法及代碼示例
- Python Numpy recarray.put()用法及代碼示例
- Python Numpy recarray.ptp()用法及代碼示例
- Python Numpy MaskedArray.all()用法及代碼示例
- Python Numpy MaskedArray.dot()用法及代碼示例
- Python Numpy recarray.all()用法及代碼示例
注:本文由純淨天空篩選整理自jana_sayantan大神的英文原創作品 Numpy MaskedArray.argmin() function | Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。