sklearn 学习: Logistic Regression 中的 coef_ 和 intercept_ 的解释


前言

Logistic Regression是机器学习领域中一种比较简单的学习器,该学习器的目标是根据训练样本的特征标记拟合出如下的映射函数f(X)的系数 w0,w1,w2,wn:

f ( X ) = 1 1 + e − ( w 0 + w 1 x 1 + w 2 x 2 + . . . + w n x n ) f(X)=\frac1{1+e^{-(w_0+w_1x_1+w_2x_2+...+w_nx_n)}} f(X)=1+e(w0+w1x1+w2x2+...+wnxn)1
在应用该模型时,直接将新实例的特征X代入该表达式即可计算出新样本的概率值。
默认设置阈值为0.5可以对概率进行分类。


一、sklearn 中的Logistic Regression

sklearn 工具包提供了 Logistic Regression 的实现,该分类器的两各属性存储了上述系数的值。即 coef_intercept_,其中**coef_**是w1,…,wnintercept_w0

下面使用示例代码辅助理解。

二、示例代码说明

1.引入库函数

import numpy as np
from sklearn.linear_model import LogisticRegression #LR学习器
from sklearn.feature_extraction.text import CountVectorizer # 词汇切分器

2.样本数据

该样本中共有6条数据,每条数据的类别为1或者0

corpus = [
    'This is the first document.',
    'This document is the second document.',
    'And this is the third one.',
    'Is this the first document?',

    'This document is the second document.',
    'and this is the fourth one'
]

labels = [1, 0, 1, 1, 0, 1]

3.数据集划分

将前4条划分为训练集,后2条划分为测试集
提取训练集中出现的单词,将每个单词看作一个特征,共有9个特征。同时,所有训练数据被转换成向量表示,每个向量中的数字代表某个单词出现的次数。

labels = [1, 0, 1, 1, 0, 1]

vector = CountVectorizer()
# 划分训练集和测试集
X_train = vector.fit_transform(corpus[:4])
X_test = vector.transform(corpus[-2:])

y_train = labels[:4]
y_test = labels[-2:]

print(vector.get_feature_names())
# ['and', 'document', 'first', 'is', 'one', 'second', 'the', 'third', 'this']

print(X_train.toarray())
"""
[[0 1 1 1 0 0 1 0 1]
 [0 2 0 1 0 1 1 0 1]
 [1 0 0 1 1 0 1 1 1]
 [0 1 1 1 0 0 1 0 1]]
"""

4.模拟LR的计算

我们在LRModel中根据第一节的公式为每个新的实例计算出它被预测为1的概率值。和sklearn中原始的模型值比较,看它们是否相等。

# 模拟LR模型预测实例值为1的概率
def LRModel(test_instance, coefficient_, intercept_):
    probability = []
    for instance in test_instance:
        predict_prob = 1 / (1 + np.exp(-(np.dot(instance, coefficient_) + intercept_)))
        probability.append(predict_prob)
    return probability

5.应用和比较

比较原始模型的概率计算结果和我们模拟的结果,发现其值是相等的:
样例5 [‘This document is the second document.’], 将其预测为1的概率 约0.49,分类结果为0。
样例6 [‘and this is the fourth one’],将其预测为1的概率 约0.87,分类结果为1。
验证了上述介绍的LR的设计原理,。

# 模拟LR模型预测实例值为1的概率
def main():
    # 定义Logistic模型并进行训练
    clf = LogisticRegression().fit(X_train, y_train)
    # 提取出学习器的系数和截距
    coefficients = clf.coef_[0]
    intercept = clf.intercept_[0]
	# 原始结果的概率
    original_result = clf.predict_proba(X_test)
    # 我们模拟的概率
    simulated_result = LRModel(X_test.toarray(), coefficients, intercept)

    print(original_result)
    # 值为0的概率,值为1的概率
    # [[0.50625194 0.49374806]
 	# [0.1279991  0.8720009 ]]

    print(simulated_result)
    # [0.49374806142939265, 0.8720008951321764]

if __name__ == '__main__':
    main()

致谢

感谢各位阅读,欢迎评论交流!

  • 22
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值