【优达学城测评】sklearn-线性回归

我的以为这是最最简单的,套直线方程就能够。python

同时,这个拟合的代码彷佛在后续监督学习中能够套用,未完待续。先贴这段代码。学习

def studentReg(ages_train, net_worths_train):
    ### import the sklearn regression module, create, and train your regression
    ### name your regression reg
    
    ### your code goes here!
    
    from sklearn.linear_model import LinearRegression
    reg=LinearRegression()
    reg.fit(ages_train,net_worths_train)
    
    return regcode

print"Katie's net worth prediction:", reg.predict([27])
print"slope:",reg.coef_
print"intercept:",reg.intercept_it

print"\n ##########stats on test dataset #########\n"
print"r-squared score:", reg.score(ages_test,net_worths_test)io

print"\n ##########stats on training dataset ##########\n"
print"r-squared score:", reg.score(ages_train,net_worths_train)class

打印出的预测结果并不精确,因此后续须要学习回归会出现的偏差类型,以及R平方指标,若是过分拟合,那么这个指标就会很低test

 

#!/usr/bin/pythonimport

import numpy
import matplotlib
matplotlib.use('agg')module

import matplotlib.pyplot as plt
from studentRegression import studentReg
from class_vis import prettyPicture, output_imagemodel

from ages_net_worths import ageNetWorthData

 ages_train, ages_test, net_worths_train, net_worths_test = ageNetWorthData()

 

reg = studentReg(ages_train, net_worths_train)


plt.clf()
plt.scatter(ages_train, net_worths_train, color="b", label="train data")
plt.scatter(ages_test, net_worths_test, color="r", label="test data")
plt.plot(ages_test, reg.predict(ages_test), color="black")
plt.legend(loc=2)
plt.xlabel("ages")
plt.ylabel("net worths")


plt.savefig("test.png")
output_image("test.png", "png", open("test.png", "rb").read())

相关文章
相关标签/搜索