当前位置:网站首页>线性回归
线性回归
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()
边栏推荐
- 5阶多项式轨迹
- [reading of the paper] a multi branch hybrid transformer network for channel terminal cell segmentation
- Mysql-centos7 install MySQL through yum
- 4. 对象映射 - Mapping.Mapster
- 如何提高网站权重
- 拼多多商品详情接口、拼多多商品基本信息、拼多多商品属性接口
- Design, configuration and points for attention of network arbitrary source multicast (ASM) simulation using OPNET
- 三级菜单数据实现,实现嵌套三级菜单数据
- Paper reading [MM21 pre training for video understanding challenge:video captioning with pre training techniqu]
- App clear data source code tracking
猜你喜欢

什么是依赖注入(DI)

The navigation bar changes colors according to the route

Pytorch builds neural network to predict temperature

High voltage leakage relay bld-20

Hcip eighth operation

Message queuing: how to ensure that messages are not lost

Determine whether the file is a DICOM file

不同网段之间实现GDB远程调试功能

分布式全局ID生成方案

数据中心为什么需要一套基础设施可视化管理系统
随机推荐
English grammar_ Noun possessive
论文阅读【Sensor-Augmented Egocentric-Video Captioning with Dynamic Modal Attention】
什么是依赖注入(DI)
集群、分布式、微服务的区别和介绍
如何提高网站权重
Mapbox Chinese map address
JSP setting header information export to excel
AI人脸编辑让Lena微笑
Make web content editable
Go 语言的 Context 详解
[paper reading] semi supervised left atrium segmentation with mutual consistency training
Nodejs get client IP
sql查询:将下一行减去上一行,并做相应的计算
Lombok plug-in
Leetcode: maximum number of "balloons"
软件测试面试技巧
CVE-2021-3156 漏洞复现笔记
[论文阅读] Semi-supervised Left Atrium Segmentation with Mutual Consistency Training
什么是消息队列?
5阶多项式轨迹