python-3.x - 置信区间外的子集数据点
问题描述
使用与上一个问题相同的示例(代码粘贴在下面),我们可以使用来自statsmodels outliers_influencesummary_table
的函数获得 95% CI 。但是现在,怎么可能只对置信区间之外的数据点 (和) 进行子集化呢?x
y
import numpy as np
import statsmodels.api as sm
from statsmodels.stats.outliers_influence import summary_table
#measurements genre
n = 100
x = np.linspace(0, 10, n)
e = np.random.normal(size=n)
y = 1 + 0.5*x + 2*e
X = sm.add_constant(x)
re = sm.OLS(y, X).fit()
st, data, ss2 = summary_table(re, alpha=0.05)
predict_ci_low, predict_ci_upp = data[:, 6:8].T
解决方案
这可能有点晚了,但您可以pandas.DataFrame
根据布尔值列表将其放入 a 和过滤器中。假设我得到了你的问题:
import numpy as np
import statsmodels.api as sm
from statsmodels.stats.outliers_influence import summary_table
import matplotlib.pyplot as plot
## Import pandas
import pandas as pd
#measurements genre
n = 100
x = np.linspace(0, 10, n)
e = np.random.normal(size=n)
y = 1 + 0.5*x + 2*e
X = sm.add_constant(x)
re = sm.OLS(y, X).fit()
st, data, ss2 = summary_table(re, alpha=0.05)
# Make prediction
prediction = re.predict(X)
predict_ci_low, predict_ci_upp = data[:, 6:8].T
# Put y and x in a pd.DataFrame
df = pd.DataFrame(y).set_index(x)
# Get the y values that are out of the ci intervals. This could be done directly in the df indexer
out_up = y > predict_ci_upp
out_down = y < predict_ci_low
# Plot everything
plot.plot(x, y, label = 'train')
plot.plot(df[out_up], marker = 'o', linewidth = 0)
plot.plot(df[out_down], marker = 'o', linewidth = 0)
plot.plot(x, predictionTrain, label = 'prediction')
plot.plot(x, predict_ci_upp, label = 'ci_up')
plot.plot(x, predict_ci_low, label = 'ci_low')
plot.legend(loc='best')
这是结果图:
推荐阅读
- python - 如何检查两列是否匹配基于它们具有相同的字符串?
- registry - 尝试远程获取/更改当前登录用户的默认打印机注册表值
- flutter - Flutter - Pub 无法重命名目录,因为访问被拒绝
- vbscript - 使用 VBS 返回两个字符之间不同长度的值
- python - 使用 az webapp deployment source 从 git 部署代码,但当应用位于 git 存储库子文件夹中时
- angular - 如何与 Angular Http 和 IP 地址通信
- python - 为 bar3d matplotlib 图选择颜色
- powerbi - DAX 计数行,但不适用于未来日期
- javascript - php - 防止后退按钮
- curl - curl命令504网关超时错误的gitlab管道失败