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"



