python实现基本的机器学习算法系列(1):逻辑回归

# 3个非常重要的公式在此代码中#################################

import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
import time

np.random.seed(1)
X = 2*np.random.rand(5000, 1)   # between 0-1
y = 10 + 2*X + np.random.randn(5000, 1)
plt.figure(figsize=(8, 6))
plt.scatter(X, y)

x_train, x_test, y_train, y_test = train_test_split(X, y)   # 分训练与测试的数据,默认训练集是75%,测试集是25%。
plt.figure(figsize=(8, 6))
plt.subplot(1, 2, 1)
plt.scatter(x_train, y_train)
plt.subplot(1, 2, 2)
plt.scatter(x_test, y_test)
plt.pause(0.5)    # 不要使用plt.show()
plt.close()

# 将训练数据加上1这一列,w与b合并了
x_train_ = np.column_stack((x_train, np.ones(x_train.shape[0])))  # equal:  np.c_[x_train, np.ones(x_train.shape[0])]
x_test_ = np.column_stack((x_test, np.ones(x_test.shape[0])))
class LR:

    def __init__(self):
        self.W = None

    def LSM(self, x, y):
        start = time.time()
        self.W = np.zeros((x.shape[1], 1))
        self.W = np.dot(np.dot(np.linalg.inv(x.T.dot(x)), x.T), y) #######################################
        loss = np.mean((np.dot(x, self.W)-y)**2)
        end = time.time()
        CostTime = end -start
        print("LSM得到的loss为:%.4f\n" % loss)
        print("花费的时间为:%.4f\n" % CostTime)
        return self.W

    def BGD(self, x, y, learning_rate, iter):
        start = time.time()
        self.W = np.zeros((x.shape[1], 1))
        for i in range(iter):
            x_pred = np.dot(x, self.W)
            diff = 2*np.dot(x.T, (x_pred-y))/len(y)   # x.T * (x_pred -y)##########################################
            self.W -= learning_rate*diff

            loss = np.mean((x_pred-y)**2) ######################################################
            if i % 100 == 0:
                print("After %d iter, loss is %.4f" % (i, loss))

        end = time.time()
        CostTime = end - start
        print("花费的时间为:%.4f\n" % CostTime)
        return self.W

    def predict(self, x, w):
        test_pred = np.dot(x,w)
        return test_pred

if __name__ == "__main__":
    lr = LR()
    w_LSM = lr.LSM(x_train_, y_train)
    w_GD = lr.BGD(x_train_, y_train, 0.05, 1000)
    print(w_LSM)
    print("\n")
    print(w_GD)
    print("\n")
    test_pred_LSM = lr.predict(x_test_, w_LSM)
    test_pred_GD = lr.predict(x_test_, w_GD)
    loss_LSM = np.mean((test_pred_LSM-y_test)**2)
    loss_GD = np.mean((test_pred_GD-y_test)**2)
    print("LSM的预测损失为:%.4f" % loss_LSM)
    print("GD的预测损失为:%.4f\n" % loss_GD)

    fig3 = plt.figure(figsize=(8, 6))
    plt.subplot(1, 2, 1)
    plt.scatter(x_test, y_test)
    plt.plot(x_test, test_pred_LSM, c='r')
    plt.title("LSM_pred")

    plt.subplot(1, 2, 2)
    plt.scatter(x_test, y_test)
    plt.plot(x_test, test_pred_GD, c='r')
    plt.title("GD_pred")
    plt.show()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值