# -*- coding: utf-8 -*-
"""
Created on Mon May 14 09:17:53 2018
@author: sun_y
"""
import numpy as np
import sklearn as sk
import os
import matplotlib.pyplot as plt
from scipy.stats import norm
import pickle
from sklearn import linear_model
def load_dataset(filename):
with open(os.path.join("E:\\python\\sklearn model",filename), 'rb') as f:
return pickle.load(f)
data=load_dataset('basisData.pkl')
X=data['X']
y=data['y']
Xtest=data['Xtest']
ytest=data['ytest']
"""
x=np.random.rand(100,1)*20-10
mu=0
sigma=0.1
s= norm.rvs(0, size=(100,1), scale=0.5)
y=0.5*x+10+s
"""
fig=plt.figure()
ax1=fig.add_subplot(321)
plt.scatter(Xtest,ytest)
ax1.set_title('linear regression')
reg=linear_model.LinearRegression()
reg.fit(X,y)
ypredict=reg.predict(Xtest)
plt.plot(Xtest,ypredict,'r')
test_error=np.mean((ypredict - ytest)**2)
print("Logistic Regression test error is %f" % test_error)
ax2=fig.add_subplot(322)
plt.scatter(Xtest,ytest)
ax2.set_title('Ridge Regression')
reg = linear_model.Ridge (alpha = 0.9)
reg.fit(X,y)
ypredict=reg.predict(Xtest)
plt.plot(Xtest,ypredict,'r')
test_error=np.mean((ypredict - ytest)**2)
print("Ridge Regression test error is %f" % test_error)
ax3=fig.add_subplot(323)
plt.scatter(Xtest,ytest)
ax3.set_title('Lasso')
reg = linear_model.Lasso(alpha = 0.9)
reg.fit(X,y)
ypredict=reg.predict(Xtest)
plt.plot(Xtest,ypredict,'r')
test_error=np.mean((ypredict - ytest)**2)
print("Lasso test error is %f" % test_error)
ax3=fig.add_subplot(324)
plt.scatter(Xtest,ytest)
ax3.set_title('BayesRidge')
reg = linear_model.BayesianRidge()
reg.fit(X,y)
ypredict=reg.predict(Xtest)
plt.plot(Xtest,ypredict,'r')
test_error=np.mean((ypredict - ytest)**2)
print("BayesRidge test error is %f" % test_error)
Linear Regression test error is 3393.869098
Ridge Regression test error is 3393.614383
Lasso test error is 26459.575158
BayesRidge test error is 26458.745573
但拟合效果不是很好,可继续尝试其他模型:RBF , Polynomial regression