首页 > 解决方案 > xarray 中是否有用于从数据集中删除异常值的内置函数?

问题描述

我有一个作为 xarray 数据集打开的时空 .nc 文件,我想删除超过 99% 的值。是否有任何简单/直接的方法来删除这些值?

关于我的数据集的信息是

Dimensions:    (latitude: 204, longitude: 180, time: 985)
Coordinates:
  * longitude  (longitude) float32 -69.958336 -69.875 ... -55.124996 -55.04166
  * latitude   (latitude) float32 -38.041668 -38.12501 ... -54.87501 -54.95834
  * time       (time) datetime64[ns] 1997-09-06 1997-09-14 ... 2019-09-06
Data variables:
    chl        (time, latitude, longitude) float64 nan nan nan ... nan nan nan

标签: pythonpython-xarray

解决方案


您可以创建自己的功能

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])

出去:

array([[[4, 1, 2],
        [4, 4, 4],
        [1, 4, 3]],

推荐阅读