当前位置:网站首页>线性回归
线性回归
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()
边栏推荐
- Flink SQL realizes reading and writing redis and dynamically generates hset key
- How to get free traffic in pinduoduo new store and what links need to be optimized in order to effectively improve the free traffic in the store
- "Multimodal" concept
- High voltage leakage relay bld-20
- “多模态”概念
- Taobao Commodity details page API interface, Taobao Commodity List API interface, Taobao Commodity sales API interface, Taobao app details API interface, Taobao details API interface
- 什么是依赖注入(DI)
- bat 批示处理详解
- Flinksql read / write PgSQL
- 集群、分布式、微服务的区别和介绍
猜你喜欢
数据中心为什么需要一套基础设施可视化管理系统
English语法_名词 - 所有格
Unity keeps the camera behind and above the player
Realize GDB remote debugging function between different network segments
随机生成session_id
Paper reading [open book video captioning with retrieve copy generate network]
Common skills and understanding of SQL optimization
消息队列:消息积压如何处理?
Différenciation et introduction des services groupés, distribués et microservices
[论文阅读] Semi-supervised Left Atrium Segmentation with Mutual Consistency Training
随机推荐
Reptile exercises (III)
win配置pm2开机自启node项目
基于NCF的多模块协同实例
Unity让摄像机一直跟随在玩家后上方
纪念下,我从CSDN搬家到博客园啦!
Reading the paper [sensor enlarged egocentric video captioning with dynamic modal attention]
Paper reading [MM21 pre training for video understanding challenge:video captioning with pre training techniqu]
ssm框架的简单案例
Leakage relay jd1-100
"Multimodal" concept
async / await
What is dependency injection (DI)
Web architecture design process
三级菜单数据实现,实现嵌套三级菜单数据
Flinksql 读写pgsql
JSP setting header information export to excel
Leakage relay llj-100fs
消息队列:消息积压如何处理?
Taobao store release API interface (New), Taobao oauth2.0 store commodity API interface, Taobao commodity release API interface, Taobao commodity launch API interface, a complete set of launch store i
Three level menu data implementation, nested three-level menu data