当前位置:网站首页>初始线性回归
初始线性回归
2022-06-28 01:03:00 【m0_61811389】
一.用Excel中数据分析功能做线性回归
1.取20组数据

2.取200组数据

3.取2000组数据

二.用jupyter编程(不借助第三方库),用最小二乘法做线性回归
1.python最小二乘法源代码
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
points = np.genfromtxt("D:/wh.csv",delimiter=",")
#将wh.csv文件中的数据赋值给points
#将points中的数据分别赋给x,y,求回归方程y=ax+b
x=points[1:21,1];
y=points[1:21,2];
pccs = np.corrcoef(x, y)
c,d=pccs
e,f=c
x_mean = np.mean(x)
y_mean = np.mean(y)
xsize = x.size
zi = (x * y).sum() - xsize * x_mean *y_mean
mu = (x ** 2).sum() - xsize * x_mean ** 2
a = zi / mu
b = y_mean - a * x_mean
a = np.around(a,decimals=2)
b = np.around(b,decimals=2)
print(f'回归线方程:y = {a}x + {b}')
print(f'相关系数为{f}')
#使用第三方库skleran画出拟合曲线
y1 = a*x + b
plt.scatter(x,y)
plt.plot(x,y1,c='r')
2.取20组数据

3.取200组数据

4.取2000组数据

三.用jupyter编程,借助skleran做线性回归
1.python借助skleran源代码
from sklearn import linear_model #表示,可以调用sklearn中的linear_model模块进行线性回归。
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
data = np.genfromtxt("wh.csv",delimiter=",")
data1=data[1:21]#根据所取数据更改值
x=[example[1] for example in data1]
y=[example[2] for example in data1]
pccs = np.corrcoef(x, y)
c,d=pccs
e,f=c
X = np.asarray(x).reshape(-1, 1)
Y = np.asarray(y).reshape(-1, 1)
model = linear_model.LinearRegression()
model.fit(X,Y)
b=model.intercept_[0] #截距
a=model.coef_[0]#线性模型的系数
a1=a[0]
print(f'回归线方程:y = {a1}x + {b}')
print(f'相关系数为{f}')
y1 = a1*X + b
plt.scatter(X,Y)
plt.plot(x,y1,c='r')
2.取20组数据

3.取200组数据

4.取2000组数据

比较:从三种方式可以看出其回归方程式大体一致,就是有相关系数有一些差别
参考文献:(3条消息) 分别使用Excel,jupyter,jupyter+skleran做线性回归练习_平行叶子的博客-CSDN博客
边栏推荐
- 贪吃蛇 C语言
- 转载文章:数字经济催生强劲算力需求 英特尔发布多项创新技术挖掘算力潜能
- 【历史上的今天】6 月 2 日:苹果推出了 Swift 编程语言;电信收购联通 C 网;OS X Yosemite 发布
- Character interception triplets of data warehouse: substrb, substr, substring
- [2D code image correction and enhancement] simulation of 2D code image correction and enhancement processing based on MATLAB
- 【历史上的今天】6 月 18 日:京东诞生;网店平台 Etsy 成立;Facebook 发布 Libra 白皮书
- Livedata interview question bank and answers -- 7 consecutive questions for livedata interview~
- Low code solution - a low code solution for digital after-sales service covering the whole process of work order, maintenance and Finance
- 在线文本按行批量反转工具
- js实现时钟
猜你喜欢

【历史上的今天】6 月 7 日:Kubernetes 开源版本发布;《魔兽世界》登陆中国;分组交换网络发明者出生

StaticLayout的使用详解

Complex and inefficient logistics? Three steps to solve problems in enterprise administration

STM32F1与STM32CubeIDE编程实例-金属触摸传感器驱动

【历史上的今天】6 月 15 日:第一个手机病毒;AI 巨匠司马贺诞生;Chromebook 发布

【历史上的今天】6 月 3 日:微软推出必应搜索引擎;Larry Roberts 启动阿帕网;Visual Basic 之父出生

批阅2022春季学期课程小论文提交情况
![[2D code image correction and enhancement] simulation of 2D code image correction and enhancement processing based on MATLAB](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[2D code image correction and enhancement] simulation of 2D code image correction and enhancement processing based on MATLAB

【历史上的今天】5 月 31 日:Amiga 之父诞生;BASIC 语言的共同开发者出生;黑莓 BBM 停运

【云原生】-Docker安装部署分布式数据库 OceanBase
随机推荐
Skills in schematic merging
横向滚动的RecycleView一屏显示五个半,低于五个平均分布
在线JSON转PlainText工具
Mysql database foundation: DML data operation language
【倒立摆控制】基于UKF无迹卡尔曼滤波的倒立摆控制simulink仿真
【云原生】-Docker安装部署分布式数据库 OceanBase
【历史上的今天】6 月 19 日:iPhone 3GS 上市;帕斯卡诞生;《反恐精英》开始测试
11 timers for STM32F103
[today in history] June 2: Apple launched swift programming language; China Telecom acquires China Unicom C network; OS X Yosemite release
畢業總結
MySQL优化小技巧
[today in history] June 11: the co inventor of Monte Carlo method was born; Google launched Google Earth; Google acquires waze
LeetCode - Easy - 197
How to use data-driven "customer lifecycle management" to improve lead conversion rate and customer satisfaction?
Opencv——几何空间变换(仿射变换和投影变换)
How does win11 add printers and scanners? Win11 add printer and scanner settings
MFC常用 当前路径
[today in history] June 3: Microsoft launched Bing search engine; Larry Roberts starts ARPANET; The father of Visual Basic was born
win11如何添加打印机和扫描仪?win11添加打印机和扫描仪的设置
I forgot my cell phone today