当前位置:网站首页>linear regression
linear regression
2022-07-07 05:47:00 【Python code doctor】
import numpy as np
from matplotlib import pyplot as plt
# Define training data
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
# forecast
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)
# The plot
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. 数据访问 - EntityFramework集成
EMMC打印cqhci: timeout for tag 10提示分析与解决
How does mapbox switch markup languages?
High voltage leakage relay bld-20
Five core elements of architecture design
【日常训练--腾讯精选50】235. 二叉搜索树的最近公共祖先
Mapbox Chinese map address
The navigation bar changes colors according to the route
[论文阅读] A Multi-branch Hybrid Transformer Network for Corneal Endothelial Cell Segmentation
随机推荐
Harmonyos practice - Introduction to development, analysis of atomized services
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
Preliminary practice of niuke.com (9)
Message queuing: how to ensure that messages are not lost
C nullable type
论文阅读【Open-book Video Captioning with Retrieve-Copy-Generate Network】
Mybaits multi table query (joint query, nested query)
淘寶商品詳情頁API接口、淘寶商品列錶API接口,淘寶商品銷量API接口,淘寶APP詳情API接口,淘寶詳情API接口
数字IC面试总结(大厂面试经验分享)
Tablayout modification of customized tab title does not take effect
I didn't know it until I graduated -- the principle of HowNet duplication check and examples of weight reduction
消息队列:如何确保消息不会丢失
How digitalization affects workflow automation
如何提高网站权重
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
JVM the truth you need to know
Introduction to distributed transactions
软件测试面试技巧
Nodejs get client IP
成为资深IC设计工程师的十个阶段,现在的你在哪个阶段 ?