当前位置:网站首页>线性回归
线性回归
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()
边栏推荐
- Go 语言的 Context 详解
- 5. Data access - entityframework integration
- 【已解决】记一次EasyExcel的报错【读取xls文件时全表读不报错,指定sheet名读取报错】
- 毕业之后才知道的——知网查重原理以及降重举例
- 关于服装ERP,你知道多少?
- Jhok-zbl1 leakage relay
- Wechat applet Bluetooth connects hardware devices and communicates. Applet Bluetooth automatically reconnects due to abnormal distance. JS realizes CRC check bit
- ForkJoin最全详解(从原理设计到使用图解)
- 上海字节面试问题及薪资福利
- Distributed global ID generation scheme
猜你喜欢
Three level menu data implementation, nested three-level menu data
Flink SQL realizes reading and writing redis and dynamically generates hset key
爬虫练习题(三)
Paper reading [semantic tag enlarged xlnv model for video captioning]
毕业之后才知道的——知网查重原理以及降重举例
5. 数据访问 - EntityFramework集成
基于NCF的多模块协同实例
Cve-2021-3156 vulnerability recurrence notes
JD commodity details page API interface, JD commodity sales API interface, JD commodity list API interface, JD app details API interface, JD details API interface, JD SKU information interface
Jhok-zbg2 leakage relay
随机推荐
得物客服一站式工作台卡顿优化之路
【Shell】清理nohup.out文件
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
4. 对象映射 - Mapping.Mapster
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
Message queuing: how to ensure that messages are not lost
[论文阅读] A Multi-branch Hybrid Transformer Network for Corneal Endothelial Cell Segmentation
Flinksql 读写pgsql
SAP webservice 测试出现404 Not found Service cannot be reached
2pc of distributed transaction solution
“多模态”概念
5. Data access - entityframework integration
数据中心为什么需要一套基础设施可视化管理系统
拼多多商品详情接口、拼多多商品基本信息、拼多多商品属性接口
Pinduoduo product details interface, pinduoduo product basic information, pinduoduo product attribute interface
SQL query: subtract the previous row from the next row and make corresponding calculations
Message queue: how to deal with message backlog?
淘宝店铺发布API接口(新),淘宝oAuth2.0店铺商品API接口,淘宝商品发布API接口,淘宝商品上架API接口,一整套发布上架店铺接口对接分享
Go language context explanation
论文阅读【Sensor-Augmented Egocentric-Video Captioning with Dynamic Modal Attention】