繁体   English   中英

sklearn(scikit-learn)逻辑回归包 - 设置训练的分类系数。

[英]sklearn (scikit-learn) logistic regression package — set trained coefficients for classification.

所以我读了scikit-learn包webpate:

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

我可以使用逻辑回归来拟合数据,在获得LogisticRegression实例后,我可以使用它来对新数据点进行分类。 到现在为止还挺好。

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

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

谢谢

系数是估算器对象的属性 - 您在实例化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

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

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

我在跟踪器上打开了一个问题 ,不要忘记这个限制。

与此同时,你可以阅读@property注释estimator.coef_方法来了解estimator.coef_estimator.raw_coef_是相关的,在更改值estimator.raw_coef_直接。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM