手动推导LogisticRegression建模结果

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
# @Date : 2023/8/25 15:51
# @Author : HELIN

import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression


# 生成虚拟数据集
np.random.seed(42)
X = np.random.randn(100, 2)  # 特征矩阵,假设有2个特征
y = (X[:, 0] + 2 * X[:, 1] > 0).astype(int)  # 标签,根据某个条件生成

# 将数据集分为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=10, random_state=42)
print(X_test)
print(y_test)

# 创建逻辑回归模型
model = LogisticRegression()

# 在训练数据上拟合模型
model.fit(X_train, y_train)

# 获取系数和截距
coefficients = model.coef_
intercept = model.intercept_
print('系数:', coefficients)
print('截距:', intercept)

# 在测试数据上进行预测

score = model.predict_proba(X_test)[:, 1]
print('预测结果:\n', score.tolist())

# 计算线性组合
linear_combination = np.dot(X_test, coefficients.T) + intercept  # .dot 是矩阵乘法

# 计算预测概率
predicted_probability = 1 / (1 + np.exp(-linear_combination))  # sigmoid函数
print("手算结果:\n", predicted_probability[:, 0].tolist())


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值