逻辑回归 使用Numpy实现逻辑回归

使用Numpy实现逻辑回归

sigmoid 函数

g ( z ) = 1 ( 1 + e − z ) g(z)=\frac{1}{(1+e^{−z} )} g(z)=(1+ez)1

# sigmoid 函数
def sigmod(z):
    return 1/(1+np.exp(-z))

线性计算与梯度下降

J ( θ ) = − 1 m [ ∑ i = 1 m y ( i ) l o g ⁡ ( h θ ( x ( i ) ) ) + ( 1 − y ( i ) ) l o g ( 1 − h θ ( x ( i ) ) ) ] J(θ)=-\frac{1}{m}[∑_{i=1}^m y^{(i)} log⁡(h_θ(x^{(i)} ))+(1−y^{(i)}) log(1−h_θ (x^{(i)}))] J(θ)=m1[i=1my(i)log(hθ(x(i)))+(1y(i))log(1hθ(x(i)))]

对于代价函数,采用梯度下降算法求θ的最小值:
θ j ≔ θ j − α ∂ J ( θ ) ∂ θ j θ_j≔θ_j−α\frac{∂J(θ)}{∂θ_j} θj:=θjαθjJ(θ)
代入梯度:
θ j ≔ θ j − α ∑ i = 1 m ( h θ ( x ( i ) ) − y ( i ) ) x j i θ_j≔θ_j−α∑_{i=1}^m(h_θ (x^{(i)} )−y^{(i)} ) x_j^i θj:=θjαi=1m(hθ(x(i))y(i))xji

# 进行计算
def compute(X,y,weights,bias):
    count=len(X)
    linear=np.dot(X,weights)+bias
    
    predictions = sigmoid(linear)
    
    dw=(1/count)*np.dot(X.T,(predictions-y))
    db=(1/count)*np.sum(predictions-y)
    
    return dw,db

def update(weights,bias,dw,db,rate):
    weights=weights-rate*dw
    bias=bias-rate*db
    
    return weights,bias

实现逻辑回归

逻辑回归公式
h θ ( x ) = 1 ( 1 + e − θ T X ) h_θ (x)=\frac{1}{(1+e^{−θ^T X} )} hθ(x)=(1+eθTX)1


#逻辑回归
def logistic(X,y,rate,iterations):
    count,col=X.shape
    weights,bias=initialize(col)
    
    for _ in range(iterations):
        dw,db=compute(X,y,weights,bias)
        
        weights,bias =update(weights,bias,dw,db,rate)
        
    return weights, bias

测试模型

import numpy as np
import matplotlib.pyplot as plt

# 生成两个特征的单调数据
np.random.seed(42)
X1 = np.linspace(1, 5, 100) + np.random.randn(100) * 0.2
X2 = np.linspace(1, 5, 100) + np.random.randn(100) * 0.2
X = np.column_stack((X1, X2))

# 生成对应的标签,假设以直线 x1 = x2 为界限进行二分类
y = (X[:, 0] > X[:, 1]).astype(int)

# 添加偏置项
X_with_bias = np.c_[np.ones((X.shape[0], 1)), X]

# 训练逻辑回归模型
weights, bias = logistic(X_with_bias, y, rate=0.1, iterations=1000)

# 选择一些数据点进行预测
X_predict = np.array([[2, 2], [3, 4], [4, 3]])
predictions = predict(np.c_[np.ones((X_predict.shape[0], 1)), X_predict], weights, bias)

# 输出预测结果
print("Predictions:", predictions)

# 画出二分类后的图
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Paired, marker='o')
plt.scatter(X_predict[:, 0], X_predict[:, 1], marker='x', color='red', label='Predictions')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.title('Binary Classification with Logistic Regression')
plt.legend()
plt.show()

在这里插入图片描述

完整代码

import numpy as np

# sigmoid 函数
def sigmod(z):
    return 1/(1+np.exp(-z))

# 初始化参数
def initialize(num_col):
    weights=np.zeros(num_col)
    bias=0
    return weights,bias

# 进行计算
def compute(X,y,weights,bias):
    count=len(X)
    linear=np.dot(X,weights)+bias
    
    predictions = sigmoid(linear)
    
    dw=(1/count)*np.dot(X.T,(predictions-y))
    db=(1/count)*np.sum(predictions-y)
    
    return dw,db

# 梯度下降
def update(weights,bias,dw,db,rate):
    weights=weights-rate*dw
    bias=bias-rate*db
    
    return weights,bias
    
#逻辑回归
def logistic(X,y,rate,iterations):
    count,col=X.shape
    weights,bias=initialize(col)
    
    for _ in range(iterations):
        dw,db=compute(X,y,weights,bias)
        
        weights,bias =update(weights,bias,dw,db,rate)
        
    return weights, bias

# 预测结果
def predict(X,weights,bias):
    linear = np.dot(X,weights)+bias
    predictions=sigmoid(linear)
    
    return [1 if y_hat>0.5 else 0  for y_hat in predictions]

可视化代码

import numpy as np
import matplotlib.pyplot as plt

# 生成两个特征的单调数据
np.random.seed(42)
X1 = np.linspace(1, 5, 100) + np.random.randn(100) * 0.2
X2 = np.linspace(1, 5, 100) + np.random.randn(100) * 0.2
X = np.column_stack((X1, X2))

# 生成对应的标签,假设以直线 x1 = x2 为界限进行二分类
y = (X[:, 0] > X[:, 1]).astype(int)

# 添加偏置项
X_with_bias = np.c_[np.ones((X.shape[0], 1)), X]

# 训练逻辑回归模型
weights, bias = logistic(X_with_bias, y, rate=0.1, iterations=1000)

# 选择一些数据点进行预测
X_predict = np.array([[2, 2], [3, 4], [4, 3]])
predictions = predict(np.c_[np.ones((X_predict.shape[0], 1)), X_predict], weights, bias)

# 输出预测结果
print("Predictions:", predictions)

# 画出二分类后的图
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Paired, marker='o')
plt.scatter(X_predict[:, 0], X_predict[:, 1], marker='x', color='red', label='Predictions')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.title('Binary Classification with Logistic Regression')
plt.legend()
plt.show()
  • 17
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小小程序○

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

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

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

打赏作者

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

抵扣说明:

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

余额充值