【问题标题】:sklearn (scikit-learn) logistic regression package -- set trained coefficients for classification.sklearn (scikit-learn) 逻辑回归包——设置训练的分类系数。
【发布时间】:2012-01-22 06:33:47
【问题描述】:

于是我阅读了 scikit-learn 包 webpate:

http://scikit-learn.sourceforge.net/dev/modules/generated/sklearn.linear_model.LogisticRegression.html

我可以使用逻辑回归来拟合数据,在获得一个 LogisticRegression 实例后,我可以使用它对新的数据点进行分类。到目前为止,一切都很好。

有没有办法设置 LogisticRegression() 实例的系数?因为在我获得训练好的系数后,我想使用相同的 API 对新的数据点进行分类。

或者也许其他人推荐了另一个具有更好 API 的 Python 机器学习包?

谢谢

【问题讨论】:

    标签: python machine-learning scikits scikit-learn


    【解决方案1】:

    确实,estimator.coef_estimator.intercept_ 属性是只读的 Python 属性,而不是通常的 Python 属性。它们的值来自estimator.raw_coef_数组,其内存布局直接映射底层liblinear逻辑回归的C++实现的预期内存布局,以避免在调用estimator.predictestimator.predict_proba时出现任何参数的内存复制。

    我同意拥有只读属性是一个限制,我们应该找到一种方法来摆脱这些属性,但是如果我们重构这个实现,我们还应该注意不要引入任何不必要的内存副本,这不是微不足道的在快速查看源代码之后。

    我在追踪器上打开了an issue,不要忘记这个限制。

    同时你可以阅读@property注解estimator.coef_方法来了解estimator.coef_estimator.raw_coef_是如何关联的,并直接更改estimator.raw_coef_中的值。

    【讨论】:

      【解决方案2】:

      系数是估计器对象的属性——你在实例化 Logistic 回归类时创建的——所以你可以用普通的 python 方式访问它们:

      >>> import numpy as NP
      >>> from sklearn import datasets
      >>> from sklearn import datasets as DS
      >>> digits = DS.load_digits()
      >>> D = digits.data
      >>> T = digits.target
      
      >>> # instantiate an estimator instance (classifier) of the Logistic Reg class
      >>> clf = LR()
      >>> # train the classifier
      >>> clf.fit( D[:-1], T[:-1] )
          LogisticRegression(C=1.0, dual=False, fit_intercept=True, 
            intercept_scaling=1, penalty='l2', tol=0.0001)
      
      >>> # attributes are accessed in the normal python way
      >>> dx = clf.__dict__
      >>> dx.keys()
          ['loss', 'C', 'dual', 'fit_intercept', 'class_weight_label', 'label_', 
           'penalty', 'multi_class', 'raw_coef_', 'tol', 'class_weight', 
           'intercept_scaling']
      

      这就是获得系数的方法,但如果您只想使用这些系数进行预测,更直接的方法是使用估计器的预测 方法:

      >>> # instantiate the L/R classifier, passing in norm used for penalty term 
      >>> # and regularization strength
      >>> clf = LR(C=.2, penalty='l1')
      >>> clf
          LogisticRegression(C=0.2, dual=False, fit_intercept=True, 
            intercept_scaling=1, penalty='l1', tol=0.0001)
      
      >>> # select some "training" instances from the original data
      >>> # [of course the model should not have been trained on these instances]
      >>> test = NP.random.randint(0, 151, 5)
      >>> d = D[test,:]     # random selected data points w/o class labels
      >>> t = T[test,:]     # the class labels that correspond to the points in d
      
      >>> # generate model predictions for these 5 data points
      >>> v = clf.predict(d)
      >>> v
          array([0, 0, 2, 0, 2], dtype=int32)
      >>> # how well did the model do?
      >>> percent_correct = 100*NP.sum(t==v)/t.shape[0]
      >>> percent_correct
          100
      

      【讨论】:

      • 嗨,道格。我了解如何拟合数据并获取系数,但是在拟合数据之后,我想 set 系数使用 predict() API 一遍又一遍地对新数据进行分类,而不必拟合再次数据。有没有办法在你的例子中设置 clf 实例的系数?像 clf.set_coefficients(), clf.set_interception() 吗?谢谢。
      • 嗨——我不确定我是否关注。要进行预测(无需重新训练分类器),您只需要在训练分类器后保持分类器 - 即,我们可以使用上面的 sn-p 进行预测。您可以一遍又一遍地调用 clf 对象上的“预测”方法,而无需重新训练。这就是你想要的,不是吗?如果您对此感到担忧,只需检查 clf.__dict__ 中的值,您会发现它没有改变。现在,如果你想重新设置系数,你可以这样做,因为 clf.__dict__ 只是一个 python 字典,你可以设置与任何键关联的值。
      • 道格,感谢您的回复。是的,我的问题正是关于如何持久化分类器。但是 ogrisel 在他的回复中回答了我的问题。非常感谢您的宝贵时间!
      猜你喜欢
      • 2015-11-11
      • 2021-01-09
      • 2016-02-01
      • 2016-07-31
      • 2013-09-30
      • 2017-03-31
      • 2019-04-11
      • 2018-10-29
      • 2018-05-01
      相关资源
      最近更新 更多