python 线性回归 预测数据

忙碌的一周将至尾声

本周尝试线性回归预测房价
假设方程式
y=kx+b
数据集:
这里写图片描述

代码如下:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn import datasets,linear_model

def get_data(filename):
    data = pd.read_csv(filename)
    x_parameter=''
    y_parameter=''
    for single_square_feet,single_price_value in zip(data['square_feet'],data['price']):
        x_parameter.append([float(single_square_feet)])
        y_parameter.append(float(single_sprice_value))
    return x_parameter,y_parameter

def linear_model_main(x_parameter,y_parameter,predict_value):
    regr = linear_model.LinearRegression()
    regr.fit(x_parameter,y_parameter)
    predict_outcome = regr.predict(predict_value)
    predicitions={}
    predictions['intercept']=regr.intercept_
    predictions['coefficient']=regr.coef_
    predictions['predicted_value']=predict_outcome_
    return predictions

def show_linear_line(x_parameter,y_parameter):
    regr=linear_model.LinearRegression()
    regr.fit(x_parameter,y_parameter)
    plt.scatter(x_parameter,y_parameter,color='blue')
    plt.plot(x_parameterregr.predict(x_parameter),color='red',linewidth=4)
    plt.xticks()
    plt.yticks()
    plt.show()

x,y=get_data('house_price.csv')
predictvalue=500
result=linear_model_main(x,y,predictvalue)
print('intercept value', result['intercept'])
print('coefficient value', result['coefficient'])
print('predicted value', result['predicted_value'])

show_linear_line(x,y)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值