import numpy as np
import matplotlib.pyplot as plt
x = np.array([1., 2., 3., 4., 5.])
y = np.array([1., 3., 2., 3., 5.])
plt.scatter(x, y)
plt.axis([0, 6, 0, 6])
plt.show()
用最小二乘法求出a, b
x_mean = np.mean(x)
y_mean = np.mean(y)
fenzi = 0.0
fenmu = 0.0
for x_i, y_i in zip(x, y):
fenzi += (x_i - x_mean) * (y_i - y_mean)
fenmu += (x_i - x_mean) ** 2
a = fenzi / fenmu
b = y_mean - a * x_mean
print(a)
print(b)
0.8
0.39999999999999947
#绘制直线
y_hat = a * x + b
plt.scatter(x, y)
plt.plot(x, y_hat, color = "red")
plt.axis([0, 6, 0, 6])
plt.show()
#新的样本
x_predict = 6
y_predict = a * x_predict + b
y_predict
5.2
使用我们自己封装的SimpleLinearRegression
在同级目录下使用pycharm新建工程play_ML
工程里新建一个python脚本命名为SimpleLinearRegression,写入以下代码
import numpy as np
from .metrics import r2_score
class SimpleLinearRegression1:
def __init__(self):
"""初始化Simple Liner Regression模型"""
self.a_ = None
self.b_ = None
def fit(self, x_train, y_train):
"""根据训练数据集X_train和y_train训练Simple Linear Regression模型"""
assert x_train.ndim == 1, \
"the Simple Linear Regression can only solve single fearture training data"
assert len(x_train) == len(y_train), \
"the size of x_train must be equal to the size of y_train"
x_mean = np.mean(x_train)
y_mean = np.mean(y_train)
fenzi = 0.0
fenmu = 0.0
for x_i, y_i in zip(x_train, y_train):
fenzi += (x_i - x_mean) * (y_i - y_mean)
fenmu += (x_i - x_mean) ** 2
self.a_ = fenzi / fenmu
self.b_ = y_mean - self.a_ * x_mean
return self
def predict(self, x_predict):
"""对于给定的待预测数据集x_predict,返回预测结果向量"""
assert x_predict.ndim == 1, \
"the Simple Linear Regression can only solve single fearture training data"
assert self.a_ is not None and self.b_ is not None, \
"must be fitte before predict!"
return np.array([self._predict(x) for x in x_predict])
def _predict(self, x_single):
"""给定单个预测值x_single,返回预测结果"""
return self.a_ * x_single + self.b_
def __repr__(self):
return "SimpleLinearRegression1()"
class SimpleLinearRegression2:
def __init__(self):
"""初始化Simple Liner Regression模型"""
self.a_ = None
self.b_ = None
def fit(self, x_train, y_train):
"""根据训练数据集X_train和y_train训练Simple Linear Regression模型"""
assert x_train.ndim == 1, \
"the Simple Linear Regression can only solve single feature training data"
assert len(x_train) == len(y_train), \
"the size of x_train must be equal to the size of y_train"
x_mean = np.mean(x_train)
y_mean = np.mean(y_train)
fenzi = (x_train - x_mean).dot(y_train - y_mean)
fenmu = (x_train - x_mean).dot(x_train - x_mean)
self.a_ = fenzi / fenmu
self.b_ = y_mean - self.a_ * x_mean
return self
def predict(self, x_predict):
"""对于给定的待预测数据集x_predict,返回预测结果向量"""
assert x_predict.ndim == 1, \
"the Simple Linear Regression can only solve single feature training data"
assert self.a_ is not None and self.b_ is not None, \
"must be fitted before predict!"
return np.array([self._predict(x) for x in x_predict])
def _predict(self, x_single):
"""给定单个预测值x_single,返回预测结果"""
return self.a_ * x_single + self.b_
def score(self, x_test, y_test):
"""根据测试数据集x_test和y_test确定当前模型的准确度"""
y_predict = self.predict(x_test)
return r2_score(y_test, y_predict)
def __repr__(self):
return "SimpleLinearRegression2()"
导入自定义的回归函数
from play_ML.SimpleLinearRegression import SimpleLinearRegression1
reg1 = SimpleLinearRegression1()
reg1.fit(x, y)
SimpleLinearRegression1()
reg1.predict(np.array([x_predict]))
array([5.2])
reg1.a_
0.8
reg1.b_
0.39999999999999947
#得到模型
y_hat1 = reg1.predict(x)
#绘图
plt.scatter(x, y)
plt.plot(x, y_hat1, color = "red")
plt.axis([0, 6, 0, 6])
plt.show()
向量化运算 实现SimpleLinearRegression
from play_ML.SimpleLinearRegression import SimpleLinearRegression2
reg2 = SimpleLinearRegression2()
reg2.fit(x, y)
SimpleLinearRegression2()
reg2.a_
0.8
reg2.b_
0.39999999999999947
#得到模型
y_hat2 = reg2.predict(x)
#绘图
plt.scatter(x, y)
plt.plot(x, y_hat1, color = "red")
plt.axis([0, 6, 0, 6])
plt.show()
向量化实现的性能测试
m = 1000000
big_x = np.random.random(size=m)
big_y = big_x * 2.0 + 3 + np.random.normal(size=m)
%timeit reg1.fit(big_x, big_y)
%timeit reg2.fit(big_x, big_y)
639 ms ± 11 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
7.76 ms ± 197 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
print(reg1.a_)
print(reg1.b_)
2.0045382114974615
2.9982778832590267
print(reg2.a_)
print(reg2.b_)
2.0045382114972243
2.9982778832591452
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix