当前位置:网站首页>线性回归
线性回归
2022-07-07 00:08:00 【python-码博士】
import numpy as np
from matplotlib import pyplot as plt
# 定义训练数据
x = np.array([0.86, 0.96, 1.12, 1.35, 1.55, 1.63, 1.71, 1.78])
y = np.array([12, 15, 20, 35, 48, 51, 59, 66])
def fit(x,y):
if len(x) != len(y):
return
numerator = 0.0
denominator = 0.0
x_mean = np.mean(x)
y_mean = np.mean(y)
for i in range(len(x)):
numerator += (x[i]-x_mean)*(y[i]-y_mean)
denominator += np.square(x[i]-x_mean)
b0 = numerator / denominator
b1 = y_mean-b0*x_mean
return b0,b1
b0,b1 = fit(x,y)
def predit(x,b0,b1):
return b0*x+b1
# 预测
x_test = np.array([0.75,1.08,1.26,1.51,1.6,1.67,1.85])
y_test = np.array([10,17,27,41,50,64,75])
y_predit = predit(x_test,b0,b1)
# 绘制图像
plt.plot(x,y,'k.')
plt.plot(x_test,y_predit,'g-')
yr = predit(x,b0,b1)
for idx,x in enumerate(x):
plt.plot([x,x],[y[idx],yr[idx]],'r-')
print(predit(1.75,b0,b1))
plt.show()
边栏推荐
- 数据中心为什么需要一套基础设施可视化管理系统
- Wechat applet Bluetooth connects hardware devices and communicates. Applet Bluetooth automatically reconnects due to abnormal distance. JS realizes CRC check bit
- AI人脸编辑让Lena微笑
- Realize GDB remote debugging function between different network segments
- “多模态”概念
- Pytorch builds neural network to predict temperature
- 论文阅读【Open-book Video Captioning with Retrieve-Copy-Generate Network】
- Codeforces Round #416 (Div. 2) D. Vladik and Favorite Game
- In memory, I moved from CSDN to blog park!
- High voltage leakage relay bld-20
猜你喜欢
![[binary tree] binary tree path finding](/img/34/1798111e9a294b025806a4d2d5abf8.png)
[binary tree] binary tree path finding

4. Object mapping Mapster

如何提高网站权重

sql查询:将下一行减去上一行,并做相应的计算

Flink SQL 实现读写redis,并动态生成Hset key

Zero sequence aperture of leakage relay jolx-gs62 Φ one hundred

Pytorch builds neural network to predict temperature

AI人脸编辑让Lena微笑

Leakage relay jd1-100

Différenciation et introduction des services groupés, distribués et microservices
随机推荐
Design, configuration and points for attention of network unicast (one server, multiple clients) simulation using OPNET
关于服装ERP,你知道多少?
纪念下,我从CSDN搬家到博客园啦!
Leetcode: maximum number of "balloons"
DOM node object + time node comprehensive case
数据中心为什么需要一套基础设施可视化管理系统
论文阅读【Semantic Tag Augmented XlanV Model for Video Captioning】
力扣102题:二叉树的层序遍历
K6el-100 leakage relay
Codeforces Round #416 (Div. 2) D. Vladik and Favorite Game
Message queuing: how to ensure that messages are not lost
Use, configuration and points for attention of network layer protocol (taking QoS as an example) when using OPNET for network simulation
async / await
Mysql-centos7 install MySQL through yum
Digital innovation driven guide
Paper reading [MM21 pre training for video understanding challenge:video captioning with pre training techniqu]
软件测试面试技巧
SAP ABAP BDC(批量数据通信)-018
Unity keeps the camera behind and above the player
分布式全局ID生成方案