【机器学习】西瓜书习题3.3Python编程实现对数几率回归

3.3 编程实现对数几率回归

参考代码
结合自己的理解,添加注释。

代码

  1. 导入相关的库
import numpy as np
import pandas as pd
import matplotlib
from matplotlib import pyplot as plt
from sklearn import linear_model
  1. 导入数据,进行数据处理和特征工程
# 1.数据处理,特征工程
data_path = 'watermelon3_0_Ch.csv'
data = pd.read_csv(data_path).values
# 取所有行的第10列(标签列)进行判断
is_good = data[:,9] == '是'
is_bad = data[:,9] == '否'
# 按照数据集3.0α,强制转换数据类型
X = data[:,7:9].astype(float)
y = data[:,9]
y[y=='是'] = 1
y[y=='否'] = 0
y = y.astype(int)
  1. 定义若干需要使用的函数
    y = 1 1 + e − x y= \frac{1}{1+e^{-x}} y=1+ex1
def sigmoid(x):
    """
    构造对数几率函数,它是一种sigmoid函数
    """
    s = 1/(1+np.exp(-x))
    return s

ℓ ( β ) = ∑ i = 1 m ( − y i β T x ^ i + l n ( 1 + e β T x ^ i ) ) \ell(\beta) = \sum_{i=1}^{m}(-y_{i}\beta^{T} \hat{x}_{i} + ln(1+e^{\beta^{T} \hat{x}_{i}})) (β)=i=1m(yiβTx^i+ln(1+eβTx^i))

def J_cost(X,y,beta):
    """
    :param X:  sample array, shape(n_samples, n_features)
    :param y: array-like, shape (n_samples,)
    :param beta: the beta in formula 3.27 , shape(n_features + 1, ) or (n_features + 1, 1)
    :return: the result of formula 3.27
    """
    # 构造x_hat,np.c_ 用于连接两个矩阵,规模是(X.row行,X.column+1列)
    X_hat = np.c_[X, np.ones((X.shape[0],1))]
    # β和y均reshape为1列,规模是(X.column+1行,1列)
    beta = beta.reshape(-1,1)
    y = y.reshape(-1,1)
    
    # 计算最大化似然函数的相反数
    L_beta = -y * np.dot(X_hat,beta) + np.log(1+np.exp(np.dot(X_hat,beta)))
    # 返回式3.27的结果
    return  L_beta.sum()

β = ( w ; b ) \beta = (w; b) β=(w;b)

def initialize_beta(column):
    """
    初始化β,对应式3.26的假设,规模是(X.column+1行,1列),x_hat规模是(17行,X.column+1列)
    """
    # numpy.random.randn(d0,d1,…,dn)
    # randn函数返回一个或一组样本,具有标准正态分布。标准正态分布又称为u分布,是以0为均值、以1为标准差的正态分布,记为N(0,1)
    # dn表格每个维度
    # 返回值为指定维度的array
    beta = np.random.randn(column+1,1)*0.5+1
    return beta

∂ ℓ ( β ) ∂ β = − ∑ i = 1 m x ^ i ( y i − p 1 ( x ^ i ; β ) ) \frac{\partial \ell(\beta)}{\partial \beta} = -\sum_{i=1}^{m}\hat{x}_{i}(y_{i}-p_{1}(\hat{x}_{i};\beta)) β(β)=i=1mx^i(yip1(x^i;β))

def gradient(X,y,beta):
    """
    compute the first derivative of J(i.e. formula 3.27) with respect to beta      i.e. formula 3.30
    计算式3.27的一阶导数
    ----------------------------------------------------
    :param X: sample array, shape(n_samples, n_features)
    :param y: array-like, shape (n_samples,)
    :param beta: the beta in formula 3.27 , shape(n_features + 1, ) or (n_features + 1, 1)
    :return:
    """
    # 构造x_hat,np.c_ 用于连接两个矩阵,规模是(X.row行,X.column+1列)
    X_hat = np.c_[X, np.ones((X.shape[0],1))]
    # β和y均reshape为1列,规模是(X.column+1行,1列)
    beta = beta.reshape(-1,1)
    y = y.reshape(-1,1)
    # 计算p1(X_hat,beta)
    p1 = sigmoid(np.dot(X_hat,beta))
    
    gra = (-X_hat*(y-p1)).sum(0)
    
    return gra.reshape(-1,1) 

