线性回归(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)样本以及拟合的直线

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值