【问题标题】:how initial bias value is chosen in sklearn logistic regression?在 sklearn 逻辑回归中如何选择初始偏差值?
【发布时间】:2021-05-04 01:58:52
【问题描述】:

在训练逻辑回归时,它会经历一个迭代过程,在每个过程中,它都会计算 x 变量的权重和偏差值以最小化损失函数。

来自sklearn官方代码class LogisticRegression | linear model in scikit-learn,逻辑回归类的拟合方法如下

def fit(self, X, y, sample_weight=None):
    """
    Fit the model according to the given training data.
    Parameters
    ----------
    X : {array-like, sparse matrix} of shape (n_samples, n_features)
        Training vector, where n_samples is the number of samples and
        n_features is the number of features.
    y : array-like of shape (n_samples,)
        Target vector relative to X.
    sample_weight : array-like of shape (n_samples,) default=None
        Array of weights that are assigned to individual samples.
        If not provided, then each sample is given unit weight.
        .. versionadded:: 0.17
           *sample_weight* support to LogisticRegression.

我猜sample_weight = weight 的 x 个变量如果没有被设置为 1,那么偏差值是否也是 1?

【问题讨论】:

    标签: python scikit-learn logistic-regression


    【解决方案1】:

    您听起来有些困惑,也许是在寻找与神经网络的权重和偏差进行类比。但这种情况并非如此; sample_weight 这里与神经网络的权重无关,即使是一个概念。

    sample_weight 的存在是为了,如果(业务)问题需要,我们可以给予某些样本比其他样本更大的权重(即更多重要性),而这种重要性直接影响失利。它有时用于数据不平衡的情况;引用documentation 的实用技巧部分(它是关于决策树的,但原理相同):

    可以通过从每个类别中抽取相同数量的样本来实现类别平衡,或者最好将每个类别的样本权重之和 (sample_weight) 归一化为相同的值。

    来自Cross Validated的相关主题:

    样本权重用于增加单个数据点的重要性(假设您的某些数据更可信,那么它们会获得更高的权重)。所以:样本权重的存在是为了改变数据点的重要性

    您可以在 SO 线程 What does `sample_weight` do to the way a `DecisionTreeClassifier` works in sklearn? 中看到更改某些样本的权重如何改变最终模型的实际演示(同样,它是关于决策树,但基本原理相同)。

    澄清后,现在应该很明显,这里没有任何“偏差”参数的空间。实际上,您问题中的介绍性段落是错误的:逻辑回归确实不计算此类权重和偏差;它返回 coefficients 和一个 intercept 项(有时本身称为 bias),而这些系数和截距与 sample_weight 无关。

    【讨论】:

    • 我同意您的评论,即“coef 和 int 与 sample_weights 无关”。但我确信logisticRegression计算x变量的权重,这是训练的重点,为每个x变量获取权重。 web.stanford.edu/~jurafsky/slp3/5.pdf 看看吧。
    • @Ambleu 这是术语的问题; weights 和 bias 这两个术语有时可互换用于 coefficients 和 intercept,分别,但这远非标准。 scikit-learn(您的问题具体是关于)不使用权重/偏差术语,而是使用系数(coef_)和截距(intercept_)之一(docs)跨度>
    猜你喜欢
    • 1970-01-01
    • 2018-02-27
    • 1970-01-01
    • 2015-05-04
    • 2016-03-06
    • 2014-01-20
    • 2016-03-30
    • 2014-01-19
    • 1970-01-01
    相关资源
    最近更新 更多