机器学习案例——梯度下降与逻辑回归简单实例

梯度下降实例

    下图是f(x) = x2+3x+4 的函数图像,这是初中的一元二次函数,它的导数为g(x) = f(x) = 2x+3。我们很明确的知道,当x = -1.5时,函数取得最小值。

image

    下面就通过梯度下降法来计算函数取最小值时x的取值。

# @Desc  : 梯度下降计算函数的极小值

"""
f(x) = x^2 + 3x + 4
f(x)的导数  g(x) = 2x + 3
"""
def test():
    def derivative(x_pre):  # f(x)的导数
        return 2 * x_pre + 3

    x_pre = -5  # 初始值
    x_now = 1  # 梯度下降初始值
    alpha = 0.01  # 学习率,即步长
    pression = 0.00001  # 更新的阀值
    count = 0  # 统计迭代次数

    while abs(x_now - x_pre) > pression:
        x_pre = x_now
        # x = x - αg(x),g(x)为f(x)的导数
        x_now = x_pre - alpha * derivative(x_pre)
        count += 1
    print(x_now)
    print(count)


# 结果
-1.4995140741236221
423

    可以看出,已经非常逼近真实的极值-1.5了,总共经过了423次迭代,逻辑回归中的梯度下降也是这样的,只不过函数没这么简单而已。

逻辑回归实例

    自己在编写代码过程中,使用的是机器学习案例——鸢尾花数据集分析中提到的鸢尾花数据集,我把其中一个类别删除了,只留下了两个类别。经过训练后,得到参数如下图所示。

image

    上面的数据有4个特征,不太好展示,因此重新用了别人家的数据,代码无需变动。建议还是参考别人写的更详细(地址在文章末尾),最后得到的参数是下面这个样子的,画出来的拟合效果还是不错的,为了不影响阅读代码贴在最后了。

[[-0.47350404]
 [ 0.74213291]
 [-5.47457118]]

image

参考内容:

    https://github.com/apachecn/MachineLearning/blob/master/src/py2.x/ML/5.Logistic/logistic.py

    http://cuijiahua.com/blog/2017/11/ml_6_logistic_1.html

image

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

alpha = 0.001  # 学习率
iteration = 5000  # 迭代次数


# 加载数据集
def load_data():
    # 读入数据
    df = pd.read_csv('test.csv')

    # 取label标签
    Y_train = np.mat(df['class'])

    # 将行向量转换为列向量
    Y_train = np.mat(Y_train)
    Y_train = Y_train.T

    # 删除最后一列,即删除标签列
    df.drop('class', axis=1, inplace=True)

    # 添加一列,当b吧,方便计算,初始化为1
    df['new'] = 1
    X_train = np.mat(df)
    return X_train, Y_train


# 返回最后训练的参数
def gradient_descent(X_train, Y_train):
    row, col = X_train.shape

    # 初始化,全为0
    W = np.zeros((col, 1))

    # 进行max_iteration次梯度下降
    for i in range(iteration):
        # 直接使用numpy提供的tanh函数
        h = np.tanh(np.dot(X_train, W))
        error = Y_train + h

        # 梯度下降
        W = W - alpha * np.dot(X_train.T, error)
    return W.getA()


# 这段代码来抄自https://github.com/apachecn/MachineLearning/blob/master/src/py2.x/ML/5.Logistic/logistic.pyu
def plot_show(W):
    X_train, Y_train = load_data()
    xcord1 = []
    ycord1 = []

    xcord2 = []
    ycord2 = []

    for i in range(X_train.shape[0]):
        if int(Y_train[i]) == 1:
            xcord1.append(X_train[i, 0])
            ycord1.append(X_train[i, 1])
        else:
            xcord2.append(X_train[i, 0])
            ycord2.append(X_train[i, 1])

    fig = plt.figure()

    ax = fig.add_subplot(111)
    ax.scatter(xcord1, ycord1, s=20, c='red', marker='s', alpha=.5)
    ax.scatter(xcord2, ycord2, s=20, c='green', alpha=.5)

    x = np.arange(-4.0, 5.0, 0.1)
    """
    函数原型是:f(x) = w0*x0 + w1*x1 + b
    x1在这里被当做y值了,f(x)被算到w0、w1、b身上去了
    所以有:w0*x0 + w1*x1 + b = 0
    可以得到:(b + w0 * x) / -w1
    """
    y = (W[2] + W[0] * x) / -W[1]

    ax.plot(x, y)
    plt.title('BestFit')
    plt.xlabel('X1')
    plt.ylabel('X2')

    plt.show()


if __name__ == '__main__':
    X_train, Y_train = load_data()
    # print(Y_train)
    W = gradient_descent(X_train, Y_train)
    print(W)
    plot_show(W)
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Guanngxu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值