个人觉得这是最最简单的,套直线方程就可以。

同时,这个拟合的代码似乎在后续监督学习中可以套用,未完待续。先贴这段代码。

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 reg

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

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

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

打印出的预测结果并不精确,所以后续需要学习回归会出现的误差类型,以及R平方指标,如果过度拟合,那么这个指标就会很低

 

#!/usr/bin/python

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

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

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())

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

 

 

转载于:https://my.oschina.net/Bettyty/blog/754145

相关文章:

  • 2021-12-21
  • 2022-12-23
  • 2021-05-08
  • 2021-12-27
  • 2021-12-25
  • 2021-12-03
  • 2022-03-13
  • 2022-12-23
猜你喜欢
  • 2021-12-13
  • 2021-12-18
  • 2022-12-23
  • 2021-08-11
  • 2022-12-23
  • 2022-12-23
  • 2021-04-30
相关资源
相似解决方案