 |
EM clustering and cluster visualization using Weka
Source code name: "weka_clustering_vis.py"
Programming language: Python
Topic: Data mining/Clustering
DMelt Version 2.29. Last modified: 03/03/2021. License: Pro
https://datamelt.org/code/cache/weka_clustering_vis_5234.py
To run this script using the DMelt IDE,
copy the above URL link to the menu [File]→[Read script from URL] of the DMelt IDE.
from java.io import FileReader
from weka.core import Instances
from jhplot import Web
xf="weather.arff"
url="https://datamelt.org/examples/data/weka/"+xf
print "Loading ",xf
print Web.get(url)
data = Instances(FileReader(xf))
from weka.clusterers import EM;
cls = EM() # new instance of clusters
cls.setOptions(["-N", "2"])
cls.buildClusterer(data) # build the clusterer
print cls
print "Evaluate.."
from weka.clusterers import ClusterEvaluation
from weka.gui.explorer import ClustererAssignmentsPlotInstances
from weka.gui.visualize import VisualizePanel
val = ClusterEvaluation()
val.setClusterer(cls)
val.evaluateClusterer( data )
cname = cls.getClass().getName()
# setup visualization
plot = ClustererAssignmentsPlotInstances()
plot.setClusterer(cls)
plot.setInstances(data)
plot.setClusterEvaluation(val)
plot.setUp()
vp = VisualizePanel()
vp.setName(cname);
vp.addPlot(plot.getPlotData(cname))
# display data
from javax.swing import JFrame
from java.awt import BorderLayout
jf = JFrame("Weka Clusterer Visualize: " + vp.getName())
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)
jf.setSize(500, 500);
jf.getContentPane().setLayout(BorderLayout())
jf.getContentPane().add(vp, BorderLayout.CENTER)
jf.setVisible(True)
You see the box below because you did not login.