<commit_before>"""
	Classification - KNN
	by <NAME>
	<EMAIL>
"""

import numpy as np
from model.classifier import Classifier

class ClassifierKNN(Classifier):
	"""
	Structure for a knn classifier
	"""

	def __init__(self, distance, nearestNeighbor):
		"""
		Saves the distance to use and the number of nearest neighbors
		"""
		self.distance = distance
		self.nearestNeighbor = nearestNeighbor

	def predict(self, data, **kwargs):
		"""
		Forecasts the output given the data
		"""
		# Computes the ditance to each data point
		distances = [self.distance(data, trainD) for trainD in self.trainData]
		labelDistance = list(zip(self.trainLabels, distances))

		# Sorts it by distance
		labelDistance.sort(key=lambda x: x[1])
		neightbor = [0] * (max(self.trainLabels) - min(self.trainLabels) + 1)

		for j in labelDistance:
			if max(neightbor) < self.nearestNeighbor :
				neightbor[j[0]] += 1
			else :
				break

		return np.argmax(neightbor) + min(self.trainLabels)

	def train(self,trainData, trainLabels, testData = None, testLabels=None):
		"""
		Trains the model and measure on the test data
		"""
		self.trainData = trainData
		self.trainLabels = trainLabels

		self.test(testData, testLabels)<commit_msg>Update knn in order to refuse if not train<commit_after>"""
	Classification - KNN
	by <NAME>
	<EMAIL>
"""

import numpy as np
from model.classifier import Classifier

class ClassifierKNN(Classifier):
	"""
	Structure for a knn classifier
	"""

	def __init__(self, distance, nearestNeighbor):
		"""
		Saves the distance to use and the number of nearest neighbors
		"""
		self.distance = distance
		self.nearestNeighbor = nearestNeighbor

	def predict(self, data, **kwargs):
		"""
		Forecasts the output given the data
		"""
		if self.trainData is None:
			print("Train before predict")
		else:
			# Computes the ditance to each data point
			distances = [self.distance(data, trainD) for trainD in self.trainData]
			labelDistance = list(zip(self.trainLabels, distances))

			# Sorts it by distance
			labelDistance.sort(key=lambda x: x[1])
			neightbor = [0] * (max(self.trainLabels) - min(self.trainLabels) + 1)

			for j in labelDistance:
				if max(neightbor) < self.nearestNeighbor :
					neightbor[j[0]] += 1
				else :
					break

			return np.argmax(neightbor) + min(self.trainLabels)

	def train(self,trainData, trainLabels, testData = None, testLabels=None):
		"""
		Trains the model and measure on the test data
		"""
		self.trainData = trainData
		self.trainLabels = trainLabels

		self.test(testData, testLabels)
{'fraction_non_alphanumeric': 0.10777733912356889, 'fraction_numerical': 0.0039478878799842085, 'mean_word_length': 4.017821782178218, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '48518', 'n_tokens_mistral': 915, 'n_tokens_neox': 810, 'n_words': 243}