线性回归(y=ax+b)
线性回归主要是拟合一个函数,能预测一个新的样本:
(1)数据集如下:
(2)预测值:feet=500
# -*- coding:utf-8 -*-
import matplotlib.pyplot as plt
import pandas as pd
from sklearn import linear_model
import os
os.chdir("/Users/xxx/PycharmProjects/dataset/")
filename = "input_data.xlsx"
datafile = pd.read_excel(filename, index_col=u'ID')
# 获取数据
def get_data(datafile):
x_paramter = []
y_paramter = []
for feet,price in zip(datafile['feet'],datafile['price']):
x_paramter.append([float(feet)])
y_paramter.append(float(price))
return x_paramter,y_paramter
# 线性回归模型
def linear_model_main(x_paramter,y_paramter,predict_value):
# 创建线性回归对象
regr = linear_model.LinearRegression()
regr.fit(x_paramter,y_paramter) # 建立模型
predict_outcome = regr.predict(predict_value) # 预测值
return regr.intercept_,regr.coef_,predict_outcome # 返回截距、斜率、预测结果
# 显示线性拟合模型的结果
def show_linear_line(x_paramter,y_paramter):
regr = linear_model.LinearRegression()
regr.fit(x_paramter,y_paramter)
plt.scatter(x_paramter,y_paramter,color="blue")
x_new = [[0],[500]] # x轴长
plt.plot(x_new,regr.predict(x_new),color="red",linewidth=2)
plt.xlabel(u'Feet',color="green")
plt.ylabel(u'Price',color="green")
# plt.plot(label=u'数据图')
# plt.xticks(())
# plt.yticks(())
plt.ylim(-2000,20000)
plt.xlim(0,500)
plt.show()
def main():
X,Y = get_data(datafile)
print('X:',X)
print('Y:',Y)
predictvalue = [[500]]
intercept,coefficient,predict_value = linear_model_main(X,Y,predictvalue)
print("截距:",intercept) # b ( y=ax+b )
print("斜率:",coefficient) # a
print("预测值:",predict_value) # y
show_linear_line(X,Y)
main()
(3)输出:
(4)样本以及拟合的直线