<commit_before>package main;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import tutorials.TutorialData;
import net.sf.javaml.classification.Classifier;
import net.sf.javaml.classification.bayes.NaiveBayesClassifier;
import net.sf.javaml.classification.evaluation.EvaluateDataset;
import net.sf.javaml.classification.evaluation.PerformanceMeasure;
import net.sf.javaml.clustering.Clusterer;
import net.sf.javaml.clustering.KMeans;
import net.sf.javaml.clustering.evaluation.AICScore;
import net.sf.javaml.clustering.evaluation.BICScore;
import net.sf.javaml.clustering.evaluation.ClusterEvaluation;
import net.sf.javaml.clustering.evaluation.SumOfSquaredErrors;
import net.sf.javaml.core.Dataset;
import net.sf.javaml.core.Instance;
import net.sf.javaml.filter.discretize.EqualWidthBinning;
import net.sf.javaml.tools.data.FileHandler;

public class TrafficJamDetector {


	
	private static void kmeans(String file, int classPosition) throws IOException{
		Dataset data = FileHandler.loadDataset(new File(file), classPosition, "	");
		
		Clusterer km = new KMeans(3);
		Dataset[] clusters = km.cluster(data);
    	System.out.println("Cluster count: " + clusters.length);
    	
    	ClusterEvaluation aic = new AICScore();
        ClusterEvaluation bic = new BICScore();
        ClusterEvaluation sse = new SumOfSquaredErrors();

        double aicScore = aic.score(clusters);
        double bicScore = bic.score(clusters);
        double sseScore = sse.score(clusters);
        
        System.out.println("AIC score: " + aicScore);
        System.out.println("BIC score: " + bicScore);
        System.out.println("Sum of squared errors: " + sseScore);

	}
	
	
	private static void naiveBayes(String file, int classPosition) throws IOException{
		/* Load a data set */
		Dataset data = FileHandler.loadDataset(new File(file), classPosition, "	");

		/* Discretize through EqualWidtBinning */
		//EqualWidthBinning eb = new EqualWidthBinning(100);
		//System.out.println("Start discretisation");
		//eb.build(data);
		//Dataset ddata = data.copy();
		//eb.filter(ddata);

		boolean useLaplace = true;
		boolean useLogs = true;
		Classifier nbc = new NaiveBayesClassifier(useLaplace, useLogs, false);
		nbc.buildClassifier(data);

		Dataset dataForClassification = FileHandler.loadDataset(new File(file), 18, "	");

		/* Counters for correct and wrong predictions. */
		int correct = 0, wrong = 0;

		/* Classify all instances and check with the correct class values */
		for (Instance inst : dataForClassification) {
			//eb.filter(inst);
			Object predictedClassValue = nbc.classify(inst);
			Object realClassValue = inst.classValue();
			if (predictedClassValue.equals(realClassValue))
				correct++;
			else {
				wrong++;
			}

		}
		System.out.println("correct " + correct);
		System.out.println("incorrect " + wrong);
		
		Map<Object, PerformanceMeasure> pm = EvaluateDataset.testDataset(nbc, dataForClassification);
        for (Object o : pm.keySet()){
        	System.out.println("------"+o+"------");
        	System.out.println("Accuracy : " + pm.get(o).getAccuracy());
        	System.out.println("Reccal : " + pm.get(o).getRecall());
        	System.out.println("Precision : " + pm.get(o).getPrecision());
        }
            
	}
	
	
	public static void main(String[] args) throws IOException {
		naiveBayes("pems_splitData_NB.csv", 18);
		kmeans("pems_splitData_NB.csv", 18);
	}

}
<commit_msg>Kmeans parametrizado e KNN adicionado<commit_after>package main;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import com.sun.org.apache.bcel.internal.util.Class2HTML;

import tutorials.TutorialData;
import net.sf.javaml.classification.Classifier;
import net.sf.javaml.classification.KNearestNeighbors;
import net.sf.javaml.classification.bayes.NaiveBayesClassifier;
import net.sf.javaml.classification.evaluation.EvaluateDataset;
import net.sf.javaml.classification.evaluation.PerformanceMeasure;
import net.sf.javaml.clustering.Clusterer;
import net.sf.javaml.clustering.KMeans;
import net.sf.javaml.clustering.evaluation.AICScore;
import net.sf.javaml.clustering.evaluation.BICScore;
import net.sf.javaml.clustering.evaluation.ClusterEvaluation;
import net.sf.javaml.clustering.evaluation.SumOfSquaredErrors;
import net.sf.javaml.core.Dataset;
import net.sf.javaml.core.Instance;
import net.sf.javaml.filter.discretize.EqualWidthBinning;
import net.sf.javaml.tools.data.FileHandler;

