python求线性回归斜率-程序员宅基地

技术标签: python  java  数据结构与算法  

一、 先说我对这个题目的理解

直线的x,y方程是这样的:y = kx+b, k就是斜率.

求线性回归斜率, 就是说
有这么一组(x, y)的对应值——样本。如果有四组,就说样本量是4.
根据这些样本,做“线性回归”,最终求出一条直线(即y = kx + b的k值和b值),使得样本里的各个点(x, y) “尽可能的”落到直线(或者直线附近)上。

二、 python解题需要安装的包

实际解题主要用到的python库是pandas. 解题算法是“最小二乘法”,这用到了pandas的ols函数。

我的系统是ubuntu14.04.

实际实验过程如下:

$ sudo pip install numpy
$ sudo pip install pandas

安装pandas的依赖包(使用pandas.ols函数就必须安装这个依赖包):
$ sudo pip install statsmodels

如果发现安装失败,则需要安装系统依赖库先:
在http://www.netlib.org/lapack/ 下载lapack-3.6.0的包,然后:
$ cd lapack-3.6.0
$ mv make.inc.example make.inc
$ sudo apt-get install gfortran
把makefile文件中的:
lib: lapacklib tmglib
改为:
lib: blaslib variants lapacklib tmglib
$ sudo make
$ sudo cp lib*.a /usr/lib
以上依然不行,然而,'可能'只需要下面两个命令:
$ sudo apt-get install libblas-dev liblapack-dev libatlas-base-dev gfortran
$ sudo pip install statsmodels (安装时间有点长...)
过了.


三、 python解题

$ ipython
> import numpy as np
import pandas as pd
x = np.array([2,3,4,6])
xx = pd.DataFrame({"k": x})
yy = pd.Series([22,33,44,66])   # 口算都知道斜率是11,最终方程是y=11x
res = pd.ols(y=yy, x=xx)           
res

-------------------------Summary of Regression Analysis-------------------------

Formula: Y ~ <k> + <intercept>

Number of Observations: 4
Number of Degrees of Freedom: 2

R-squared: 1.0000
Adj R-squared: 1.0000

Rmse: 0.0000

F-stat (1, 2): inf, p-value: 0.0000

Degrees of Freedom: model 1, resid 2

-----------------------Summary of Estimated Coefficients------------------------
Variable Coef Std Err t-stat p-value CI 2.5% CI 97.5%
--------------------------------------------------------------------------------
k 11.0000 0.0000 1404889085527666.75 0.0000 11.0000 11.0000
intercept -0.0000 0.0000 -0.68 0.5691 -0.0000 0.0000
---------------------------------End of Summary---------------------------------

可以看出,直线方程就是这样的:y = 11x - 0  (就是最左边的一列值作为k和b)

可以直接读取beta,关注每个系数:
res.beta

k 1.100000e+01
intercept -2.131628e-14
dtype: float64

res.beta[0]       # <--------- 斜率

(试试把上面样本里x最后一个改为7,即x = np.array([2,3,4,7]))

四、 end

 参考资料:
http://www.cnblogs.com/fangwenyu/p/4284523.html

转载于:https://www.cnblogs.com/xiaouisme/p/5173083.html

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_30634661/article/details/96958489

智能推荐

RF-Adaboost 基于随机森林的Adaboost的时间序列预测算法,集成学习/增强学习MATLAB程序...-程序员宅基地

文章浏览阅读26次。作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,matlab项目合作可私信。????个人主页:Matlab科研工作室????个人信条:格物致知。???? 内容介绍在机器学习领域,时间序列预测一直是一个重要的研究方向。随着大数据时代的到来,时间序列数据的规模和复杂性不断增加,传统的预测方法面临着挑战。为了提高预测准确性和稳定...

go学习之 简单的web开发_go web网页交互-程序员宅基地

