Вы можете создать свою собственную функцию
import xarray as xr
import numpy as np
# perc -> percentile that define the exclusion threshold
# dim -> dimension to which apply the filtering
def replace_outliers(data, dim=0, perc=0.99):
# calculate percentile
threshold = data[dim].quantile(perc)
# find outliers and replace them with max among remaining values
mask = data[dim].where(abs(data[dim]) <= threshold)
max_value = mask.max().values
# .where replace outliers with nan
mask = mask.fillna(max_value)
print(mask)
data[dim] = mask
return data
Тестирование
data = np.random.randint(1,5,[3, 3, 3])
# create outlier
data[0,0,0] = 100
temp = xr.DataArray(data.copy())
print(temp[0])
Выход:
array([[100, 1, 2],
[ 4, 4, 4],
[ 1, 4, 3]])
Применить функцию:
temp = replace_outliers(temp, dim=0, perc=99)
print(temp[0])
Out:
array([[[4, 1, 2],
[4, 4, 4],
[1, 4, 3]],