Clustring IRIS data using k-means using JSAT
Code: "jsat_kmeans.py". Programming language: Python
DMelt Version 2.2. Last modified: 03/03/2021. License: Pro
https://datamelt.org/code/cache/jsat_kmeans_1693.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 File
from jsat import ARFFLoader,DataSet
from jsat.classifiers import ClassificationDataSet
from jsat.clustering import Clusterer
from jsat.clustering.kmeans import KMeans,GMeans,HamerlyKMeans,KMeansPDN,XMeans,XMeans
from jsat.clustering.evaluation import NormalizedMutualInformation
from java.util.stream import IntStream
print "Download iris_org.arff"
from jhplot import *
print Web.get("https://datamelt.org/examples/data/iris_org.arff")
fi=File("iris_org.arff")
dataSet = ARFFLoader.loadArffFile(fi)
# We specify '0' as the class we would like to make the target class.
data = ClassificationDataSet(dataSet, 0)
"""
We will use the NMI as our evaluation criteria. It compares the
clustering results with the class labels. The class labels aren't
necessarily the best ground truth for clusters. In fact, how to
properly evaluate clustering algorithms is a very open question! But
this is a commonly used method.
he ClusterEvaluation interface dictates that values near 0 are
better, and larger values are worse. NMI is usually the opposite, but
obeys the interface. Read the NMI's Javadoc for more details.
"""
evaluator = NormalizedMutualInformation();
"""
We will use a normal k-means algorithm to do clustering
when we specify the number of clusters we want. JSAT implements a
number of different algorithms that all solve the k-means problem,
and are better in different scenarios. This one is likely to be the
best for most users.
"""
simpleKMeans = HamerlyKMeans()
from jarray import zeros
clusteringResults = zeros(data.getSampleSize(), "i")
# try different algorithms now..
def evaluate(methodsToEval):
methodsToEval.cluster(data, clusteringResults)
kFound = IntStream.of(clusteringResults).max().getAsInt()+1
print methodsToEval.toString(), " found=",kFound, " -> ",evaluator.evaluate(clusteringResults, data)
evaluate(KMeansPDN())
evaluate(XMeans())
evaluate(GMeans())
print "Run k-means with a specific value of k, and keep track of cluster assignments"
print "Row evaluate the cluster assignments and print a score.."
print simpleKMeans.toString()
for k in range(2,7):
clusteringResults = simpleKMeans.cluster(data, k, clusteringResults);
print "k=",k," score=",evaluator.evaluate(clusteringResults, data)
print "Running for 3 clusters (optimal):"
clusteringResults = simpleKMeans.cluster(data, 3, clusteringResults)
clusters=clusteringResults.tolist()
print "Cluster assignments=", clusters
c1 = SPlot()
c1.visible()
c1.setAutoRange()
c1.setMarksStyle('various')
c1.setNameX('X')
c1.setNameY('Y')
for i in range(data.getSampleSize()):
dataPoint = data.getDataPoint(i)
category = data.getDataPointCategory(i) # get category
vec=dataPoint.getNumericalValues();
c1.addPoint(clusters[i],vec.get(0),vec.get(1),1)
if (i%10==0):
c1.update()
print i,dataPoint,category," Nr cluster=",clusters[i]
You see the box below because you did not login.