void ExampleKMeans(int argIndex, int argc, char **argv) {	
	char filename[FILENAME_MAX];
	CLUSTER_ALOG *kmeans;
	CLUSTER_ITEM *dataset;
	int iteration;

	LocateFile("iris.csv",filename,FILENAME_MAX);
	kmeans = CreateKMeans(3,4);
	dataset = KMeansLoadCSV(filename,4,0,4);

	/* Note, it is not necessary to delete the dataset list, it will be deleted
	   when the clusters are deleted.  However, if you did need to delete it, pre-clustering,
	   the following command would do that.

		KMeansDeleteList(dataset); */

	/* To display the dataset, pre-clustering, use: 
	
	   KMeansDumpList(stdout,dataset,4); */

	
	KMeansInitForgy(kmeans,dataset);
	
	iteration = 1;
	while(!KMeansIteration(kmeans)) {
		printf("Iteration #%i\n",iteration);
		iteration++;
	}
	KMeansDump(stdout,kmeans);

	DeleteKMeans(kmeans);
}
Exemple #2
0
/*
This example uses a Generalized Linear Model (GLM), in this case a LOGIT to fit to the Wisconsin Breast Cancer
data set.  The output is shown below.  The solution coefficients are shown.

Actual input count: 9
Actual output count: 1
Iteration #1, Error: inf
Iteration #2, Error: 0.63698368
Iteration #3, Error: 0.37629115
Iteration #4, Error: 0.24127030
Iteration #5, Error: 0.12430888
Iteration #6, Error: 0.03587100
Iteration #7, Error: 0.00312089
Ideal: 0.00, Actual: 0.02
Ideal: 0.00, Actual: 0.91
Ideal: 0.00, Actual: 0.01
Ideal: 0.00, Actual: 0.77
Ideal: 0.00, Actual: 0.02
Ideal: 1.00, Actual: 1.00
Ideal: 0.00, Actual: 0.09
Ideal: 0.00, Actual: 0.01
Ideal: 0.00, Actual: 0.01
...
Ideal: 0.00, Actual: 0.00
Ideal: 1.00, Actual: 0.94
Ideal: 0.00, Actual: 0.00
Ideal: 0.00, Actual: 0.01
Ideal: 0.00, Actual: 0.00
Ideal: 0.00, Actual: 0.00
Ideal: 1.00, Actual: 0.99
Ideal: 1.00, Actual: 0.95
Ideal: 1.00, Actual: 0.98
*/
void ExampleGLM(int argIndex, int argc, char **argv) {	
	char filename[FILENAME_MAX];
	NORM_DATA *norm;
	DATA_SET *data;
	REGRESSION_MODEL *reg;
	double *ideal, actual, *input, error;
	unsigned int i, iteration;

	LocateFile("breast-cancer-wisconsin.csv",filename,FILENAME_MAX);
	
	/* Normalize the data.  If you use a different data set, this area will need to be
	   updated. */
	norm = NormCreate();
	NormDefIgnore(norm);
	NormDefPass(norm);
	NormDefPass(norm);
	NormDefPass(norm);
	NormDefPass(norm);
	NormDefPass(norm);
	NormDefPass(norm);
	NormDefPass(norm);
	NormDefPass(norm);
	NormDefPass(norm);
	NormDefReplace(norm,4,1,0);

	NormAnalyze(norm,filename);
	data = NormProcess(norm,filename,9,1);
	
	printf("Actual input count: %i\n", data->inputCount);
	printf("Actual output count: %i\n", data->idealCount);

	reg = RegressionCreate(data->inputCount,LinkLOGIT);
	
	iteration = 0;
    do {
		iteration++;
		error = RegressionReweightLeastSquares(reg,data);
		printf("Iteration #%i, Error: %.8f\n",iteration,error);
	} while (iteration < 1000 && error > 0.01);

	/* Display results */
	for(i=0;i<data->recordCount;i++) {
		ideal = DataGetIdeal(data,i);
		input = DataGetInput(data,i);
		actual = RegressionCalculate(reg,input);
		printf("Ideal: %.2f, Actual: %.2f\n",ideal[0],actual);
	}

	NormDelete(norm);
	DataDelete(data);
	RegressionDelete(reg);
}
Exemple #3
0
/*** Simple scan for file type ***/
char WhatIs( char *GuideName, AGLink link, char **path )
{
	int fd, len;

	/* This routine is far to be exhaustive, but covers 90% of possible cases */
	if(link->file == NULL) return LOCAL_NODE;

	if( (*path = LocateFile( GuideName, link->file )) != NULL )
	{
		extern char AGHeader[];
		char *p;

		/* Read a few bytes from this file */
		if( (fd = open(*path,O_RDONLY)) == -1 )
			return NOT_LOCATED;

		buffer[ len = read(fd,buffer,128) ] = '\0';
		close( fd );

		if( len == 0 ) return UNKNOWN_TYPE;
		if( !strncasecmp(buffer,AGHeader,strlen(AGHeader)) )
			/* Target is an AmigaGuide file */
			return EXTERN_NODE;

		if( !strncasecmp(buffer,"FORM",4) && !strncasecmp(buffer+8,"ILBM",4) )
			/* IFF ILBM picture have a such chunk Id */
			return PICTURE;

		p = link->file + strlen( link->file );
		/* That's coarse, but sometimes works */
		if( !strncasecmp(p-3,"gif", 3) ||
		    !strncasecmp(p-3,"jpg", 3) ||
		    !strncasecmp(p-4,"jpeg",4) )
			return PICTURE;

		/* Check for a few bytes, whether it's ASCII text */
		for(p=buffer+len-1; (char *)p>=buffer; p--)
		{
			unsigned char u = *p;
			/* Non-ISO-8859-1 characters */
			if( u>126 && u<160 ) break;
			if( u<32  && u!='\r' && u!='\n' && u!='\t' && u!=27) break;
		}
		if((char *)p<buffer) return TEXT_FILE;

		return UNKNOWN_TYPE;
	}
	return NOT_LOCATED;
}
/* Create the training set, and normalize the Iris data.  If you are using a data set
   other than Iris, you will need to update the normalization. */
