コード例 #1
0
ファイル: kmlsample.cpp プロジェクト: excelly/xpy-ml
//------------------------------------------------------------------------
//  Print summary of execution
//------------------------------------------------------------------------
void printSummary(
    const KMlocal&		theAlg,		// the algorithm
    const KMdata&		dataPts,	// the points
    KMfilterCenters&		ctrs)		// the centers
{
    cout << "Number of stages: " << theAlg.getTotalStages() << "\n";
    cout << "Average distortion: " <<
	         ctrs.getDist(false)/double(ctrs.getNPts()) << "\n";
    					// print final center points
    cout << "(Final Center Points:\n";
    ctrs.print();
    cout << ")\n";
    					// get/print final cluster assignments
    KMctrIdxArray closeCtr = new KMctrIdx[dataPts.getNPts()];
    double* sqDist = new double[dataPts.getNPts()];
    ctrs.getAssignments(closeCtr, sqDist);

    *kmOut	<< "(Cluster assignments:\n"
		<< "    Point  Center  Squared Dist\n"
		<< "    -----  ------  ------------\n";
    for (int i = 0; i < dataPts.getNPts(); i++) {
	*kmOut	<< "   " << setw(5) << i
		<< "   " << setw(5) << closeCtr[i]
		<< "   " << setw(10) << sqDist[i]
		<< "\n";
    }
    *kmOut << ")\n";
    delete [] closeCtr;
    delete [] sqDist;
}
コード例 #2
0
ファイル: kmeans.cpp プロジェクト: samy-/svmNAO
//------------------------------------------------------------------------
//  Print summary of execution
//------------------------------------------------------------------------
void printSummary(
		  const KMlocal&		theAlg,		// the algorithm
		  const KMdata&		dataPts,	// the points
		  KMfilterCenters&		ctrs)		// the centers
{
  ofstream out;			// output data file stream
  out.open("output3.txt",ios::out);
  if(!out){  // si l'ouverture a échoué
    cerr << "Impossible d'ouvrir le fichier erreur2" << endl;
    exit(0);
  }
  
  out << "Number of stages: " << theAlg.getTotalStages() << "\n";
  out << "Average distortion: " <<
    ctrs.getDist(false)/double(ctrs.getNPts()) << "\n";
  // print final center points
  out << "(Final Center Points:\n";
  ctrs.print();
  out << ")\n";
  // get/print final cluster assignments
  KMctrIdxArray closeCtr = new KMctrIdx[dataPts.getNPts()];
  double* sqDist = new double[dataPts.getNPts()];
  ctrs.getAssignments(closeCtr, sqDist); // obligé d'avoir la distance ?
  
  out	<< "(Cluster assignments:\n"
	<< "Point  Center :\n"
	<< "-----  ------  \n";
  for (int i = 0; i < dataPts.getNPts(); i++) {
    out	<< setw(5) << i
	<< setw(5) << closeCtr[i]
      
	<< "\n";
  }
  out << ")\n";
  delete [] closeCtr;
  delete [] sqDist;
  out.close();  // on ferme le fichier
}
コード例 #3
0
ファイル: kmain.cpp プロジェクト: excelly/xpy-ml
//------------------------------------------------------------------------
//  Print summary of execution
//------------------------------------------------------------------------
void printSummary(const KMlocal&		theAlg,		// the algorithm
				  const KMdata&		dataPts,	// the points
				  KMfilterCenters&		ctrs)		// the centers
{
	double dbval, xbval;
	dbval = ctrs.getDBIndex();
	xbval = ctrs.getXBIndex();

	FILE* logfp = fopen("kmean.log", "wt");
	fprintf(logfp, "Number of stages: %d\n", theAlg.getTotalStages());
	fprintf(logfp, "Average distortion: %g\n", ctrs.getDist(false)/double(ctrs.getNPts()));
	fprintf(logfp, "DB-index: %g\n", dbval);
	fprintf(logfp, "XB-index: %g\n", xbval);	

	cout << "Number of stages: " << theAlg.getTotalStages() << "\n";
	cout << "Average distortion: " <<
		ctrs.getDist(false)/double(ctrs.getNPts()) << "\n";
	cout << "DB-index = " << dbval << "\n";
	cout << "XB-index = " << xbval << "\n";

	KMctrIdxArray closeCtr = new KMctrIdx[dataPts.getNPts()];
	double* sqDist = new double[dataPts.getNPts()];
	ctrs.getAssignments(closeCtr, sqDist);

	int*  hist = new int[ctrs.getK()];
	memset(hist, 0, sizeof(int) * ctrs.getK() );
	for(int i=0; i < dataPts.getNPts(); i++ )
	{
		int k = closeCtr[i];
		hist[k] ++;
	}

	KMpoint kpt;
	for(int i=0; i< ctrs.getK(); i++ )
	{
		fprintf(logfp, "%3d-th, #%5d ", i, hist[i]);

		// print final center points
		kpt = ctrs[i];
		fprintf(logfp, " [");
		for (int j = 0; j < ctrs.getDim(); j++) 
		{
			fprintf(logfp, "%7g ", kpt[j]);
		}
		fprintf(logfp, " ]\n");
	}
	fclose(logfp);

	// write to fea-file
	CFeaFileWriter theFeaWriter;
	char saveName[256];
	sprintf(saveName, "kmain_%s", infname.c_str() );
	theFeaWriter.openFile(saveName);

	FEA_FILE_HEADER feaHeader;
	feaHeader.nVersion = FEA_FILE_VERSION;
	feaHeader.nRecords = ctrs.getK();
	feaHeader.nFeaDim = ctrs.getDim();
	feaHeader.nElemType = ELEM_TYPE_FLOAT;
	feaHeader.nElemSize = sizeof(float);
	feaHeader.bIndexTab = 0;
	feaHeader.bVarLen = 0;
	sprintf(feaHeader.szFeaName, "kmean codebook");
	theFeaWriter.setFileHeader(feaHeader);

	float* pFea = new float[ctrs.getDim()];
	for(int i=0; i< ctrs.getK(); i++ )
	{
		// save final center points
		kpt = ctrs[i];
		for (int j = 0; j < ctrs.getDim(); j++) 
			pFea[j] = kpt[j];

		theFeaWriter.writeRecordAt(pFea, i);
	}
	theFeaWriter.flush2Disk();
	theFeaWriter.closeFile();
	theFeaWriter.releaseMemory();
	delete [] pFea;

	delete [] closeCtr;
	delete [] sqDist;
	delete [] hist;
}