当前位置:网站首页>linear regression
linear regression
2022-07-07 05:47:00 【Python code doctor】
import numpy as np
from matplotlib import pyplot as plt
# Define training data
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
# forecast
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)
# The plot
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()
边栏推荐
- Flink SQL realizes reading and writing redis and dynamically generates hset key
- 分布式全局ID生成方案
- Web authentication API compatible version information
- 原生小程序 之 input切换 text与password类型
- async / await
- 高级程序员必知必会,一文详解MySQL主从同步原理,推荐收藏
- WEB架构设计过程
- Message queue: how to handle repeated messages?
- Paper reading [open book video captioning with retrieve copy generate network]
- 分布式事务解决方案之2PC
猜你喜欢
随机推荐
微信小程序蓝牙连接硬件设备并进行通讯,小程序蓝牙因距离异常断开自动重连,js实现crc校验位
Bat instruction processing details
消息队列:如何确保消息不会丢失
SQL query: subtract the previous row from the next row and make corresponding calculations
判断文件是否为DICOM文件
【已解决】记一次EasyExcel的报错【读取xls文件时全表读不报错,指定sheet名读取报错】
bat 批示处理详解
基于NCF的多模块协同实例
Mysql-centos7 install MySQL through yum
How does mapbox switch markup languages?
SQLSTATE[HY000][1130] Host ‘host. docker. internal‘ is not allowed to connect to this MySQL server
[PM products] what is cognitive load? How to adjust cognitive load reasonably?
How digitalization affects workflow automation
《HarmonyOS实战—入门到开发,浅析原子化服务》
JVM the truth you need to know
[binary tree] binary tree path finding
随机生成session_id
分布式全局ID生成方案
TCC of distributed transaction solutions
In memory, I moved from CSDN to blog park!