Bayesian classification of IRIS data using JSAT package
Code: "jsat_bayes.py". Programming language: Python
DMelt Version 2.2. Last modified: 03/03/2021. License: Pro
https://datamelt.org/code/cache/jsat_bayes_6872.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.classifiers import DataPoint,Classifier,CategoricalResults,ClassificationDataSet
from jsat.classifiers.bayesian import NaiveBayes
from jsat import ARFFLoader,DataSet
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.
cDataSet = ClassificationDataSet(dataSet, 0)
errors = 0
classifier = NaiveBayes()
classifier.train(cDataSet)
for i in range(dataSet.getSampleSize()):
# It is important not to mix these up, the class has been removed from data points in 'cDataSet'
dataPoint = cDataSet.getDataPoint(i)
truth = cDataSet.getDataPointCategory(i) # We can grab the true category from the data set
# Categorical Results contains the probability estimates for each possible target class value.
# Classifiers that do not support probability estimates will mark its prediction with total confidence.
predictionResults = classifier.classify(dataPoint)
predicted = predictionResults.mostLikely()
if(predicted != truth): errors +=1
print i,"| True Class: ", truth, ", Predicted: ", predicted, ", Confidence: ", predictionResults.getProb(predicted)
print errors, " errors were made, ", 100.0*errors/dataSet.getSampleSize(), "% error rate"
You see the box below because you did not login.