<commit_before>/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 *
 * Written (W) 2006 <NAME>
 * Written (W) 2006-2009 <NAME>
 * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
 */

#include "classifier/KNN.h"
#include "features/Labels.h"
#include "lib/Mathematics.h"
#include "lib/Signal.h"

using namespace shogun;

CKNN::CKNN()
: CDistanceMachine(), k(3), num_classes(0), num_train_labels(0), train_labels(NULL)
{
}

CKNN::CKNN(int32_t k_, CDistance* d, CLabels* trainlab)
: CDistanceMachine(), k(k_), num_classes(0), train_labels(NULL)
{
    set_distance(d);
    set_labels(trainlab);
    num_train_labels=trainlab->get_num_labels();
}


CKNN::~CKNN()
{
	delete[] train_labels;
}

bool CKNN::train(CFeatures* data)
{
	ASSERT(labels);
	ASSERT(distance);

	if (data)
	{
		if (labels->get_num_labels() != data->get_num_vectors())
			SG_ERROR("Number of training vectors does not match number of labels\n");
		distance->init(data, data);
	}

	train_labels=labels->get_int_labels(num_train_labels);
	ASSERT(train_labels);
	ASSERT(num_train_labels>0);

	int32_t max_class=train_labels[0];
	int32_t min_class=train_labels[0];

	int32_t i;
	for (i=1; i<num_train_labels; i++)
	{
		max_class=CMath::max(max_class, train_labels[i]);
		min_class=CMath::min(min_class, train_labels[i]);
	}

	for (i=0; i<num_train_labels; i++)
		train_labels[i]-=min_class;

	min_label=min_class;
	num_classes=max_class-min_class+1;

	SG_INFO( "num_classes: %d (%+d to %+d) num_train: %d\n", num_classes, min_class, max_class, num_train_labels);
	return true;
}

CLabels* CKNN::classify()
{
	ASSERT(num_classes>0);
	ASSERT(distance);
	ASSERT(distance->get_num_vec_rhs());

	int32_t num_lab=distance->get_num_vec_rhs();
	ASSERT(k<=num_lab);

	CLabels* output=new CLabels(num_lab);
	SG_REF(output);

	//distances to train data and working buffer of train_labels
	float64_t* dists=new float64_t[num_train_labels];
	int32_t* train_lab=new int32_t[num_train_labels];

	///histogram of classes and returned output
	int32_t* classes=new int32_t[num_classes];

	ASSERT(dists);
	ASSERT(train_lab);
	ASSERT(classes);

	SG_INFO( "%d test examples\n", num_lab);
	CSignal::clear_cancel();

	for (int32_t i=0; i<num_lab && (!CSignal::cancel_computations()); i++)
	{
		if ((i%(num_lab/10+1))== 0)
			SG_PROGRESS(i, 0, num_lab);

		int32_t j;
		for (j=0; j<num_train_labels; j++)
		{
			//copy back train labels and compute distance
			train_lab[j]=train_labels[j];
			
			dists[j]=distance->distance(j,i);
		}
<commit_msg>changed computation of distance values<commit_after>/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 *
 * Written (W) 2006 <NAME>
 * Written (W) 2006-2009 <NAME>
 * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
 */

#include "classifier/KNN.h"
#include "features/Labels.h"
#include "lib/Mathematics.h"
#include "lib/Signal.h"

using namespace shogun;

CKNN::CKNN()
: CDistanceMachine(), k(3), num_classes(0), num_train_labels(0), train_labels(NULL)
{
}

CKNN::CKNN(int32_t k_, CDistance* d, CLabels* trainlab)
: CDistanceMachine(), k(k_), num_classes(0), train_labels(NULL)
{
    set_distance(d);
    set_labels(trainlab);
    num_train_labels=trainlab->get_num_labels();
}


CKNN::~CKNN()
{
	delete[] train_labels;
}

bool CKNN::train(CFeatures* data)
{
	ASSERT(labels);
	ASSERT(distance);

	if (data)
	{
		if (labels->get_num_labels() != data->get_num_vectors())
			SG_ERROR("Number of training vectors does not match number of labels\n");
		distance->init(data, data);
	}

	train_labels=labels->get_int_labels(num_train_labels);
	ASSERT(train_labels);
	ASSERT(num_train_labels>0);

	int32_t max_class=train_labels[0];
	int32_t min_class=train_labels[0];

	int32_t i;
	for (i=1; i<num_train_labels; i++)
	{
		max_class=CMath::max(max_class, train_labels[i]);
		min_class=CMath::min(min_class, train_labels[i]);
	}

	for (i=0; i<num_train_labels; i++)
		train_labels[i]-=min_class;

	min_label=min_class;
	num_classes=max_class-min_class+1;

	SG_INFO( "num_classes: %d (%+d to %+d) num_train: %d\n", num_classes, min_class, max_class, num_train_labels);
	return true;
}

CLabels* CKNN::classify()
{
	ASSERT(num_classes>0);
	ASSERT(distance);
	ASSERT(distance->get_num_vec_rhs());

	int32_t num_lab=distance->get_num_vec_rhs();
	ASSERT(k<=num_lab);

	CLabels* output=new CLabels(num_lab);
	SG_REF(output);

	//distances to train data and working buffer of train_labels
	float64_t* dists=new float64_t[num_train_labels];
	int32_t* train_lab=new int32_t[num_train_labels];

	///histogram of classes and returned output
	int32_t* classes=new int32_t[num_classes];

	ASSERT(dists);
	ASSERT(train_lab);
	ASSERT(classes);

	SG_INFO( "%d test examples\n", num_lab);
	CSignal::clear_cancel();

	for (int32_t i=0; i<num_lab && (!CSignal::cancel_computations()); i++)
	{
		if ((i%(num_lab/10+1))== 0)
			SG_PROGRESS(i, 0, num_lab);
                distances(dists,0,num_train_labels-1,i);
		int32_t j;
		for (j=0; j<num_train_labels; j++)
		{
			//copy back train labels and compute distance
			train_lab[j]=train_labels[j];
			
//			dists[j]=distance->distance(j,i);
		}

{'fraction_non_alphanumeric': 0.14555011781765453, 'fraction_numerical': 0.02392604676454595, 'mean_word_length': 5.491764705882353, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 27, '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': '3930474', 'n_tokens_mistral': 2271, 'n_tokens_neox': 2116, 'n_words': 451}