Guest User

Untitled

a guest
Aug 13th, 2012
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. float64_t C = 1.0;
  2. index_t num_samples = 50;
  3. CMath::init_random(5);
  4. SGMatrix<float64_t> data =
  5. CDataGenerator::generate_gaussians(num_samples, 2, 2);
  6. CDenseFeatures<float64_t> features(data);
  7.  
  8. SGVector<index_t> train_idx(num_samples), test_idx(num_samples);
  9. SGVector<float64_t> labels(num_samples);
  10. for (index_t i = 0, j = 0; i < data.num_cols; ++i)
  11. {
  12. if (i % 2 == 0)
  13. train_idx[j] = i;
  14. else
  15. test_idx[j++] = i;
  16.  
  17. labels[i/2] = (i < data.num_cols/2) ? 0.0 : 1.0;
  18. }
  19.  
  20. CDenseFeatures<float64_t>* train_feats = (CDenseFeatures<float64_t>*)features.copy_subset(train_idx);
  21. CDenseFeatures<float64_t>* test_feats = (CDenseFeatures<float64_t>*)features.copy_subset(test_idx);
  22.  
  23. CMulticlassLabels* ground_truth = new CMulticlassLabels(labels);
  24.  
  25. //CMulticlassOCAS* mocas = new CMulticlassOCAS(C, train_feats, ground_truth);
  26. CMulticlassLibLinear* mocas = new CMulticlassLibLinear(C, train_feats, ground_truth);
  27. mocas->io->set_loglevel(MSG_DEBUG);
  28. mocas->train();
  29.  
  30. CLabels* pred = mocas->apply_multiclass(test_feats);
  31. for (int i = 0; i < num_samples; ++i)
  32. std::cout << ((CMulticlassLabels*)pred)->get_label(i) << " " << ((CMulticlassLabels)labels).get_label(i) << std::endl;
  33.  
  34. SG_UNREF(mocas);
  35. SG_UNREF(train_feats);
  36. SG_UNREF(test_feats);
  37. SG_UNREF(pred);
Advertisement
Add Comment
Please, Sign In to add comment