请问python建模-线性回归,每运行一次,r2值会变化,是正常的吗?
import pandas as pd
import xlrd
survey=pd.read_excel("D:\data\第26节数据源-survey.xlsx")
survey.head()
survey_suv=survey[survey['category']=='SUV']
survey_suv
survey_suv.corr()
import seaborn as sns
sns.heatmap(data=survey_suv.corr())
sns.scatterplot(data=survey_suv,x='price_ratio',y='price')
#创建自变量和因变量
x=survey_suv['price_ratio']
y=survey_suv['price']
#导入机器学习交叉验证包的分割模块
from sklearn.model_selection import train_test_split
#二八分,拆分过程
x_train,x_test,y_train,y_test=train_test_split(x,y,train_size=0.8,test_size=0.2)
#检查拆分结果
print('原始数据集大小:',x.shape,'训练数据集大小:',x_train.shape,'测试训练集大小:',x_test.shape)
#导入线性模型包
from sklearn.linear_model import LinearRegression
#创建模型
model=LinearRegression()
#训练模型
model.fit(x_train,y_train)
x_train
x_train=x_train.values.reshape(-1,1)
x_test=x_test.values.reshape(-1,1)
x_train
model.fit(x_train,y_train)
a=model.intercept_
b=model.coef_
a=round(float(a),2)
b=round(float(b),2)
#print('一元一次方程为:y={}{}x'.format(a,b))
r2=model.score(x_test,y_test)
r2