我有一个执行功能工程和模型选择的管道。在
特征工程与模型选择
from sklearn.svm import LinearSVC
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
特征工程和模型的流水线
^{pr2}$参数选择
from sklearn.model_selection import GridSearchCV
parameters = {'vectorizer__ngram_range': [(1, 1), (1, 2),(2,2)],
'tfidf__use_idf': (True, False)}
gs_clf_svm = GridSearchCV(model, parameters, n_jobs=-1)
gs_clf_svm = gs_clf_svm.fit(X, y)
print(gs_clf_svm.best_score_)
print(gs_clf_svm.best_params_)
使用所选参数准备最终管道
model = Pipeline([('vectorizer', CountVectorizer(ngram_range=(1,2))),
('tfidf', TfidfTransformer(use_idf=True)),
('clf', OneVsRestClassifier(LinearSVC(class_weight="balanced")))])
用训练数据拟合模型 模型.拟合(X车,y车)
保存模型
from sklearn.externals import joblib
joblib.dump(model, 'model_question_topic.pkl', compress=1)
现在在另一个文件中,我正在加载模型和预测
from sklearn.externals import joblib
model = joblib.load('model_question_topic.pkl')
现在它正确地预测了类1
question = "apply leave"
model.predict([question])[0]
但问题是我需要置信率或者百分率
Class1 = 0.8 -- Class2 = 0.05 -- Class3 = 0.05 -- Class4 = 0.1
model.predict_proba([question])[0]
在python3中怎么做?在
目前没有回答
相关问题 更多 >
编程相关推荐