线性回归主要是拟合一个函数,能预测一个新的样本:

(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) # aprint("预测值:",predict_value) # yshow_linear_line(X,Y)
main()

(3)输出:

(4)样本以及拟合的直线

线性回归(y=ax+b)相关推荐

  1. pytorch nn.Linear(对输入数据做线性变换:y=Ax+b)(全连接层?)

    Linear layers class torch.nn.Linear(in_features, out_features, bias=True) 对输入数据做线性变换:y=Ax+b 参数: in_f ...

  2. Python数据拟合幂函数y=ax^b

    Python数据拟合--幂函数y=ax^b from scipy.optimize import curve_fit import numpy as np import matplotlib.pypl ...

  3. 你是否真的读懂了回归方程y=ax+b?

    当我们对一些现象进行回归分析之后,可以得到回归方程y=ax+b,很简单的一根直线,如果结合我们的实际应用场景,则可以从该方程的2个系数中发现很多问题: 1  当b比a大很多时,如何理解? 比如,工作量 ...

  4. 用r语言画出y = ax^2 + bx + c,R语言中使用curve函数绘制常用函数曲线

    前面文章中介绍了使用plot函数绘制图形的方法.本文介绍一下使用R中curve函数绘制常见函数曲线的方法. 1.curve函数简介 curve函数语法格式如下: curve(expr, from = ...

  5. 用r语言画出y = ax^2 + bx + c,R语言中如何使用最小二乘法

    这里只是介绍下R语言中如何使用最小二乘法解决一次函数的线性回归问题. 代码如下: > x > y > lsfit(x,y) 结果如下: $coefficients Intercept ...

  6. 算法数据分析实例——城市气候与海洋关系数据分析[线性回归y=w*x+b]

    城市气候与海洋的关系研究 导入包 import numpy as npimport pandas as pd from pandas import Series,DataFrameimport mat ...

  7. 为什么torch.nn.Linear的表达形式为y=xA^T+b而不是常见的y=Ax+b?

    今天看代码,对比了常见的公式表达与代码的表达,发觉torch.nn.Linear的数学表达与我想象的有点不同,于是思索了一番. 众多周知,torch.nn.Linear作为全连接层,将下一层的每个结点 ...

  8. matplotlib绘制线性回归y=kx+b参数的损失函数等高线图

    如题,直接上代码和结果 import math import random import numpy as np import pandas as pd import matplotlib.pyplo ...

  9. 线性回归和广义线性回归

    预备公式: 求导 X是一个矩阵,θ是一个列向量. dXθ/dθ=XT dXθT/dθT=XT dXθT/dθ=X 一.线性回归 y=ax+b 给出一个离散点,咱们想办法表示出a和b.(最小二乘法) 多 ...

最新文章

  1. redis value is not an integer or out of range
  2. libslog---高性能开源C/C++多线程安全日志库
  3. 第六章 深度学习(上中)
  4. echo和print 的区别
  5. JVM之垃圾收集机制四种GC算法详解
  6. Navicat Premium创建MySQL存储过程
  7. xampp默认mysql数据库root密码的修改
  8. 联发科天玑800适配鸿蒙系统,联发科天玑800什么水平
  9. 修改environment导致重启不能登录的问题解决
  10. Nginx端口映射(外网访问)
  11. 像excel一样规律填充(二)
  12. 神器 Nginx 的学习手册(建议收藏吃灰)
  13. office2016 64位下载链接
  14. ChromeOS 体验
  15. css字体加粗_CSS字体
  16. 可可私房菜:一个普通中国女孩的移动互联网创业经
  17. 国内外三维图形引擎研究现状
  18. Install YouTube-DL – A Command Line Video Download Tool for Linux
  19. NLP是什么,百度的NLP技术有有哪些?
  20. 十一大排序算法的实现

热门文章

  1. 那些我希望在一开始使用 Zsh(oh-my-zsh) 时就知道的
  2. Spring MVC 学习笔记 json格式的输入和输出
  3. 经典java程序员的面试题及答案
  4. 【分享】WeX5的正确打开方式(1)
  5. FreeMarker模板文件的组成(2)
  6. so文件成品评论【整理】
  7. SQL Server 影响dbcc checkdb的 8 种因素
  8. dm9000AE调试记录
  9. Silverlight Telerik控件学习:数据录入、数据验证
  10. 高质量C /C编程指南---附录B :C /C试题和谜底