# -*-coding:utf-8-*-
"""
牛顿法实现对率回归(Logistic Regression)
来源: '机器学习, 周志华'
模型: P69 problem 3.3
数据集: P89 watermelon_3.0a (watermelon_3.0a.npy)
"""
import matplotlib.pyplot as plt
import numpy as np
import xlrd
def sigmoid(x):
"""
Sigmoid function.
Input:
x:np.array
Return:
y: the same shape with x
"""
y = 1.0 / (1 + np.exp(-x))
return y
def newton(X, y):
"""
Input:
X: np.array with shape [N, 3]. Input.
y: np.array with shape [N, 1]. Label.
Return:
beta: np.array with shape [1, 3]. Optimal params with newton method
"""
N = X.shape[0]
# initialization
beta = np.ones((1, 3))
# shape [N, 1]
z = X.dot(beta.T)
# log-likehood
old_l = 0
new_l = np.sum(-y * z + np.log(1 + np.exp(z))) # 计算对数似然的代价函数值
iters = 0
while (np.abs(old_l - new_l) > 1e-6):
# shape [N, 1]
p1 = np.exp(z) / (1 + np.exp(z))
# shape [N, N]
p = np.diag((p1 * (
【机器学习】周志华西瓜书3.3题——Python代码实现,牛顿法与梯度下降法
最新推荐文章于 2022-03-15 21:32:17 发布