∂ 2 ℓ ( β ) ∂ β ∂ β T = ∑ i = 1 m x ^ i x ^ i T p 1 ( x ^ i ; β ) ( 1 − p 1 ( x ^ i ; β ) ) \frac{\partial^2 \ell(\beta)}{\partial \beta \partial \beta^T} = \sum_{i=1}^{m}\hat{x}_{i}\hat{x}_{i}^Tp_{1}(\hat{x}_{i};\beta)(1-p_{1}(\hat{x}_{i};\beta)) ββT2(β)=i=1mx^ix^iTp1(x^i;β)(1p1(x^i;β))

def hessian(X,y,beta):
    '''
    compute the second derivative of J(i.e. formula 3.27) with respect to beta      i.e. formula 3.31
    计算式3.27的二阶导数
    ----------------------------------
    :param X: sample array, shape(n_samples, n_features)
    :param y: array-like, shape (n_samples,)
    :param beta: the beta in formula 3.27 , shape(n_features + 1, ) or (n_features + 1, 1)
    :return:
    '''
    # 构造x_hat,np.c_ 用于连接两个矩阵,规模是(X.row行,X.column+1列)
    X_hat = np.c_[X, np.ones((X.shape[0],1))]
    # β和y均reshape为1列,规模是(X.column+1行,1列)
    beta = beta.reshape(-1,1)
    y = y.reshape(-1,1)
    # 计算p1(X_hat,beta)
    p1 = sigmoid(np.dot(X_hat,beta))
    
    m,n=X.shape
    # np.eye()返回的是一个二维2的数组(N,M),对角线的地方为1,其余的地方为0.
    P = np.eye(m)*p1*(1-p1)
    assert P.shape[0] == P.shape[1]
    # X_hat.T是X_hat的转置
    return np.dot(np.dot(X_hat.T,P),X_hat)

使用梯度下降法求解

def update_parameters_gradDesc(X,y,beta,learning_rate,num_iterations,print_cost):
    """
    update parameters with gradient descent method
    """
    for i in range(num_iterations):
        grad = gradient(X,y,beta)
        beta = beta - learning_rate*grad
        
        # print_cost为true时,并且迭代为10的倍数时,打印本次迭代的cost
        if (i%10==0)&print_cost:
            print('{}th iteration, cost is {}'.format(i,J_cost(X,y,beta)))
    
    return beta

def logistic_model(X,y,print_cost=False,method='gradDesc',learning_rate=1.2,num_iterations=1000):
    """
    :param method: str 'gradDesc'or'Newton'
    """
    # 得到X的规模
    row,column = X.shape
    # 初始化β
    beta = initialize_beta(column)
    
    if method == 'gradDesc':
        return update_parameters_gradDesc(X,y,beta,learning_rate,num_iterations,print_cost)
    elif method == 'Newton':
        return update_parameters_newton(X,y,beta,print_cost,num_iterations)
    else:
        raise ValueError('Unknown solver %s' % method)
  1. 可视化结果
# 1.可视化数据点
# 设置字体为楷体
matplotlib.rcParams['font.sans-serif'] = ['KaiTi']
plt.scatter(data[:, 7][is_good], data[:, 8][is_good], c='b', marker='o') #c参数是颜色,marker是标记
plt.scatter(data[:, 7][is_bad], data[:, 8][is_bad], c='r', marker='x')
# 设置横轴坐标标题
plt.xlabel('密度')
plt.ylabel('含糖量')

