【发布时间】:2020-11-18 03:35:18
【问题描述】:
我正在研究一个多类文本分类问题,该问题需要具有相应概率的前 3 个预测标签。我可以使用sklearn predict_proba(),但很难像 table A 那样格式化输出。我的代码如下:
cv = StratifiedKFold(n_splits = 10, random_state = 42, shuffle = None)
pipeline_sgd = Pipeline([
('vect', CountVectorizer()),
('tfdif', TfidfTransformer()),
('nb', CalibratedClassifierCV(base_estimator = SGDClassifier(), cv=cv)),
])
Model = pipeline_sgd.fit(X_train, y_train)
n_top_labels = 3
probas = model.predict_probas(test["text"])
top_n_lables_idx = probas.argsort()[::-1][:n_top_lables]
top_n_probs = probas[top_n_lables_idx]
top_n_labels = label_encoder.inverse_transform(top_n_lables_idx.ravel())
results = list(zip(top_n_labels, top_n_probas))
输出:
[(A, .80),
(B, .10),
(C, .10)]
我在上述输出中遇到的挑战是它没有为我提供每行文本的前 3 个标签/概率。例如,当我对一组新文档(文本)进行推理时,我只得到一个输出,而不是每个文档(行)的输出。
我遇到的第二个挑战是,当我使用 pd.Dataframe(data = results) 将其插入数据帧时,我得到以下信息:
| | 0 | 1 |
|---|---|-----------------|
| 0 | A | [[.80,.10,.10]] |
| 1 | B | [[.85,.10,.05]] |
| 2 | C | [[.70,.20,.10]] |
应该是:
| | 0 | 1 |
|---|-------|-----------------|
| 0 | A,B,C | [[.80,.10,.10]] |
| 1 | B,C,A | [[.85,.10,.05]] |
| 2 | C,B,A | [[.70,.20,.10]] |
表 A
| Text | Predicted labels | Probabilities |
|--------------------------------------------|------------------|----------------|
| Hello World! | A,B,C | [.80,.10,10] |
| Have a nice Day! | B,C,A | [.90,.05,05] |
| It's a wonderful day in the neighborhood. | C,A,B | [.80,.10,10] |
【问题讨论】:
标签: python machine-learning scikit-learn