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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
|
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <cstdlib>
#include "Dataset.h"
using std::cerr;
using std::ios;
using std::ifstream;
using std::string;
using std::cout;
using std::endl;
Dataset::Dataset(void)
{
pmtr.svm_type = C_SVC;
pmtr.kernel_type = SIGMOID;
pmtr.degree = 3;
//pmtr.gamma = 0;
pmtr.coef0 = 0;
pmtr.nu = 0.5;
pmtr.cache_size = 100;
pmtr.C = 1;
pmtr.eps =1e-3;
pmtr.p = 0.1;
pmtr.shrinking = 1;
pmtr.probability = 0;
pmtr.nr_weight = 0;
pmtr.gamma = 0.001;
pmtr.weight_label = NULL;
pmtr.weight = NULL;
}
Dataset::~Dataset(void)
{
}
void Dataset::getDataset(std::ifstream &infile)
{
position = 0;
elements = 0;
attributes = 0;
problem.l = 0;
int c = 0;
char character;
string s;
infile.open("C:\\visual C\\heart_scale.txt", ios::in);
if (!infile)
{
cerr << "Cant Open the File\n";
exit(1);
}
while (infile.get(character))
{
//if (character=='\n') break;
if (character==':')
{
++attributes;
} // count attributes
}
infile.seekg(0);
while(!infile.eof())
{
infile.get(character);
switch (character)
{
case '\n':
++problem.l;
case ':':
elements++;
case EOF:
break;
default:
;
} // end of switch
} // end of while
infile.seekg(0);
//infile.close();
Dataset::getSvmNodeDetails(infile, c, attributes);
model = svm_train(&problem,&pmtr);
makePrediction();
} // end of getDataset
void Dataset::makePrediction()
{
int correct_classification;
double error = 0;
double target;
double v;
double *estimates = NULL;
int svm_type = svm_get_svm_type(model);
int nr_class = svm_get_nr_class(model);
int *classes =(int *) malloc(nr_class*sizeof(int));
svm_get_labels(model, classes);
estimates=(double *) malloc(nr_class*sizeof(double));
for (i = 0; i<=problem.l; i++)
{
v = svm_predict(model, problem.x[i]);
if (problem.y[i] != v) {error++;}
} // end of for
std::cout << error << std::endl;
}
void Dataset::getSvmNodeDetails(std::ifstream &infile, int c, int attibutes)
{
infile.open("C:\\visual C\\heart_scale.txt",ios::in); //this is where i cannot open file
if (!infile)
{
cerr << "can't open input file\n";
exit(1);
}
problem.y = Malloc(double,problem.l);
problem.x = Malloc(struct svm_node *, problem.l);
x_values = Malloc(struct svm_node, elements + problem.l);
int max_index = 0;
j = 0;
char character;
char ch[2];
char val [80];
char attrib [3000];
int countch = 0;
int k;
char *stringPtr;
//char *endLine;
int lenght = 0;
// Get Label
for(i = 0;i< problem.l; i++)
{
infile.get(character);
ch[countch] = character;
countch++;
if (countch==2)
{
ch[countch] = '\0';
if ((ch[0] == '+') && (ch[1] =='1'))
{
problem.y[i] = 1;
}
if ((ch[0] == '-') && (ch[1] =='1'))
{
problem.y[i] = -1;
}
while(infile.get(character))
{
if (character=='\n') break;
}
countch = 0;
}
} // end of Loop
// Goto begining of the file
infile.seekg(0);
for (i=0;i<problem.l; i++)
{
problem.x[i] = &x_values[j];
k = 0;
j = 0;
while(infile.get(character))
{
if (character==' ') break;
}
while(!infile.eof())
{
//problem.x[i] = &x_values[j];
infile.get(character);
if (character != ':')
{
attrib[k] = character;
k++;
}
if (character=='\n')
{
attrib[k] = '\0';
// end of vector
// problem.x[i] = &x_values[j];
x_values[j++].index = -1;
// problem.x[i] = &x_values[j];
j= 0;
k = 0;
break;
}
if (character == ':')
{
j++;
attrib[k] = '\0';
k = 0;
infile.getline(val,80,' ');
// lenght = strlen(val);
// endLine=strrchr(val,'\n');
x_values[j].index = atoi(attrib);
x_values[j].value = strtod (val,&stringPtr);
} //
} // end of while
} // end of for Loop
infile.close();
} // end of getSVMDetails
| |