当前位置:网站首页>线性回归
线性回归
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()
边栏推荐
- pytorch_ 01 automatic derivation mechanism
- 消息队列:如何确保消息不会丢失
- Paper reading [semantic tag enlarged xlnv model for video captioning]
- Dynamic memory management
- Differences and introduction of cluster, distributed and microservice
- 5. Data access - entityframework integration
- K6el-100 leakage relay
- Tablayout modification of customized tab title does not take effect
- Mybaits multi table query (joint query, nested query)
- The 2022 China low / no code Market Research and model selection evaluation report was released
猜你喜欢
分布式事务解决方案之2PC
Unity让摄像机一直跟随在玩家后上方
Three level menu data implementation, nested three-level menu data
Zero sequence aperture of leakage relay jolx-gs62 Φ one hundred
[PM products] what is cognitive load? How to adjust cognitive load reasonably?
什么是依赖注入(DI)
爬虫练习题(三)
Web architecture design process
Modes of optical fiber - single mode and multimode
Simple case of SSM framework
随机推荐
Nodejs get client IP
Wechat applet Bluetooth connects hardware devices and communicates. Applet Bluetooth automatically reconnects due to abnormal distance. JS realizes CRC check bit
Paper reading [MM21 pre training for video understanding challenge:video captioning with pre training techniqu]
Pinduoduo product details interface, pinduoduo product basic information, pinduoduo product attribute interface
Go 语言的 Context 详解
In memory, I moved from CSDN to blog park!
Cve-2021-3156 vulnerability recurrence notes
Determine whether the file is a DICOM file
Polynomial locus of order 5
The navigation bar changes colors according to the route
Codeforces Round #416 (Div. 2) D. Vladik and Favorite Game
关于服装ERP,你知道多少?
What is dependency injection (DI)
Differences and introduction of cluster, distributed and microservice
pytorch_ 01 automatic derivation mechanism
AI face editor makes Lena smile
C#可空类型
爬虫练习题(三)
上海字节面试问题及薪资福利
随机生成session_id