2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > python线性回归做预测_python-线性回归预测

python线性回归做预测_python-线性回归预测

时间:2020-10-14 17:27:03

相关推荐

python线性回归做预测_python-线性回归预测

导入包

# Required Packages

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

from sklearn import datasets, linear_model

回归拟合的建立

创建一个线性模型,用我们的X_parameters和Y_parameter训练它。

# Function for Fitting our data to Linear model

def linear_model_main(X_parameters,Y_parameters,predict_value):

# Create linear regression object

regr = linear_model.LinearRegression()

regr.fit(X_parameters, Y_parameters)

predict_outcome = regr.predict(predict_value)

predictions = {}

predictions['intercept'] = regr.intercept_

predictions['coefficient'] = regr.coef_

predictions['predicted_value'] = predict_outcome

return predictions

预测

X,Y = get_data('input_data.csv')

predictvalue = 700

result = linear_model_main(X,Y,predictvalue)

print "Intercept value " , result['intercept']

print "coefficient" , result['coefficient']

print "Predicted value: ",result['predicted_value']

#脚本输出:

Intercept value 1771.80851064

coefficient [ 28.77659574]

Predicted value: [ 21915.42553191]

[Finished in 0.7s]

#这里,Intercept value(截距值)就是θ0的值,coefficient value(系数)就是θ1的值。 我们得到预测的价格值为21915.4255

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。