当前位置:网站首页>线性回归
线性回归
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()
边栏推荐
- C#可空类型
- SAP webservice 测试出现404 Not found Service cannot be reached
- 拼多多商品详情接口、拼多多商品基本信息、拼多多商品属性接口
- 不同网段之间实现GDB远程调试功能
- DOM node object + time node comprehensive case
- 判断文件是否为DICOM文件
- Paper reading [open book video captioning with retrieve copy generate network]
- Jhok-zbg2 leakage relay
- 得物客服一站式工作台卡顿优化之路
- AI face editor makes Lena smile
猜你喜欢
随机推荐
【已解决】记一次EasyExcel的报错【读取xls文件时全表读不报错,指定sheet名读取报错】
Getting started with DES encryption
Win configuration PM2 boot auto start node project
The year of the tiger is coming. Come and make a wish. I heard that the wish will come true
Design, configuration and points for attention of network arbitrary source multicast (ASM) simulation using OPNET
Egr-20uscm ground fault relay
不同网段之间实现GDB远程调试功能
ForkJoin最全详解(从原理设计到使用图解)
Design, configuration and points for attention of network unicast (one server, multiple clients) simulation using OPNET
Leetcode: maximum number of "balloons"
zabbix_get测试数据库失败
Mapbox Chinese map address
[reading of the paper] a multi branch hybrid transformer network for channel terminal cell segmentation
Distributed global ID generation scheme
上海字节面试问题及薪资福利
JVM the truth you need to know
三级菜单数据实现,实现嵌套三级菜单数据
[binary tree] binary tree path finding
The navigation bar changes colors according to the route
Mysql-centos7 install MySQL through yum