static DATA_SET *create_iris_training(NORM_DATA *norm) {
	char filename[FILENAME_MAX];
	
	DATA_SET *data;

	LocateFile("iris.csv",filename,FILENAME_MAX);
	
	NormDefRange(norm,0,1);
	NormDefRange(norm,0,1);
	NormDefRange(norm,0,1);
	NormDefRange(norm,0,1);
	NormDefClass(norm,NORM_CLASS_ONEOFN,0,1);

	NormAnalyze(norm,filename);
	data = NormProcess(norm,filename,4,1);	
	return data;
}
Exemple #5
0
/*
This example shows how to simply read a CSV file.  This example reads the iris data set.  
The output is shown here.  

Reading CSV file: ./datasets/iris.csv
Field: "sepal_length"
Field: "sepal_width"
Field: "petal_length"
Field: "petal_width"
Field: "class"
Row done
Field: "5.1"
Field: "3.5"
Field: "1.4"
Field: "0.2"
Field: "Iris-setosa"
Row done
Field: "4.9"
Field: "3.0"
Field: "1.4"
Field: "0.2"
Field: "Iris-setosa"
Row done
Field: "4.7"
Field: "3.2"
Field: "1.3"
Field: "0.2"
Field: "Iris-setosa"
...
Row done
Field: "6.5"
Field: "3.0"
Field: "5.2"
Field: "2.0"
Field: "Iris-virginica"
Row done
Field: "6.2"
Field: "3.4"
Field: "5.4"
Field: "2.3"
Field: "Iris-virginica"
Row done
Field: "5.9"
Field: "3.0"
Field: "5.1"
Field: "1.8"
Field: "Iris-virginica"
Row done
755 fields, 151 rows
*/
void ExampleReadCSV(int argIndex, int argc, char **argv) {
	char filename[FILENAME_MAX];
	FILE *fp;
	struct csv_parser p;
	char buf[1024];
	size_t bytes_read;
	struct counts c = {0, 0};
	
	if( argIndex>=argc ) {
		LocateFile("iris.csv",filename,FILENAME_MAX);
	} else {
		strncpy(filename,argv[argIndex],FILENAME_MAX);
	}
	
	printf("Reading CSV file: %s\n", filename);

	/* Setup csvlib to read the CSV file */
	if (csv_init(&p, CSV_APPEND_NULL) != 0) exit(EXIT_FAILURE);
	fp = fopen(filename, "rb");
	if (!fp)
	{ 
		printf("Could not open: %s\n", filename);
		exit(EXIT_FAILURE); 
	}

	/* Loop over the contents.  It is important to note that we are not reading line by
	   line, at this level.  Rather, we are passing blocks off to csvlib.  Then csvlib
	   calls our two callbacks as needed. */

	while ((bytes_read=fread(buf, 1, 1024, fp)) > 0)
		if (csv_parse(&p, buf, bytes_read, CallbackColumn, CallbackRow, &c) != bytes_read) {
			fprintf(stderr, "Error while parsing file: %s\n",
			csv_strerror(csv_error(&p)) );
			exit(EXIT_FAILURE);
		}

	/* Handle any final data.  May call the callbacks once more */
	csv_fini(&p, CallbackColumn, CallbackRow, &c);

	/* Print final stats on CSV file */
	printf("%lu fields, %lu rows\n", c.fields, c.rows);

	/* Cleanup */
	fclose(fp);
	csv_free(&p);
}
Exemple #6
0
int PushFileName(const char *Name, struct Stack *stack)
{
    FILE *fh = NULL;
    static char NameBuf[BUFSIZ];

    if (Name && stack)
    {
        if (LocateFile(Name, NameBuf, ".tex", &TeXInputs))
        {
            if ((fh = fopen(NameBuf, "r")))
            {
                return (PushFile(NameBuf, fh, stack));
            }
        }
        PrintPrgErr(pmNoTeXOpen, Name);
    }
    return (FALSE);
}
Exemple #7
0
/**
 * Substitute for standard fopen, treats certain filenames specially,
 * and also considers the mode argument. If a file is being opened
 * for reading, the file is assumed to be locateable using CodeWarrior's
 * standard access paths. If it's for writing, the file is opened in
 * the current project's output directory.
 */