public class TrafficJamDetector {

	private static void kmeans(String file, int classPosition, int k)
			throws IOException {
		Dataset data = FileHandler.loadDataset(new File(file), classPosition,
				"	");

		System.out.println("------KMeans------");
		System.out.println("------Treinando------");
		Clusterer km = new KMeans(k);
		Dataset[] clusters = km.cluster(data);
		System.out.println("OK");
		System.out.println("Cluster count: " + clusters.length);

		ClusterEvaluation aic = new AICScore();
		ClusterEvaluation bic = new BICScore();
		ClusterEvaluation sse = new SumOfSquaredErrors();

		double aicScore = aic.score(clusters);
		double bicScore = bic.score(clusters);
		double sseScore = sse.score(clusters);

		System.out.println("AIC score: " + aicScore);
		System.out.println("BIC score: " + bicScore);
		System.out.println("Sum of squared errors: " + sseScore);
		
	}

	private static void naiveBayes(String file, int classPosition, boolean metricas)
			throws IOException {
		System.out.println("------Naive Bayes------");
		Dataset data = FileHandler.loadDataset(new File(file), classPosition,
				"	");

		/* Discretize through EqualWidtBinning */
//		EqualWidthBinning eb = new EqualWidthBinning(100);
//		System.out.println("Start discretisation");
//		eb.build(data);
//		Dataset ddata = data.copy();
//		eb.filter(ddata);

		System.out.println("------Treinando------");
		boolean useLaplace = true;
		boolean useLogs = true;
		Classifier nbc = new NaiveBayesClassifier(useLaplace, useLogs, false);
		nbc.buildClassifier(data);
		System.out.println("OK");
		
		classifica(file, classPosition, nbc, metricas);
	}
	
	

	public static void knn(String file, int classPosition, int k, boolean metricas)
			throws IOException {
		System.out.println("------" + k + "NN------");

		Dataset data = FileHandler.loadDataset(new File(file), classPosition,
				"	");
		System.out.println("------Treinando------");
		Classifier knn = new KNearestNeighbors(k);
		knn.buildClassifier(data);
		System.out.println("OK");

		classifica(file, classPosition, knn, metricas);
	}
	
	

	public static void classifica(String file, int classPosition,
			Classifier classificador, boolean metricas) throws IOException {
		System.out.println("------Classificando------");
		Dataset dataForClassification = FileHandler.loadDataset(new File(file),
				classPosition, "	");
		int correct = 0, wrong = 0;
		
		for (Instance inst : dataForClassification) {
			Object predictedClassValue = classificador.classify(inst);
			Object realClassValue = inst.classValue();
			if (predictedClassValue.equals(realClassValue))
				correct++;
			else
				wrong++;
		}
		System.out.println("Correct predictions  " + correct);
		System.out.println("Wrong predictions " + wrong);

		if (metricas) {
			Map<Object, PerformanceMeasure> pm = EvaluateDataset.testDataset(
					classificador, dataForClassification);
			for (Object o : pm.keySet()) {
				System.out.println("------" + o + "------");
				System.out.println("Accuracy : " + pm.get(o).getAccuracy());
				System.out.println("Reccal : " + pm.get(o).getRecall());
				System.out.println("Precision : " + pm.get(o).getPrecision());
			}
		}
	}

	public static void main(String[] args) throws IOException {
		System.out.println("_____________________________");
		naiveBayes("pems_splitData_NB_4.csv", 10, true);
		System.out.println("_____________________________");
		knn("pems_splitData_NB_4.csv", 10, 10, true);
		System.out.println("_____________________________");
		kmeans("pems_splitData_NB_4.csv", 10, 3);
	}
}

{'fraction_non_alphanumeric': 0.13468545057080397, 'fraction_numerical': 0.0036434296818071412, 'mean_word_length': 3.5903010033444818, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5607589', 'n_tokens_mistral': 2869, 'n_tokens_neox': 2580, 'n_words': 493}