a d*_*ben 21 python statistics scipy linear-regression
使用scipy.stats.linregress,我在一些高度相关的x,y实验数据集上执行简单的线性回归,并且最初在视觉上检查每个x,y散点图以获得异常值.更一般地(即以编程方式)是否有一种方法来识别和屏蔽异常值?
xAp*_*ple 27
该statsmodels包有你所需要的.看看这个小代码片段及其输出:
# Imports #
import statsmodels.api as smapi
import statsmodels.graphics as smgraphics
# Make data #
x = range(30)
y = [y*10 for y in x]
# Add outlier #
x.insert(6,15)
y.insert(6,220)
# Make graph #
regression = smapi.OLS(x, y).fit()
figure = smgraphics.regressionplots.plot_fit(regression, 0)
# Find outliers #
test = regression.outlier_test()
outliers = ((x[i],y[i]) for i,t in enumerate(test) if t[2] < 0.5)
print 'Outliers: ', list(outliers)
Run Code Online (Sandbox Code Playgroud)

Outliers: [(15, 220)]
随着更新的版本statsmodels,事情发生了一些变化.这是一个新的代码段,显示了相同类型的异常值检测.
# Imports #
from random import random
import statsmodels.api as smapi
from statsmodels.formula.api import ols
import statsmodels.graphics as smgraphics
# Make data #
x = range(30)
y = [y*(10+random())+200 for y in x]
# Add outlier #
x.insert(6,15)
y.insert(6,220)
# Make fit #
regression = ols("data ~ x", data=dict(data=y, x=x)).fit()
# Find outliers #
test = regression.outlier_test()
outliers = ((x[i],y[i]) for i,t in enumerate(test.icol(2)) if t < 0.5)
print 'Outliers: ', list(outliers)
# Figure #
figure = smgraphics.regressionplots.plot_fit(regression, 1)
# Add line #
smgraphics.regressionplots.abline_plot(model_results=regression, ax=figure.axes[0])
Run Code Online (Sandbox Code Playgroud)

Outliers: [(15, 220)]
scipy.stats没有任何直接用于异常值的东西,所以回答一些链接和statsmodels的广告(这是scipy.stats的统计补充)
用于识别异常值
http://jpktd.blogspot.ca/2012/01/influence-and-outlier-measures-in.html
http://jpktd.blogspot.ca/2012/01/anscombe-and-diagnostic-statistics.html
而不是掩蔽,更好的方法是使用稳健的估计器
http://statsmodels.sourceforge.net/devel/rlm.html
有例子,遗憾的是,这些情节目前没有显示 http://statsmodels.sourceforge.net/devel/examples/generated/tut_ols_rlm.html
RLM下调异常值.估计结果具有weights属性,对于异常值,权重小于1.这也可用于查找异常值.RLM如果有几个异常值,也会更加稳健.
| 归档时间: |
|
| 查看次数: |
20134 次 |
| 最近记录: |