# 2.可视化自己写的模型
# 学习得到模型
beta = logistic_model(X,y,print_cost=True,method='gradDesc',learning_rate=0.3, num_iterations=1000)
# 得到模型参数及偏置(截距)
w1, w2, intercept = beta
x1 = np.linspace(0, 1)
y1 = -(w1 * x1 + intercept) / w2
ax1, = plt.plot(x1, y1, label=r'my_logistic_gradDesc')

# 3.可视化sklearn的对率回归模型,进行对比
lr = linear_model.LogisticRegression(solver='lbfgs', C=1000)  # 注意sklearn的逻辑回归中,C越大表示正则化程度越低。
lr.fit(X, y)
lr_beta = np.c_[lr.coef_, lr.intercept_]
print(J_cost(X, y, lr_beta))
# 可视化sklearn LogisticRegression 模型结果
w1_sk, w2_sk = lr.coef_[0, :]
x2 = np.linspace(0, 1)
y2 = -(w1_sk * x2 + lr.intercept_) / w2
ax2, = plt.plot(x2, y2, label=r'sklearn_logistic')
plt.legend(loc='upper right')
plt.show()

可视化结果如下:
在这里插入图片描述

  • 7
    点赞
  • 67
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
对率回归(Logistic Regression)是一种二分类的机器学习算法,用于预测二元变量的概率。它的目标是找到一条最优的直线(或超平面),将数据集分成两部分,并根据数据集中的特征预测新数据点的类别。 以下是一个简单的Python代码实现对率回归: ```python import numpy as np class LogisticRegression: def __init__(self, learning_rate=0.01, num_iterations=10000, fit_intercept=True, verbose=False): self.learning_rate = learning_rate self.num_iterations = num_iterations self.fit_intercept = fit_intercept self.verbose = verbose def __add_intercept(self, X): intercept = np.ones((X.shape[0], 1)) return np.concatenate((intercept, X), axis=1) def __sigmoid(self, z): return 1 / (1 + np.exp(-z)) def __loss(self, h, y): return (-y * np.log(h) - (1 - y) * np.log(1 - h)).mean() def fit(self, X, y): if self.fit_intercept: X = self.__add_intercept(X) # 初始化权重为0 self.theta = np.zeros(X.shape[1]) for i in range(self.num_iterations): z = np.dot(X, self.theta) h = self.__sigmoid(z) gradient = np.dot(X.T, (h - y)) / y.size self.theta -= self.learning_rate * gradient if self.verbose and i % 1000 == 0: z = np.dot(X, self.theta) h = self.__sigmoid(z) print(f'loss: {self.__loss(h, y)} \t') def predict_prob(self, X): if self.fit_intercept: X = self.__add_intercept(X) return self.__sigmoid(np.dot(X, self.theta)) def predict(self, X, threshold=0.5): return self.predict_prob(X) >= threshold ``` 在实现中,我们使用了numpy库进行矩阵运算,并定义了以下方法: - `__add_intercept`:用于添加截距项(intercept)到数据集X中。 - `__sigmoid`:用于计算sigmoid函数值。 - `__loss`:用于计算损失函数的值。 - `fit`:用于训练模型,更新权重theta。 - `predict_prob`:用于预测数据点的类别概率。 - `predict`:用于预测数据点的类别。 接下来,我们可以使用实现对率回归模型来处理一个简单的二分类问题: ```python from sklearn.datasets import make_classification from sklearn.model_selection import train_test_split # 生成随机数据集 X, y = make_classification(n_samples=100, n_features=2, n_informative=2, n_redundant=0, n_classes=2, random_state=42) # 划分训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) # 训练模型 model = LogisticRegression(learning_rate=0.1, num_iterations=3000) model.fit(X_train, y_train) # 预测测试集 y_pred = model.predict(X_test) # 输出模型的准确率 print(f'Accuracy: {(y_pred == y_test).mean()}') ``` 输出结果: ``` Accuracy: 0.9 ``` 可以看出,我们的实现在这个简单的二分类问题上表现良好。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一个甜甜的大橙子

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

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

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

打赏作者

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

抵扣说明:

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

余额充值