【问题标题】:How to get the weight vector in Logistic Regression?如何获得逻辑回归中的权重向量?
【发布时间】:2018-04-24 21:18:58
【问题描述】:

我有一个 X 特征矩阵和一个 y 标签矩阵,我正在使用二元逻辑回归,如何在给定矩阵 X 特征和 Y 标签矩阵的情况下获得权重向量 w。我对如何在 sklean 中实现这一点感到有些困惑。

我该如何解决这个问题?

【问题讨论】:

    标签: python numpy machine-learning scikit-learn logistic-regression


    【解决方案1】:

    如果我理解正确,您正在寻找the coef_ attribute

    lr = LogisticRegression(C=1e5)
    lr.fit(X, Y)
    
    print(lr.coef_) # returns a matrix of weights (coefficients)
    

    coef_ 属性的形状应该是:(# of classes, # of features)

    如果您还需要截距(AKA 偏差)列,请使用:

    np.hstack((clf.intercept_[:,None], clf.coef_))
    

    这会给你一个形状数组:(n_classes, n_features + 1)

    【讨论】:

    • 偏差和权重的默认值是多少?
    【解决方案2】:
    clf_bow_perb = LogisticRegression(C= 10, penalty= 'l2')
    clf_bow_perb.fit(X_1,y_1)
    y_pred = clf_bow_perb.predict(X_1)
    print("Accuracy on test set: %0.3f%%"%(accuracy_score(y_1, y_pred)*100))
    print("Non Zero weights:",np.count_nonzero(clf.coef_))
    

    【讨论】:

      猜你喜欢
      • 2018-12-29
      • 1970-01-01
      • 2019-08-17
      • 2020-10-25
      • 2018-05-04
      • 2018-09-20
      • 2015-10-10
      • 2018-07-08
      • 2018-05-28
      相关资源
      最近更新 更多