Exemplo n.º 1
0
void saveMatrix(int *matrix, int rank, char **nomes, char *arquivo)
{
	FILE *f=fopen(arquivo, "wt+");
	char buffer[255];
	if (f!=NULL)
	{
		fprintf(f, "-------------------------------------------------------------------------------------------------------------------------------\n");
        fprintf(f, "|%20s|", "Nomes");
		for (int i=0;i<rank;i++) fprintf(f, "%20s|", nomes[i]);
        fprintf(f, "%20s|\n", "Acuracia");
		fprintf(f, "-------------------------------------------------------------------------------------------------------------------------------\n");

		for (int i=0;i<rank;i++) 
		{
			fprintf(f, "|%20s|", nomes[i]);
			int acc=0, total=0;
			for (int j=0;j<rank;j++)
			{
				fprintf(f, "%20d|", matrix[i*rank+j]);
				if (i==j) acc = matrix[i*rank+j];
				total += matrix[i*rank+j];

			}
			if (total == 0) sprintf(buffer, "%.2f", 0);
			else sprintf(buffer, "%.2f", (float)acc*100/(float)total);
			strcat(buffer, "%");
			fprintf(f, "%20s|\n", buffer);
		}

		fprintf(f, "-------------------------------------------------------------------------------------------------------------------------------\n");
		fprintf(f, "|%20s|", "Especificidade");

		for (int j=0;j<rank;j++)
		{
			int acc=0, total=0;
			for (int i=0;i<rank;i++)
			{
				if (i==j) acc = matrix[i*rank+j];
				total += matrix[i*rank+j];
			}
			if (total == 0) sprintf(buffer, "%.2f", 0);
			else sprintf(buffer, "%.2f", (float)acc*100/(float)total);
			strcat(buffer, "%");
			fprintf(f, "%20s|", buffer);
		}
        fprintf(f, "%20s|\n", "");
		fprintf(f, "-------------------------------------------------------------------------------------------------------------------------------\n");
		fprintf(f, "\n");

		sprintf(buffer, "%.2f", acertos(matrix, rank));
		strcat(buffer, "%");

		fprintf(f, "Valor Kappa    : %.2f\n", Kappa(matrix, rank));
		fprintf(f, "Acurácia Geral : %s\n", buffer);
		fprintf(f, "Total Exemplos : %d\n", exemplos(matrix, rank));
		fclose(f);
	}
}
bool ClassificadorAdaboostM1::executarClassificacao( Corpus &corpusProva, int atributo ) {
    vector<vector<double> > exemplos(corpusProva.pegarQtdTotalExemplos(), vector<double>(valores.size(), 0.0));
    int k, indice;
    for (unsigned int t = 0; t < classificadores.size(); t++) {
        (classificadores[t])->executarClassificacao(corpusProva, atributo);
        k = 0;
        for (int i = 0; i < corpusProva.pegarQtdSentencas(); i++) {
            for (int j = 0; j < corpusProva.pegarQtdExemplos(i); j++) {
                indice = -1;
                for (unsigned int a = 0; a < valores.size() - 1; a++)
                    if (valores[a] == corpusProva(i, j, atributo)) {
                        indice = a;
                        a = valores.size();
                    }
                if (indice == -1)
                    indice = valores.size() - 1;
                exemplos[k][indice] -= log(betas[t]);
                k++;
            }
        }
    }
    double maxBeta; int maxAtr;
    k = 0;
    for (int i = 0; i < corpusProva.pegarQtdSentencas(); i++) {
        for (int j = 0; j < corpusProva.pegarQtdExemplos(i); j++) {
            maxBeta = exemplos[k][valores.size()-1];
            maxAtr = valores.size() - 1;
            for (unsigned int a = 0; a < valores.size() - 1; a++) {
                if (maxBeta <= exemplos[k][a]) {
                    maxBeta = exemplos[k][a];
                    maxAtr = a;
                }
            }
            corpusProva(i, j, atributo, valores[maxAtr]);
            k++;
        }
    }
    return true;
}