python - 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
解决方案
您可以创建自己的功能
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]],
推荐阅读
- amcharts4 - amcharts - 在类别之间添加刻度
- ecmascript-6 - Es6 程序中的类和构造函数
- tensorflow - 如何解决错误 - 预期的布尔值(或无值)以下参数:--train
- c# - 我只能从大约 8 到 9 中提取数据。但我知道我的 HDA 中有更多数据。我的 ReadRaw 数据函数有问题吗?
- django - Django formset:无法保存
- python - 为什么这个递归函数会迅速增加内存使用?
- javascript - 单击功能会无意中在调试器中“创建”一个新的 html 文档
- c# - Unity Vector2.Lerp "t" 没有按预期工作,它很快
- java - Java、Springboot、MongoDB 扩展 SimpleMongoRepository (2020)
- mysql - MySQL:多插入的顺序是否有保证?