1 | /*************************************************************************/ |
---|
2 | /* */ |
---|
3 | /* Program to prepare data file for cross-validation */ |
---|
4 | /* ------------------------------------------------- */ |
---|
5 | /* */ |
---|
6 | /* The number of blocks for the cross-validation appears as the */ |
---|
7 | /* first argument. The data are shuffled and divided into the */ |
---|
8 | /* specified number of blocks, with class distributions as even */ |
---|
9 | /* as possible in each block. */ |
---|
10 | /* */ |
---|
11 | /*************************************************************************/ |
---|
12 | |
---|
13 | |
---|
14 | #include <math.h> |
---|
15 | #include <stdio.h> |
---|
16 | |
---|
17 | |
---|
18 | long random(); |
---|
19 | #define randf ((random()&2147483647) / 2147483648.0) |
---|
20 | #define ForEach(var,F,L) for(var=F; var<=L; ++var) |
---|
21 | #define dig(x) (x >= '0' && x <= '9') |
---|
22 | |
---|
23 | #define MAXLINE 5000 /* maximum line length */ |
---|
24 | |
---|
25 | char **Item; |
---|
26 | int ItemSpace=1000, MaxItem=0; |
---|
27 | |
---|
28 | |
---|
29 | main(argc, argv) |
---|
30 | /* ---- */ |
---|
31 | int argc; |
---|
32 | char *argv[]; |
---|
33 | { |
---|
34 | int i, First=0, Last, Length, Splits; |
---|
35 | char Line[MAXLINE], **ClassPtr, *Temp, *BeginClass(); |
---|
36 | |
---|
37 | sscanf(argv[1], "%d", &Splits); |
---|
38 | |
---|
39 | Item = (char **) malloc(ItemSpace * sizeof(char *)); |
---|
40 | |
---|
41 | while ( fgets(Line, MAXLINE, stdin) ) |
---|
42 | { |
---|
43 | if ( MaxItem >= ItemSpace ) |
---|
44 | { |
---|
45 | ItemSpace += 1000; |
---|
46 | Item = (char **) realloc(Item, ItemSpace * sizeof(char *)); |
---|
47 | } |
---|
48 | |
---|
49 | Length = strlen(Line)+2; |
---|
50 | Item[MaxItem] = (char *) malloc(Length); |
---|
51 | memcpy(Item[MaxItem], Line, Length); |
---|
52 | MaxItem++; |
---|
53 | } |
---|
54 | |
---|
55 | if ( ! MaxItem-- ) exit(1); |
---|
56 | |
---|
57 | Shuffle(); |
---|
58 | |
---|
59 | /* Find classes */ |
---|
60 | |
---|
61 | ClassPtr = (char **) malloc((MaxItem+1) * sizeof(char *)); |
---|
62 | ForEach(i, 0, MaxItem) |
---|
63 | { |
---|
64 | ClassPtr[i] = BeginClass(Item[i]); |
---|
65 | } |
---|
66 | |
---|
67 | /* Sort by class */ |
---|
68 | |
---|
69 | fprintf(stderr, "\nClass frequencies:\n"); |
---|
70 | |
---|
71 | while ( First <= MaxItem ) |
---|
72 | { |
---|
73 | Last = First; |
---|
74 | |
---|
75 | ForEach(i, First+1, MaxItem) |
---|
76 | { |
---|
77 | if ( ! strcmp(ClassPtr[i], ClassPtr[First]) ) |
---|
78 | { |
---|
79 | Last++; |
---|
80 | Temp = Item[Last]; |
---|
81 | Item[Last] = Item[i]; |
---|
82 | Item[i] = Temp; |
---|
83 | |
---|
84 | Temp = ClassPtr[Last]; |
---|
85 | ClassPtr[Last] = ClassPtr[i]; |
---|
86 | ClassPtr[i] = Temp; |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | fprintf(stderr, "%6d class %s\n", Last-First+1, ClassPtr[First]); |
---|
91 | |
---|
92 | First = Last+1; |
---|
93 | } |
---|
94 | |
---|
95 | ForEach(First, 0, Splits-1) |
---|
96 | { |
---|
97 | for ( i = First ; i <= MaxItem ; i += Splits ) |
---|
98 | { |
---|
99 | printf("%s\n", Item[i]); |
---|
100 | } |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | |
---|
105 | |
---|
106 | /*************************************************************************/ |
---|
107 | /* */ |
---|
108 | /* Find the beginning character of a class name */ |
---|
109 | /* */ |
---|
110 | /*************************************************************************/ |
---|
111 | |
---|
112 | |
---|
113 | char *BeginClass(S) |
---|
114 | /* ---------- */ |
---|
115 | char *S; |
---|
116 | { |
---|
117 | char *F; |
---|
118 | |
---|
119 | F = S - 1; |
---|
120 | do |
---|
121 | { |
---|
122 | S = F + 1; |
---|
123 | while ( *S == ' ' || *S == '\t' || *S == '\n' ) S++; |
---|
124 | F = S; |
---|
125 | while ( *F != ',' && (*F != '.' || dig(*(F+1))) && *F != '\n' ) F++; |
---|
126 | } while ( *F == ',' ); |
---|
127 | |
---|
128 | if ( *F != '.' ) *F = '.'; |
---|
129 | *(F+1) = '\0'; |
---|
130 | |
---|
131 | return S; |
---|
132 | } |
---|
133 | |
---|
134 | |
---|
135 | |
---|
136 | /*************************************************************************/ |
---|
137 | /* */ |
---|
138 | /* Shuffle the data items */ |
---|
139 | /* */ |
---|
140 | /*************************************************************************/ |
---|
141 | |
---|
142 | |
---|
143 | Shuffle() |
---|
144 | /* ------- */ |
---|
145 | { |
---|
146 | int this, alt, left = MaxItem+1; |
---|
147 | char *hold; |
---|
148 | |
---|
149 | this = 0; |
---|
150 | while ( left ) |
---|
151 | { |
---|
152 | alt = this + (left--) * randf; |
---|
153 | if ( alt > MaxItem || alt < this ) |
---|
154 | { |
---|
155 | fprintf(stderr, "ERROR!\n"); |
---|
156 | exit(1); |
---|
157 | } |
---|
158 | hold = Item[this]; |
---|
159 | Item[this++] = Item[alt]; |
---|
160 | Item[alt] = hold; |
---|
161 | } |
---|
162 | } |
---|