pythonscikit-learn

Scikit dendrogram: How to disable ouput?


If I run dendrogram from the scikit libary:

from scipy.cluster.hierarchy import linkage, dendrogram
# ...
X = np.asarray(X)
Z = linkage(X, 'single', 'correlation')
plt.figure(figsize=(16,8))
dendrogram(Z, color_threshold=0.7)

I get a ton of print output in my ipython notebook:

{'color_list': ['g',
  'r',
  'c',
  'm',
  'y',
   ...
   0.70780175324891315,
   0.70172263980890581],
  [0.0, 0.54342622932769225, 0.54342622932769225, 0.0],
  [0.0, 0.46484932243120658, 0.46484932243120658, 0.0],
  ...
  177,
  196,
  82,
  19,
  108]}

How can I disable this? I'm only interested in the actual dendrogram.


Solution

  • to redirect print to nothing:

    import os
    import sys
    f = open(os.devnull, 'w')
    temp = sys.stdout
    sys.stdout = f
    
    # print is disabled here
    
    sys.stdout = temp
    
    # print works again!