1

i am using calibrateed cv for my for linear kernal on SGD classifier because my loss is hinge loss . But now i want get the top 10 features or classes , so how to do it , i tried using .coef_ but its not throwing me error.

linear_svm_sgd=SGDClassifier(penalty=penalty,alpha=i,max_iter=1000,class_weight='balanced')
    calibrated_clf= CalibratedClassifierCV(linear_svm_sgd,cv=3, method='sigmoid')

    #fit the model on train and predict its probability 
    clf_model=calibrated_clf.fit(xtrain_bow,ytrain_bow)
    predictl1=clf_model.predict_proba(xtrain_bow)
    fp_rate, tp_rate, thresholds = roc_curve(ytrain_bow, predictl1[:,1])

    #fit the model on cv & predict its probablity
    clf_model=calibrated_clf.fit(xcv_bow,ycv_bow)
    fp_rate_cv, tp_rate_cv, thresholds = roc_curve(ycv_bow,clf_model.predict_proba(xcv_bow)[:,1])

    #saving the value for hyperparamater foe each penalty l1 & l2
    if penalty=="l1":
        auc_valuel1_train.append(auc(fp_rate,tp_rate))
        auc_valuel1_cv.append(auc(fp_rate_cv,tp_rate_cv))
    else:
        auc_valuel2_train.append(auc(fp_rate,tp_rate))
        auc_valuel2_cv.append(auc(fp_rate_cv,tp_rate_cv))

It's giving me following error

 Top10_features=linear_svm_sgd.coef_

AttributeError: 'SGDClassifier' object has no attribute 'coef_'

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Vishal Suryavanshi
  • 355
  • 1
  • 4
  • 15
  • 1
    Possible duplicate of [AttributeError: LinearRegression object has no attribute 'coef\_'](https://stackoverflow.com/questions/38646040/attributeerror-linearregression-object-has-no-attribute-coef) – liamhawkins Feb 22 '19 at 17:28

1 Answers1

2

Before calibrating your model, just .fit the SGDClassifier.

linear_svm_sgd.fit(xtrain_bow, ytrain_bow)
calibrated_clf= CalibratedClassifierCV(linear_svm_sgd,cv=3, method='sigmoid')    
#fit the model on train and predict its probability 
clf_model=calibrated_clf.fit(xtrain_bow,ytrain_bow)
predictl1=clf_model.predict_proba(xtrain_bow)

Then you will have access to the coefficients.

Gustavo Fonseca
  • 621
  • 6
  • 9
  • If you suggest to fit `SGDClassifier` before calibration, than you should be using `cv=prefit` option and fit `CalibratedClassifierCV` with data different form the one used for `SGDClassifier.fit()` See the doc https://scikit-learn.org/stable/modules/calibration.html#calibrating-a-classifier – Michael Sep 05 '22 at 06:26