-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_iris_data.c
124 lines (109 loc) · 3.16 KB
/
read_iris_data.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#define MAX_FILE_NAME 100
#define TRAIN_NUM 120
#define TEST_NUM 30
#define FEATURE 4
#define NUMBER_OF_CLASSES 3
int countNumRows(char *filename)
{
FILE *fp;
int count = 0; // Line counter (result)
//char filename[MAX_FILE_NAME];
char c; // To store a character read from file
// Get file name from user. The file should be
// either in current folder or complete path should be provided
//printf("Enter file name: ");
//scanf("%s", filename);
// Open the file
fp = fopen(filename, "r");
// Check if file exists
if (fp == NULL)
{
printf("Could not open file %s", filename);
return -1;
}
// Extract characters from file and store in character c
for (c = getc(fp); c != EOF; c = getc(fp))
if (c == '\n') // Increment count if this character is newline
count = count + 1;
// Close the file
fclose(fp);
//printf("The file %s has %d lines\n ", filename, count);
return count;
}
const char* getfield(char* line, int num)
{
const char* tok;
for (tok = strtok(line, ",");
tok && *tok;
tok = strtok(NULL, ",\n"))
{
if (!--num)
return tok;
}
return NULL;
}
/*
Labels for IRIS:
Iris-setosa - 0
Iris-versicolor - 1
Iris-virginica - 2
*/
void read_csv_iris(float *data, float *label, int row_count, char *filename)
{
//data = (float *)malloc(row_count*4*sizeof(float));
//label = (int *)malloc(row_count*sizeof(int));
FILE *fp = fopen(filename,"r");
char line[1024];
int count = 0;
int idx = 0;
for(int iter = 0;iter<row_count;iter++)
{
fgets(line,1024,fp);
const char *temp_field;
for(int i=0;i<5;i++)
{
float temp_num;
char *tmp = strdup(line);
temp_field = getfield(tmp,i+1);
if(i==4)
{
if(strcmp(temp_field,"Iris-setosa")==0)
{
label[idx] = 0;
continue;
}
if(strcmp(temp_field,"Iris-versicolor")==0)
{
label[idx] = 1;
continue;
}
if(strcmp(temp_field,"Iris-virginica")==0)
{
label[idx] = 2;
continue;
}
}
temp_num = atof(temp_field);
data[idx*4 + i] = temp_num;
}
idx++;
}
}
int main(int argc,char *argv[])
{
int count;
float *dataset_train,*dataset_test;
float *labels_train,*labels_test;
dataset_train = (float *)malloc(FEATURE * TRAIN_NUM*sizeof(float));
labels_train = (float *)malloc(TRAIN_NUM*sizeof(float));
dataset_test = (float *)malloc(FEATURE * TEST_NUM*sizeof(float));
labels_test = (float *)malloc(TEST_NUM*sizeof(float));
char file_train_set[] = "data/iris_train.data";
char file_test_set[] = "data/iris_test.data";
read_csv_iris(dataset_train,labels_train,TRAIN_NUM,file_train_set);
read_csv_iris(dataset_test,labels_test,TEST_NUM,file_test_set);
return 0;
}