Changeset 10771:a99c79bb08d1 in orange
 Timestamp:
 04/06/12 13:30:23 (2 years ago)
 Branch:
 default
 Location:
 source/orange
 Files:

 2 edited
Legend:
 Unmodified
 Added
 Removed

source/orange/liblinear_interface.cpp
r10683 r10771 211 211 { 212 212 std::istringstream str_stream(buffer); 213 str_stream.exceptions(ios::failbit  ios::badbit); 213 214 return linear_load_model_alt(str_stream); 214 215 } … … 233 234 } 234 235 235 feature_node *feature_nodeFromExample(const TExample &ex, map<int, int> &indexMap, bool includeMeta=false, bool includeRegular=true){ 236 //cout << "example " << endl; 237 int numOfNodes = countFeatures(ex, includeMeta, includeRegular); 238 /*if (includeRegular) 239 numOfNodes += ex.domain>attributes>size(); 240 if (includeMeta) 241 numOfNodes += ex.meta.size();*/ 242 feature_node *nodes = new feature_node[numOfNodes]; 236 feature_node *feature_nodeFromExample(const TExample &ex, double bias){ 237 int n_nodes = countFeatures(ex, false, true); 238 239 if (bias >= 0.0) 240 n_nodes++; 241 242 feature_node *nodes = new feature_node[n_nodes]; 243 243 feature_node *ptr = nodes; 244 244 245 int index = 1; 245 int featureIndex = 1; 246 if (includeRegular){ 247 for (TExample::iterator i=ex.begin(); i!=ex.end(); i++){ 248 if ((i>varType==TValue::INTVAR  (i>varType==TValue::FLOATVAR && (*i==*i))) && i>isRegular() && i!=&ex.getClass()){ 249 if (i>varType==TValue::INTVAR) 250 ptr>value = (int) *i; 251 else 252 ptr>value = (float) *i; 253 ptr>index = index; 254 if (indexMap.find(index)==indexMap.end()){ 255 ptr>index = featureIndex; 256 indexMap[index] = featureIndex++; 257 } else 258 ptr>index = indexMap[index]; 259 //featureIndices.insert(index); 260 //cout << ptr>value << " "; 261 ptr++; 262 } 263 index++; 264 } 265 } 266 if (includeMeta){ 267 feature_node *first = ptr; 268 for (TMetaValues::const_iterator i=ex.meta.begin(); i!=ex.meta.end(); i++){ 269 if ((i>second.valueType==TValue::INTVAR  i>second.valueType==TValue::FLOATVAR) && i>second.isRegular()){ 270 ptr>value = (float) i>second; 271 //ptr>index = index  i>first; 272 if (indexMap.find(i>first)==indexMap.end()){ 273 ptr>index = featureIndex; 274 indexMap[i>first] = featureIndex++; 275 } else 276 ptr>index = indexMap[i>first]; 277 //featureIndices.insert(ptr>index); 278 ptr++; 279 } 280 } 281 //cout << endl << " sorting" << endl; 282 sort(first, ptr, NodeSort()); 283 } 246 247 for (TExample::iterator i=ex.begin(); i!=ex.end(); i++) 248 if (i!=&ex.getClass()){ 249 if ((i>varType==TValue::INTVAR  (i>varType==TValue::FLOATVAR && (*i==*i))) && i>isRegular()){ 250 if (i>varType==TValue::INTVAR) 251 ptr>value = (int) *i; 252 else 253 ptr>value = (float) *i; 254 ptr>index = index; 255 ptr++; 256 } 257 index++; 258 } 259 260 if (bias >= 0.0) 261 { 262 ptr>value = bias; 263 ptr>index = index; 264 ptr++; 265 } 266 284 267 ptr>index = 1; 285 268 return nodes; 286 269 } 287 270 288 problem *problemFromExamples(PExampleGenerator examples, map<int, int> &indexMap, bool includeMeta=false, bool includeRegular=true){271 problem *problemFromExamples(PExampleGenerator examples, double bias){ 289 272 problem *prob = new problem; 290 273 prob>l = examples>numberOfExamples(); 274 prob>n = examples>domain>attributes>size(); 275 276 if (bias >= 0) 277 prob>n++; 278 291 279 prob>x = new feature_node* [prob>l]; 292 280 prob>y = new int [prob>l]; 293 prob>bias = 1.0;281 prob>bias = bias; 294 282 feature_node **ptrX = prob>x; 295 283 int *ptrY = prob>y; 296 284 PEITERATE(iter, examples){ 297 *ptrX = feature_nodeFromExample(*iter, indexMap, includeMeta, includeRegular);285 *ptrX = feature_nodeFromExample(*iter, bias); 298 286 *ptrY = (int) (*iter).getClass(); 299 287 ptrX++; 300 288 ptrY++; 301 289 } 302 prob>n = indexMap.size();303 //cout << "prob>n " << prob>n <<endl;304 290 return prob; 305 291 } … … 317 303 solver_type = L2R_LR; 318 304 eps = 0.01f; 319 C=1; 305 C = 1; 306 bias = 1.0; 320 307 set_print_string_function(&dont_print_string); 321 308 } 322 309 323 310 PClassifier TLinearLearner::operator()(PExampleGenerator examples, const int &weight){ 324 //cout << "initializing param" << endl;325 311 parameter *param = new parameter; 326 312 param>solver_type = solver_type; … … 330 316 param>weight_label = NULL; 331 317 param>weight = NULL; 332 //cout << "initializing problem" << endl; 333 map<int, int> *indexMap =new map<int, int>; 334 problem *prob = problemFromExamples(examples, *indexMap); 335 //cout << "cheking parameters" << endl; 318 319 PVariable classVar = examples>domain>classVar; 320 if (!classVar) 321 raiseError("classVar expected"); 322 if (classVar>varType != TValue::INTVAR) 323 raiseError("Discrete class expected"); 324 325 problem *prob = problemFromExamples(examples, bias); 326 336 327 const char * error_msg = check_parameter(prob, param); 337 328 if (error_msg){ … … 350 341 destroy_problem(prob); 351 342 352 return PClassifier(mlnew TLinearClassifier(examples>domain>classVar, examples, model , indexMap));353 } 354 355 TLinearClassifier::TLinearClassifier(const PVariable &var, PExampleTable _examples, struct model *_model , map<int, int> *_indexMap){343 return PClassifier(mlnew TLinearClassifier(examples>domain>classVar, examples, model)); 344 } 345 346 TLinearClassifier::TLinearClassifier(const PVariable &var, PExampleTable _examples, struct model *_model){ 356 347 classVar = var; 348 domain = _examples>domain; 349 examples = _examples; 357 350 linmodel = _model; 358 examples = _examples;359 d omain = examples>domain;360 indexMap = _indexMap; 351 bias = _model>bias; 352 dbias = _model>bias; 353 361 354 computesProbabilities = check_probability_model(linmodel) != 0; 362 355 int nr_classifier = (linmodel>nr_class==2 && linmodel>param.solver_type != MCSVM_CS)? 1 : linmodel>nr_class; 356 357 int nr_feature = linmodel>nr_feature; 358 if (linmodel>bias >= 0.0) 359 nr_feature++; 360 363 361 weights = mlnew TFloatListList(nr_classifier); 364 for (int i =0; i<nr_classifier; i++){365 weights>at(i) = mlnew TFloatList( linmodel>nr_feature);366 for (int j =0; j<linmodel>nr_feature; j++)362 for (int i = 0; i < nr_classifier; i++){ 363 weights>at(i) = mlnew TFloatList(nr_feature); 364 for (int j = 0; j < nr_feature; j++) 367 365 weights>at(i)>at(j) = linmodel>w[j*nr_classifier+i]; 368 366 } … … 372 370 if (linmodel) 373 371 free_and_destroy_model(&linmodel); 374 if (indexMap)375 delete indexMap;376 372 } 377 373 … … 380 376 int numClass = get_nr_class(linmodel); 381 377 map<int, int> indexMap; 382 feature_node *x = feature_nodeFromExample(new_example, indexMap, false);378 feature_node *x = feature_nodeFromExample(new_example, bias); 383 379 384 380 int *labels = new int [numClass]; … … 402 398 int numClass = get_nr_class(linmodel); 403 399 map<int, int> indexMap; 404 feature_node *x = feature_nodeFromExample(new_example, indexMap, false);400 feature_node *x = feature_nodeFromExample(new_example, bias); 405 401 406 402 int predict_label = predict(linmodel, x); 
source/orange/liblinear_interface.hpp
r8978 r10771 52 52 float eps; //P Stopping criteria 53 53 float C; //P Regularization parameter 54 float bias; //P bias parameter (default 1.0  no bias) 54 55 55 56 TLinearLearner(); … … 61 62 __REGISTER_CLASS 62 63 TLinearClassifier() {}; 63 TLinearClassifier(const PVariable &var, PExampleTable examples, model *_model , map<int, int> *indexMap=NULL);64 TLinearClassifier(const PVariable &var, PExampleTable examples, model *_model); 64 65 ~TLinearClassifier(); 65 66 … … 69 70 PFloatListList weights; //P Computed feature weights 70 71 PExampleTable examples; //P Examples used to train the classifier 71 72 float bias; //PR bias 72 73 model *getModel(){ return linmodel; } 73 74 private: 74 75 model *linmodel; 75 map<int, int> *indexMap;76 double dbias; 76 77 }; 77 78
Note: See TracChangeset
for help on using the changeset viewer.