@@ -0,0 +1,1932 @@
+# Chow-Liu algorithm %0A# Author: Drishtii@%0A# Based on%0A# https://github.com/probml/pmtk3/blob/master/demos/chowliuTreeDemo.m%0A%0A#!pip install pgmpy%0A%0A%0Aimport pandas as pd%0Afrom sklearn.datasets import fetch_20newsgroups%0Aimport networkx as nx%0Afrom pgmpy.estimators import TreeSearch%0Afrom sklearn.feature_extraction.text import CountVectorizer%0Aimport pydot%0Afrom networkx.drawing.nx_pydot import graphviz_layout%0Afrom IPython.display import Image, display%0Aimport pyprobml_utils as pml%0A%0Anewsgroups_train = fetch_20newsgroups(subset='train')%0A%0Alist_of_words = %5B'email','disk','ftp','files','format','image','display','phone','number','card', 'graphics','windows','driver', 'pc','drive', 'memory','scsi', %0A'data','system','problem','dos','program','space','version','win','team', 'won','car','video', 'software','bmw', 'dealer', 'engine', 'honda', 'mac', 'help', 'server',%0A'launch', 'moon', 'nasa', 'orbit', 'shuttle', 'technology', 'fans', 'games', 'hockey', 'league', 'players', 'puck', 'season','oil','lunar','bible','children',%0A'mars', 'earth','god','satellite', 'solar','mission', 'nhl','war','world','science','computer','baseball','hit','christian','power','jesus', 'religion','jews',%0A'government','israel','state','university','research','question','aids','msg','food','water','health','insurance','patients','medicine','studies','case','president',%0A'human','fact','course','rights','law','gun','evidence'%5D%0A%0Acount_vect = CountVectorizer(newsgroups_train.data, vocabulary=list_of_words)   %0AX_train_counts = count_vect.fit_transform(newsgroups_train.data)%0Adf_ = pd.DataFrame.sparse.from_spmatrix(X_train_counts, columns=list_of_words)%0A%0A# Learning graph structure %0Aest = TreeSearch(df_, root_node='email')%0Adag = est.estimate(estimator_type=%22chow-liu%22)%0A%0A# Plot and display%0Adef view_pydot(pdot):%0A    plt = Image(pdot.create_png())%0A    display(plt)%0A%0Ap=nx.drawing.nx_pydot.to_pydot(dag)%0Aview_pydot(p)%0Ap.write_png('../figures/tree_structure.png')

Convert chowliuTreeDemo to python (#519)