FILE* std::fopen(const char* filename, const char *mode)
{
	FSSpec filespec;
	CWResult err = noErr;
	do {
		if (filename == gSourcePath || strcmp(filename, gSourcePath) == 0) {
			// opening the main source file.
			filespec = gSourceFile;
		} else if (mode[0] == 'w') {
			// if an output file, open it in the current compilation's output directory.
			c2p_strcpy(filespec.name, filename);
			filespec.vRefNum = gOutputFile.vRefNum;
			filespec.parID = gOutputFile.parID;
			c2p_strcpy(gOutputFile.name, filename);
		} else {
			// an input file, use CodeWarrior's search paths to find the named source file.
			err = LocateFile(gPluginContext, filename, filespec);
		}
	} while (0);
	// if all went well, we have a file to open.
	return (err == noErr ? FSp_fopen(&filespec, mode) : NULL);
}
void ExampleAnalyze(int argIndex, int argc, char **argv) {	
	char filename[FILENAME_MAX];
	NORM_DATA *norm;
	NORM_DATA_ITEM *col;
	NORM_DATA_CLASS *currentClass;

	LocateFile("iris.csv",filename,FILENAME_MAX);
	norm = NormCreate();
	NormDefRange(norm,0,1);
	NormDefRange(norm,0,1);
	NormDefRange(norm,0,1);
	NormDefRange(norm,0,1);
	NormDefClass(norm,NORM_CLASS_ONEOFN,0,1);
	NormAnalyze(norm,filename);
	
	col = norm->firstItem;
	while(col!=NULL) {

		if( col->type == NORM_TYPE_RANGE ) {
			printf("Column: \"%s\",actualMin=%.2f,actualHigh=%.2f\n",col->name,col->actualHigh,col->actualLow);
		} else {
			printf("Column: \"%s\",classes= ",col->name);
			currentClass = col->firstClass;
			while(currentClass!=NULL) {
				printf("\"%s\";",currentClass->name);
				currentClass = currentClass->next;
			}
			printf("\n");

		}
		col = col->next;
	}

	printf("Rows: %i\n",norm->rowCount);

	NormDelete(norm);
}
Exemple #9
0
bool DataDirsAccess::InWriteDir(const std::string& path, const std::string& prefix)
{
	std::string locatedFile = LocateFile(path, FileQueryFlags::WRITE);
	return (locatedFile != "") && (locatedFile != path);
}
Exemple #10
0
bool DataDirsAccess::InReadDir(const std::string& path)
{
	std::string locatedFile = LocateFile(path);
	return (locatedFile != "") && (locatedFile != path);
}
bool FileSystem::InWriteDir(const std::string& path, const std::string& prefix)
{
	std::string locatedFile = LocateFile(path, WRITE);
	return (locatedFile != "") && (locatedFile != path);
}
bool FileSystem::InReadDir(const std::string& path)
{
	std::string locatedFile = LocateFile(path);
	return (locatedFile != "") && (locatedFile != path);
}
Exemple #13
0
		BOOL GetModuleVerInfoString(LPCTSTR szSubBlock, CString& szData)
		{
			CString szFilePath;
			return LocateFile(this, szFilePath) &&
				GetFileVerInfoString(szFilePath, szSubBlock, szData);
		}