如何在使用校准的交叉验证和线性核的SGD分类器时获取特征权重

我使用校准的交叉验证来对线性核的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)

然后你就可以访问到系数了。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注