8000 DOC Fix notebook-style for plot_document_clustering.py by gpapadok · Pull Request #22443 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions examples/text/plot_document_clustering.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,16 @@
)

print(__doc__)
op.print_help()
print()


def is_interactive():
return not hasattr(sys.modules["__main__"], "__file__")


if not is_interactive():
op.print_help()
print()

# work-around for Jupyter notebook and IPython console
argv = [] if is_interactive() else sys.argv[1:]
(opts, args) = op.parse_args(argv)
Expand All @@ -134,8 +136,10 @@ def is_interactive():
sys.exit(1)


# #############################################################################
# %%
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the rendering, I see that we always print the documentation if we are using the file as a script.

Could you edit the l.121 to not print it in interactive mode?

# Load some categories from the training set
# ------------------------------------------

categories = [
"alt.atheism",
"talk.religion.misc",
Expand All @@ -156,6 +160,11 @@ def is_interactive():
print("%d categories" % len(dataset.target_names))
print()


# %%
# Feature Extraction
# ------------------

labels = dataset.target
true_k = np.unique(labels).shape[0]

Expand Down Expand Up @@ -214,8 +223,9 @@ def is_interactive():
print()


# #############################################################################
# Do the actual clustering
# %%
# Clustering
# ----------

if opts.minibatch:
km = MiniBatchKMeans(
Expand All @@ -241,6 +251,11 @@ def is_interactive():
print("done in %0.3fs" % (time() - t0))
print()


# %%
# Performance metrics
# -------------------

print("Homogeneity: %0.3f" % metrics.homogeneity_score(labels, km.labels_))
print("Completeness: %0.3f" % metrics.completeness_score(labels, km.labels_))
print("V-measure: %0.3f" % metrics.v_measure_score(labels, km.labels_))
Expand All @@ -253,6 +268,8 @@ def is_interactive():
print()


# %%

if not opts.use_hashing:
print("Top terms per cluster:")

Expand Down
0