文章浏览阅读406次。Go语言基础之net/httpGo语言内置的net/http包十分的优秀,提供了HTTP客户端和服务端的实现。相对于一些简单的请求,我们不用框架,直接使用net包就可以完成web的交互了啊哈哈哈哈哈,来试试看服务器端package mainimport ( "fmt" "io/ioutil" "net/http")// 使用net/http这个包来进行开发 server端func f1(w http.ResponseWriter, r *http.Request) {_go web网页交互

ssh代理-程序员宅基地

文章浏览阅读7.2k次,点赞3次,收藏9次。ssh功能还是很强大的,提供了三种代理功能-L参数,正向代理-R参数,反向代理-D参数,socks5代理说的通俗易懂点就是我有 hostA,hostB,hostC#mermaid-svg-o500HVE8uv90TAF7 {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-o500HVE8uv90TAF7 .error-icon{fill:#552222;}#me_ssh代理

zsh: command not found: brew 报错解诀办法-程序员宅基地

文章浏览阅读1.9w次,点赞31次,收藏34次。项目场景:在使用命令行安装东西的时候提示错误提示:zsh: command not found: brew解决方案:Mac安装 homebrew使用一下命令安装,/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"使用中科大的。..._zsh: command not found: brew

org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 6; 不允许有匹配 "[xX][mM][lL]" 的处理指令目标。...-程序员宅基地

文章浏览阅读495次。Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: ### Error building SqlSession.### The error may exist in com/qianfeng/bean/PetsMapper.xml### C..._nested exception: org.xml.sax.saxparseexception; linenumber: 2; columnnumber

MaxCompute|Python 3 环境配置方法_python maxcompute-程序员宅基地

文章浏览阅读471次。MaxCompute Python 3 环境配置方法_python maxcompute

随便推点

(课堂讨论)关于软件过程模型(部分摘自老师PPT及其他网站)_软件开发模型ppt-程序员宅基地

文章浏览阅读910次。软件开发过程模型主要有:51Testing软件测试网@$H5K2K2e0x/k8O 瀑布模型(V模型、喷泉模型 )51Testing软件测试网 tQ5e8r8E#E 螺旋模型51Testing软件测试网'E,g#YO!e^Z!R  原型模型(锯齿模型、快速原型)51Testing软件测试网(A'L0y*{8F`~�R"j"a  构件组装模型 (增量模型_软件开发模型ppt

css 弹性盒子space-between两端对齐,最后一列数量不够时,最后一列左对齐。_css space-between-程序员宅基地

文章浏览阅读8.4k次。代码:<!DOCTYPE html><html><head> <title>测试</title> <style> .container{ width: 200px; height:200px; border:2px solid red; } .box { display: flex; fle_css space-between

连接mysql数据库报错java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized or represe...-程序员宅基地

文章浏览阅读1.1k次。在使用spring boot操作数据库的时候,莫名其妙抛了一个错误:mysql6.0.6时间区域时差问题ava.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone. You must configure ei..._sqlexception@3060

Leetcode189: Peeking Iterator_// **do not** save a copy of nums and manipulate i-程序员宅基地

文章浏览阅读206次。Given an Iterator class interface with methods: next() and hasNext(), design and implement a PeekingIterator that support the peek() operation -- it essentially peek() at the element that will be _// **do not** save a copy of nums and manipulate it directly. // you shoul

实战PyQt5: 140-QChart图表之烛台图_pyqt candlestickset-程序员宅基地

文章浏览阅读1k次。烛台图(Candlestick Chart)最常见的应用就是表示诸如股票交易当日成交价格幅度以及开盘及收盘价格的图表。QChart中提供类QCandlestickSeries, QCandlestickSet来实现绘制烛台图。_pyqt candlestickset

JavaScript获取时间(js中的new Date(),获取前7天时间)_js获取7天前日期-程序员宅基地

文章浏览阅读3.5k次,点赞3次,收藏5次。我们在写Echarts的时候,难免会用到js中的new Date(),用来获取时间,今天就来分享下它的用法,顺便做下笔记。_js获取7天前日期