第一个机器学习的算法——LinerRegression线性回归
以下代码仅供机器学习新手参考:
# 导入相关包与库`在这里插入代码片`
from sklearn.linear_model import LinearRegression
import numpy as np
# 使用sklearn.linear_model实现线性回归
class sklearn_LR:
def __init__(self, x_train, y_train):
self.x = x_train
self.y = y_train
def test(self):
# 训练模型
model = LinearRegression()
model.fit(self.x, self.y)
# 预测
x_test = np.array([8, 2, 9, 0, 11, 2, 16, 2, 12, 0]).reshape(5, 2)
y_test = [11, 8.5, 15, 18, 11]
predictions = model.predict(x_test)
for i, prediction in enumerate(predictions):
print('预测值:%s,实际值:%s' % (prediction, y_test[i]))
return (model.coef_, model.intercept_) # 返回权重w和截距b
# print(sklearn_LR)
# 自造轮子实现线性回归
class customize_LR:
def __init__(self, x_train, y_train):
self.x = x_train
self.y = y_train
# 输入训练样本数据集
# 给定一个w,b来产生真实的y;如果你有完整的训练数据x与y,则不需要使用此函数
def f(x):
w = np.array([1, 3, 2])
b = 10
return np.dot(x, w.T) + b
# 计算损失函数值
def cost(self, w, b):
m = self.x.shape[0] # 确定样本数量
y = np.dot(self.x, w) + b # 确定预测的y值
y_prediction = y.reshape(m, 1)
cost = np.sum(np.square(y_prediction - self.y)) / (2 * m)
return cost
# 梯度下降求w,b
def gradient_descent(self, w, b, learning_rate):
m = self.x.shape[0]
w = w - learning_rate * (1 / m) * self.x.T.dot(np.dot(self.x, w) + b - self.y)
b = b - learning_rate * (1 / m) * np.sum(np.dot(self.x, w) + b - self.y)
return [w, b]
def main(self):
n = self.x.shape[1]
# 定义初始参数
w = np.ones((n, 1), dtype=float).reshape(n, 1)
b = 0.0
# 定义学习率和迭代次数
learning_rate = 0.01
iter_num = 10000
i = 0
J = []
while i <= iter_num:
i += 1
w = customize_LR.gradient_descent(self, w, b, learning_rate)[0]
b = customize_LR.gradient_descent(self, w, b, learning_rate)[1]
j = customize_LR.cost(self, w, b)
J.append(j)
# print('========第%s次迭代========' %str(i-1))
# print('训练的w为:\n',w)
# print('训练的b为:',b)
# plt.plot(J)
# plt.show() #绘制损失函数,调参使用
return [w, b, J]
# 给一组训练样本训练模型
x_train = np.array([6, 2, 8, 1, 10, 0, 14, 2, 18, 0]).reshape(5, 2)
y_train = [[7], [9], [13], [17.5], [18]]
# 打印sklearn.linear_model的预测结果
print('使用sklearn.linear_model预测结果如下: ')
(w, b) = sklearn_LR(x_train, y_train).test()
w = np.array((w, b)[0]).reshape(2, 1)
print('w=%s,b=%s\n' % (w, b))
# 根据自己造的模型训练得出w,b,j
w = np.array(customize_LR(x_train, y_train).main()[0])
b = customize_LR(x_train, y_train).main()[1]
j = customize_LR(x_train, y_train).main()[2][-1] # 最后一次迭代的损失函数值
# 给一组测试数据测试模型(与sklearn_LR中的测试数据一致)
x_test = np.array([8, 2, 9, 0, 11, 2, 16, 2, 12, 0]).reshape(5, 2)
y_test = [11, 8.5, 15, 18, 11]
# 根据自己造的模型得出预测的y并打印结果
y_predictionys = np.dot(x_test, w) + b
print('使用自己的模型预测结果:')
for i, y_prediction in enumerate(y_predictionys):
print('预测值:%s,实际值:%s' % (y_prediction, y_test[i]))
print('w=%s,b=%s,j=%s' % (w, b, j))
运行以上代码得到的结果如下图所示: