我使用校准的交叉验证来对线性核的SGD分类器进行训练,因为我的损失函数是铰链损失。现在我想获取前10个特征或类别,该怎么做?我尝试使用.coef_
,但它没有返回任何错误信息。
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))
它给出了以下错误
Top10_features=linear_svm_sgd.coef_
AttributeError: ‘SGDClassifier’ object has no attribute ‘coef_’
回答:
在校准模型之前,只需对SGDClassifier进行.fit
操作。
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)
然后你就可以访问到系数了。