EM clustering and printing cluster membership using Weka
Code: "weka_em_clustering.py". Programming language: Python
DMelt Version 2.2. Last modified: 03/03/2021. License: Pro
https://datamelt.org/code/cache/weka_em_clustering_3580.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 clusterer
cls.setOptions(["-N", "2"])
cls.buildClusterer(data) # build the clusterer
print cls
# print cluster membership
from weka.core import Utils
for i in range(data.numInstances()):
cluster = cls.clusterInstance(data.instance(i))
dist = cls.distributionForInstance(data.instance(i))
print ( str(i+1)+" - "+str(cluster)+" - "+Utils.arrayToString(dist))
You see the box below because you did not login.