当前位置:网站首页>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()
边栏推荐
- 盘点国内有哪些EDA公司?
- Flink SQL 实现读写redis,并动态生成Hset key
- SQL query: subtract the previous row from the next row and make corresponding calculations
- Leakage relay jd1-100
- Go 语言的 Context 详解
- Jhok-zbg2 leakage relay
- 2pc of distributed transaction solution
- Message queuing: how to ensure that messages are not lost
- Wechat applet Bluetooth connects hardware devices and communicates. Applet Bluetooth automatically reconnects due to abnormal distance. JS realizes CRC check bit
- make makefile cmake qmake都是什么,有什么区别?
猜你喜欢
随机推荐
2pc of distributed transaction solution
1. AVL tree: left-right rotation -bite
淘宝店铺发布API接口(新),淘宝oAuth2.0店铺商品API接口,淘宝商品发布API接口,淘宝商品上架API接口,一整套发布上架店铺接口对接分享
Unity keeps the camera behind and above the player
Harmonyos practice - Introduction to development, analysis of atomized services
分布式事务解决方案之TCC
What is message queuing?
SQL query: subtract the previous row from the next row and make corresponding calculations
软件测试面试技巧
《ClickHouse原理解析与应用实践》读书笔记(6)
EMMC打印cqhci: timeout for tag 10提示分析与解决
Reading the paper [sensor enlarged egocentric video captioning with dynamic modal attention]
Pinduoduo product details interface, pinduoduo product basic information, pinduoduo product attribute interface
Go 语言的 Context 详解
Lombok plug-in
Paper reading [semantic tag enlarged xlnv model for video captioning]
得物客服一站式工作台卡顿优化之路
Flinksql read / write PgSQL
Web authentication API compatible version information
4. 对象映射 - Mapping.Mapster