python离群点检测_利用分位数回归和Python识别离群值

你需要弄清楚某个点是在95%分位数线上还是在5%分位数线之下。这可以使用叉积来实现,请参见this answer以获得简单的实现。在

在您的例子中,您需要将分位数线上下的点组合起来,可能是在一个遮罩中。在

vQkgd.png

下面是一个例子:import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import statsmodels.formula.api as smf

df = pd.DataFrame(np.random.normal(0, 1, (100, 2)))

df.columns = ['LST', 'NDVI']

model = smf.quantreg('NDVI ~ LST', df)

quantiles = [0.05, 0.95]

fits = [model.fit(q=q) for q in quantiles]

figure, axes = plt.subplots()

x = df['LST']

y = df['NDVI']

fit = np.polyfit(x, y, deg=1)

_x = np.linspace(x.min(), x.max(), num=len(y))

# fit lines

_y_005 = fits[0].params['LST'] * _x + fits[0].params['Intercept']

_y_095 = fits[1].params['LST'] * _x + fits[1].params['Intercept']

# start and end coordinates of fit lines

p = np.column_stack((x, y))

a = np.array([_x[0], _y_005[0]]) #first point of 0.05 quantile fit line

b = np.array([_x[-1], _y_005[-1]]) #last point of 0.05 quantile fit line

a_ = np.array([_x[0], _y_095[0]])

b_ = np.array([_x[-1], _y_095[-1]])

#mask based on if coordinates are above 0.95 or below 0.05 quantile fitlines using cross product

mask = lambda p, a, b, a_, b_: (np.cross(p-a, b-a) > 0) | (np.cross(p-a_, b_-a_) < 0)

mask = mask(p, a, b, a_, b_)

axes.scatter(x[mask], df['NDVI'][mask], facecolor='r', edgecolor='none', alpha=0.3, label='data point')

axes.scatter(x[~mask], df['NDVI'][~mask], facecolor='g', edgecolor='none', alpha=0.3, label='data point')

axes.plot(x, fit[0] * x + fit[1], label='best fit', c='lightgrey')

axes.plot(_x, _y_095, label=quantiles[1], c='orange')

axes.plot(_x, _y_005, label=quantiles[0], c='lightblue')

axes.legend()

axes.set_xlabel('LST')

axes.set_ylabel('NDVI')

plt.show()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值