係数が推定オブジェクトの属性である - あなたはロジスティック回帰クラスをインスタンス化したときに作成していること - あなたは、通常の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
こんにちは。私はデータをフィットさせて係数を取得する方法を理解していますが、データをフィットさせた後で、データを再度適合させることなく、新しいデータを何度も分類するためにpredict()APIを使用して係数を設定します。あなたの例でclfインスタンスの係数を設定する方法はありますか? clf.set_coefficients()、clf.set_interception()などのように?ありがとう。 – CodeNoob
こんにちは - 私は私に従っているか分からない。クラシファイアを再トレーニングせずに予測するには、トレーニング後にクラシファイアを保持する必要があります。つまり、上記のスニペットを使用して行うことができます。 clfオブジェクトの 'predict'メソッドは、再訓練を何度も繰り返して呼び出すことができます。それはあなたが望んでいるものなのですか?これについて心配している場合は、clf .__ dict__の値をチェックするだけで、変更されていないことがわかります。今度は係数を再設定したい場合は、clf .__ dict__は単にPython辞書であり、任意のキーに関連付けられた値を設定することができます。 – doug
ダグ、ご返信ありがとうございます。はい、私の質問は、分類子を永続化する方法です。しかし、オグリエルは私の質問に答えました。あなたのお時間をありがとうございました! – CodeNoob