Skip to content

Commit 6b381ae

Browse files
kmikejnothman
authored andcommitted
DOC use target_names over named categories in 20newsgroups example (scikit-learn#7423)
1 parent 4da44c8 commit 6b381ae

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

examples/text/document_classification_20newsgroups.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@
122122
remove=remove)
123123
print('data loaded')
124124

125-
categories = data_train.target_names # for case categories == None
125+
# order of labels in `target_names` can be different from `categories`
126+
target_names = data_train.target_names
126127

127128

128129
def size_mb(docs):
@@ -218,16 +219,15 @@ def benchmark(clf):
218219

219220
if opts.print_top10 and feature_names is not None:
220221
print("top 10 keywords per class:")
221-
for i, category in enumerate(categories):
222+
for i, label in enumerate(target_names):
222223
top10 = np.argsort(clf.coef_[i])[-10:]
223-
print(trim("%s: %s"
224-
% (category, " ".join(feature_names[top10]))))
224+
print(trim("%s: %s" % (label, " ".join(feature_names[top10]))))
225225
print()
226226

227227
if opts.print_report:
228228
print("classification report:")
229229
print(metrics.classification_report(y_test, pred,
230-
target_names=categories))
230+
target_names=target_names))
231231

232232
if opts.print_cm:
233233
print("confusion matrix:")

0 commit comments

Comments
 (0)