全部版块 我的主页
论坛 站务区 十一区 新手入门区
2756 1
2016-10-16
import matplotlib.pyplot as plt  
import numpy as np  
import scipy as sp  
import seaborn as sns
from scipy.stats import norm  
from sklearn.pipeline import Pipeline  
from sklearn.linear_model import LinearRegression  
from sklearn.preprocessing import PolynomialFeatures  
from sklearn import linear_model

''''' 数据生成 '''  
x = np.arange(0, 1, 0.002)  
y = norm.rvs(0, size=500, scale=0.1)  
y = y + x**2  
  
''''' 均方误差根 '''  
def rmse(y_test, y):  
    return sp.sqrt(sp.mean((y_test - y) ** 2))  
  
''''' 与均值相比的优秀程度,介于[0~1]。0表示不如均值。1表示完美预测.这个版本的实现是参考scikit-learn官网文档  '''  
def R2(y_test, y_true):  
    return 1 - ((y_test - y_true)**2).sum() / ((y_true - y_true.mean())**2).sum()  
  
  
''''' 这是Conway&White《机器学习使用案例解析》里的版本 '''  
def R22(y_test, y_true):  
    y_mean = np.array(y_true)  
    y_mean[:] = y_mean.mean()  
    return 1 - rmse(y_test, y_true) / rmse(y_mean, y_true)  
  
  
plt.scatter(x, y, s=5)  
degree = [1,2,100]  
y_test = []  
y_test = np.array(y_test)  
  
  
for d in degree:  
    clf = Pipeline([('poly', PolynomialFeatures(degree=d)),  
                    ('linear', LinearRegression(fit_intercept=False))])  
    clf.fit(x[:, np.newaxis], y)  
    y_test = clf.predict(x[:, np.newaxis])  
  
    print(clf.named_steps['linear'].coef_)  
    print('rmse=%.2f, R2=%.2f, R22=%.2f, clf.score=%.2f' %  
      (rmse(y_test, y),  
       R2(y_test, y),  
       R22(y_test, y),  
       clf.score(x[:, np.newaxis], y)))      
      
    plt.plot(x, y_test, linewidth=2)  
      
plt.grid()  
plt.legend(['1','2','100'], loc='upper left')  
plt.show()   
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2016-10-16 10:29:04
新人回复  好厉害的分析。 抱大腿
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群