- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章python3 线性回归验证方法由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
#-*- coding: utf-8 -*-
import
pandas as pd
import
numpy as np
from
patsy.highlevel
import
dmatrices
#2.7里面是from patsy import dmatrices
from
statsmodels.stats.outliers_influence
import
variance_inflation_factor
import
statsmodels.api as sm
import
scipy.stats as stats
from
sklearn.metrics
import
mean_squared_error
import
seaborn as sns
import
matplotlib.pyplot as plt
import
matplotlib.mlab as mlab
import
matplotlib
#数据获取
ccpp
=
pd.read_excel(
'CCPP.xlsx'
)
ccpp.describe()
#绘制各变量之间的散点图
sns.pairplot(ccpp)
plt.show()
#发电量(PE)与自变量之间的相关系数
a
=
ccpp.corrwith(ccpp.PE)
print
(a)
#将因变量PE,自变量AT,V,AP和截距项(值为1的1维数值)以数据框的形式组合起来
y,x
=
dmatrices(
'PE~AT+V+AP'
,data
=
ccpp,return_type
=
'dataframe'
)
#构造空的数据框
vif
=
pd.DataFrame()
vif["
"VIF Factor"
"]
=
[variance_inflation_factor(x.values,i)
for
i
in
range
(x.shape[
1
])]
vif["
"features"
"]
=
x.columns
print
(vif)
#构建PE与AT,V和AP之间的线性模型
fit
=
sm.formula.ols(
'PE~AT+V+AP'
,data
=
ccpp).fit()
b
=
fit.summary()
# print(b)
#计算模型的RMSE值
pred
=
fit.predict()
c
=
np.sqrt(mean_squared_error(ccpp.PE,pred))
print
(c)
#离群点检验
outliers
=
fit.get_influence()
#高杠杆值点(帽子矩阵)
leverage
=
outliers.hat_matrix_diag
#dffits值
dffits
=
outliers.dffits[
0
]
#学生化残差
resid_stu
=
outliers.resid_studentized_external
#cook距离
cook
=
outliers.cooks_distance[
0
]
#covratio值
covratio
=
outliers.cov_ratio
#将上面的几种异常值检验统计量与原始数据集合并
contat1
=
pd.concat([pd.Series(leverage,name
=
'leverage'
),pd.Series(dffits,name
=
'dffits'
),
pd.Series(resid_stu,name
=
'resid_stu'
),pd.Series(cook,name
=
'cook'
),
pd.Series(covratio,name
=
'covratio'
),],axis
=
1
)
ccpp_outliers
=
pd.concat([ccpp,contat1],axis
=
1
)
d
=
ccpp_outliers.head()
print
(d)
#计算异常值数量的比例
outliers_ratio
=
sum
(np.where((np.
abs
(ccpp_outliers.resid_stu)>
2
),
1
,
0
))
/
ccpp_outliers.shape[
0
]
e
=
outliers_ratio
print
(e)
#删除异常值
ccpp_outliers
=
ccpp_outliers.loc[np.
abs
(ccpp_outliers.resid_stu)<
=
2
,]
#重新建模
fit2
=
sm.formula.ols(
'PE~AT+V+AP'
,data
=
ccpp_outliers).fit()
f
=
fit2.summary()
# print(f)
pred2
=
fit2.predict()
g
=
np.sqrt(mean_squared_error(ccpp_outliers.PE,pred2))
print
(g)
#
#残差的正态性检验(直方图法)
resid
=
fit2.resid
#中文和负号的正常显示
# plt.rcParams['font.sans=serif'] = ['Microsoft YaHei']
plt.rcParams[
'font.sans-serif'
]
=
[
'SimHei'
]
# plt.rcParams['font.sans=serif'] = 'sans-serif'
plt.rcParams[
'axes.unicode_minus'
]
=
False
plt.hist(resid,bins
=
100
,normed
=
True
,color
=
'steelblue'
,edgecolor
=
'k'
)
#设置坐标轴标签和标题
plt.title(
'残差直方图'
)
plt.ylabel(
'密度值'
)
#生成正态曲线的数据
x1
=
np.linspace(resid.
min
(),resid.
max
(),
1000
)
normal
=
mlab.normpdf(x1,resid.mean(),resid.std())
#绘制正态分布曲线
plt.plot(x1,normal,
'r-'
,linewidth
=
2
,label
=
'正态分布曲线'
)
#生成核密度曲线的数据
kde
=
mlab.GaussianKDE(resid)
x2
=
np.linspace(resid.
min
(),resid.
max
(),
1000
)
#绘制核密度曲线
plt.plot(x2,kde(x2),
'k-'
,linewidth
=
2
,label
=
'核密度曲线'
)
#去除图形顶部边界和右边界的刻度
plt.tick_params(top
=
'off'
,right
=
'off'
)
#显示图例
plt.legend(loc
=
'best'
)
#显示图形
plt.show()
#生成的正态曲线的数据
pp_qq_plot
=
sm.ProbPlot(resid)
pp_qq_plot.ppplot(line
=
'45'
)
plt.title(
'P-P图'
)
pp_qq_plot.qqplot(line
=
'q'
)
plt.title(
'Q-Q图'
)
plt.show()
#残差的正态性检验(非参数法)
standard_resid
=
(resid
-
np.mean(resid))
/
np.std(resid)
g
=
stats.kstest(standard_resid,
'norm'
)
print
(g)
# 总结:由于shapiro正态性检验对样本量的需求是5000以内,而本次数据集样本量有9000多,故选择k-s来完成正态性检验。
# 从k-s检验的p值来看,拒绝了残差服从正态分布的假设,即认为残差并不满足正态性假设这个前提。
# 如果残差不服从正态分布的话,建议对Y变量进行box-cox变换处理。
# 由于fit2模型的残差并没有特别明显的偏态(偏度为0.058,接近于0),故这里就不对Y进行变换。
#
# import scipy.stats as stats
# #找到box-cox变换的Lambda系数
# lamd = stats.boxcox_normmax(vif.y,method = 'mle')
# #对y进行变换
# vif['trans_y'] = stats.boxcox(vif.y,lamd)
# #建模
# fit3 = sm.formula.ols('y~x1+x2...',data = vif).fit()
# fit3.summary()
|
以上这篇python3 线性回归验证方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我.
原文链接:https://blog.csdn.net/SunWuKong_Hadoop/article/details/80254848 。
最后此篇关于python3 线性回归验证方法的文章就讲到这里了,如果你想了解更多关于python3 线性回归验证方法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
在 JSF2 应用程序中遇到验证属性的问题时,有两种主要方法。 使用 Annotation 在 ManagedBean 上定义验证 @ManagedBean public class MyBean {
我想实现一个不常见的功能,我认为 jquery 验证插件将是最好的方法(如果您在没有插件的情况下建议和回答,我们也会欢迎)。我想在用户在输入字段中输入正确的单词后立即隐藏表单。我试过这个: $("
我有几个下拉菜单(类名为month_dropdown),并且下拉菜单的数量不是恒定的。我怎样才能为它们实现 NotEqual 验证。我正在使用 jQuery 验证插件。 这就是我写的 - jQuery
我设法制作了这个网址验证代码并且它起作用了。但我面临着一个问题。我认为 stackoverflow 是获得解决方案的最佳场所。 function url_followers(){ var url=do
我目前正在使用后端服务,该服务允许用户在客户端应用程序上使用 Google Games 库登录。 用户可以通过他们的 gplay ID 向我们发送信息,以便登录或恢复旧帐户。用户向我们发送以下内容,包
我正在尝试验证输入以查看它是否是有效的 IP 地址(可能是部分地址)。 可接受的输入:172、172.112、172.112.113、172.112.113.114 Not Acceptable 输入
我从 Mongoose 验证中得到这条消息: 'Validator failed for path phone with value ``' 这不应该发生,因为不需要电话。 这是我的模型架构: var
我一直在尝试使用Python-LDAP (版本 2.4.19)在 MacOS X 10.9.5 和 Python 2.7.9 下 我想在调用 .start_tls_s() 后验证与给定 LDAP 服务
我正在处理一个仅与 IE6 兼容的旧 javascript 项目(抱歉...),我想仅在 VS 2017 中禁用此项目的 ESLint/CSLint/Javascript 验证/CSS 验证。 我知道
我正在寻找一种方法来验证 Spring 命令 bean 中的 java.lang.Double 字段的最大值和最小值(一个值必须位于给定的值范围之间),例如, public final class W
我正在尝试在 springfuse(JavaEE 6 + Spring Framework (针对 Jetty、Tomcat、JBoss 等)) 和 maven 的帮助下构建我的 webapps 工作
我试图在我们的项目中使用 scalaz 验证,但遇到了以下情况: def rate(username: String, params: Map[String, String]): Validation
我有一个像这样的 Yaml 文件 name: hhh_aaa_bbb arguments: - !argument name: inputsss des
我有一个表单,人们可以单击并向表单添加字段,并且我需要让它在单击时验证这些字段中的值。 假设我单击它两次并获取 2 个独立的字段集,我需要旋转 % 以确保它在保存时等于 100。 我已放入此函数以使其
在我的页面中有一个选项可以创建新的日期字段输入框。用户可以根据需要创建尽可能多的“截止日期”和“起始日期”框。就像, 日期_to1 || date_from1 日期到2 ||日期_from2 date
我有一个像这样的 Yaml 文件 name: hhh_aaa_bbb arguments: - !argument name: inputsss des
有没有办法在动态字段上使用 jquery 验证表单。 我想将其设置为必填字段 我正在使用 Jsp 动态创建表单字段。 喜欢 等等...... 我想使用必需的表单字段验证此表单字段。 最佳答
嗨,任何人都可以通过提供 JavaScript 代码来帮助我验证用户名文本框不应包含数字,它只能包含一个字符。 最佳答案 使用正则表达式: (\d)+ 如果找到匹配项,则字符串中就有一个数字。 关于J
我有两个输入字段holidayDate和Description(id=tags) $(document).ready(function() {
我遇到了这个问题,这些验证从电子邮件验证部分开始就停止工作。 我只是不明白为什么即使经过几天的观察,只是想知道是否有人可以在这里指出我的错误? Javascript部分: function valid
我是一名优秀的程序员,十分优秀!