# -*- coding: utf-8 -*-from sklearn.ensemble import BaggingRegressor
from sklearn.tree import ExtraTreeRegressor
from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeRegressor
from sklearn.svm import SVR
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from ydata_profiling import ProfileReport
import matplotlib.pyplot as plt
from sklearn.metrics import mean_absolute_error
import joblib
data_x=pd.read_csv('H:/Myjupyter/zhw/血糖/修改数据/S01now4.7.csv',encoding='ISO-8859-1')
data_y=pd.read_csv('H:/Myjupyter/zhw/血糖/修改数据/S014.7.csv',encoding='ISO-8859-1')from sklearn.ensemble import RandomForestRegressor,ExtraTreesRegressor,GradientBoostingRegressor
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
features = data_x["4.7AD"]
targets=data_y["4.7AD"]
score_train1=[]
score_test1=[]
x_train,x_test,y_train,y_test=train_test_split(features,targets,train_size=0.75,random_state=4000)
x_train1 =x_train.values.reshape(-1,1)
y_train1 = y_train.values.reshape(-1,1)
x_test1 = x_test.values.reshape(-1,1)
y_test1 = y_test.values.reshape(-1,1)
regressor = LinearRegression()
regressor.fit(x_train1,y_train1)
reg_y_prediction = regressor.predict(x_test1)
plt.scatter(x_train1,y_train1)
plt1,= plt.plot(x_test1,reg_y_prediction,label='regressor')
plt.xlabel('X')
plt.ylabel('y')
plt.legend(handles=[plt1])#handles需要传入你所画线条的实例对象
plt.show()#输出线性回归模型在训练样本上的R-squared值print('The R-squared value of linear regressor performing on the training data is',regressor.score(x_train1,y_train1))print('The R-squared value of linear regressor performing on the test data is',regressor.score(x_test1,y_test1))
多项式模型训练
degree=2
# -*- coding: utf-8 -*-from sklearn.ensemble import BaggingRegressor
from sklearn.tree import ExtraTreeRegressor
from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeRegressor
from sklearn.svm import SVR
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from ydata_profiling import ProfileReport
import matplotlib.pyplot as plt
from sklearn.metrics import mean_absolute_error
import joblib
data_x=pd.read_csv('H:/Myjupyter/zhw/血糖/修改数据/S01now4.7.csv',encoding='ISO-8859-1')
data_y=pd.read_csv('H:/Myjupyter/zhw/血糖/修改数据/S014.7.csv',encoding='ISO-8859-1')from sklearn.ensemble import RandomForestRegressor,ExtraTreesRegressor,GradientBoostingRegressor
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
features = data_x["4.7AD"]
targets=data_y["4.7AD"]
score_train1=[]
score_test1=[]
x_train,x_test,y_train,y_test=train_test_split(features,targets,train_size=0.75,random_state=4000)#使用PolynomialFeatures(degree=2)映射出2次多项式
poly2 = PolynomialFeatures(degree=2)
x_train1 =x_train.values.reshape(-1,1)
y_train1 = y_train.values.reshape(-1,1)
x_test1 = x_test.values.reshape(-1,1)
y_test1 = y_test.values.reshape(-1,1)
x_train_poly2 = poly2.fit_transform(x_train1)
regressor_poly2 = LinearRegression()
regressor_poly2.fit(x_train_poly2,y_train1)
x_poly2 = poly2.transform(x_test1)
y_poly2 = regressor_poly2.predict(x_poly2)
plt.scatter(x_train1,y_train1)
plt1,= plt.plot(x_test1,reg_y_prediction,label='regressor')
plt2,= plt.plot(x_test1,y_poly2,label='Degree=2')
plt.xlabel('X')
plt.ylabel('y')
plt.legend(handles=[plt1,plt2])#handles需要传入你所画线条的实例对象
plt.show()print(x_train_poly2.shape)print(y_train1.shape)#输出线性回归模型在训练样本上的R-squared值#x_train_poly2 = x_train_poly2.reshape(-1, 1)#y_test1 = y_test.values.reshape(-1, 1)#输出模型在训练样本上的R-squared值print('The R-squared value of Polynomial regressor(Degress=2) performing on the training data is',regressor_poly2.score(x_train_poly2,y_train1))print('The R-squared value of linear regressor performing on the test data is',regressor.score(x_test1,y_test1))
degree = 4
# -*- coding: utf-8 -*-from sklearn.ensemble import BaggingRegressor
from sklearn.tree import ExtraTreeRegressor
from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeRegressor
from sklearn.svm import SVR
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from ydata_profiling import ProfileReport
import matplotlib.pyplot as plt
from sklearn.metrics import mean_absolute_error
import joblib
data_x=pd.read_csv('H:/Myjupyter/zhw/血糖/修改数据/S01now4.7.csv',encoding='ISO-8859-1')
data_y=pd.read_csv('H:/Myjupyter/zhw/血糖/修改数据/S014.7.csv',encoding='ISO-8859-1')from sklearn.ensemble import RandomForestRegressor,ExtraTreesRegressor,GradientBoostingRegressor
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
features = data_x["4.7AD"]
targets=data_y["4.7AD"]
score_train1=[]
score_test1=[]
x_train,x_test,y_train,y_test=train_test_split(features,targets,train_size=0.75,random_state=4000)#使用PolynomialFeatures(degree=4)映射出4次多项式
poly4 = PolynomialFeatures(degree=4)
x_train1 =x_train.values.reshape(-1,1)
y_train1 = y_train.values.reshape(-1,1)
x_test1 = x_test.values.reshape(-1,1)
y_test1 = y_test.values.reshape(-1,1)
x_train_poly4 = poly4.fit_transform(x_train1)
regressor_poly4 = LinearRegression()
regressor_poly4.fit(x_train_poly4,y_train1)
x_poly4 = poly4.transform(x_test1)
y_poly4 = regressor_poly4.predict(x_poly4)
plt.scatter(x_train1,y_train1)#plt1, = plt.plot(x_test1,reg_y_prediction,label='regressor')#plt2, = plt.plot(x_test1,y_poly2,label='Degree=2')
plt4,= plt.plot(x_test1,y_poly4,label='Degree=4')
plt.xlabel('X')
plt.ylabel('y')
plt.legend(handles=[plt4])#handles需要传入你所画线条的实例对象
plt.show()#输出线性回归模型在训练样本上的R-squared值print('The R-squared value of Polynomial regressor(Degress=4) performing on the training data is',regressor_poly4.score(x_train_poly4,y_train1))print('The R-squared value of linear regressor performing on the test data is',regressor.score(x_test1,y_test1))
预测
生成模型
# -*- coding: utf-8 -*-from sklearn.ensemble import BaggingRegressor
from sklearn.tree import ExtraTreeRegressor
from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeRegressor
from sklearn.svm import SVR
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from ydata_profiling import ProfileReport
import matplotlib.pyplot as plt
from sklearn.metrics import mean_absolute_error
import joblib
data_x=pd.read_csv('H:/Myjupyter/zhw/血糖/修改数据/S01now4.7.csv',encoding='ISO-8859-1')
data_y=pd.read_csv('H:/Myjupyter/zhw/血糖/修改数据/S014.7.csv',encoding='ISO-8859-1')from sklearn.ensemble import RandomForestRegressor,ExtraTreesRegressor,GradientBoostingRegressor
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
features = data_x["4.7AD"]
targets=data_y["4.7AD"]
score_train1=[]
score_test1=[]
x_train,x_test,y_train,y_test=train_test_split(features,targets,train_size=0.75,random_state=4000)#使用PolynomialFeatures(degree=2)映射出2次多项式
poly2 = PolynomialFeatures(degree=2)
x_train1 =x_train.values.reshape(-1,1)
y_train1 = y_train.values.reshape(-1,1)
x_test1 = x_test.values.reshape(-1,1)
y_test1 = y_test.values.reshape(-1,1)
x_train_poly2 = poly2.fit_transform(x_train1)
regressor_poly2 = LinearRegression()
regressor_poly2.fit(x_train_poly2,y_train1)#生成模型
joblib.dump(regressor_poly2,'model.pkl')
joblib.dump(poly2,'poly_feature.pkl')