예제 #1
0
int
main (int argc, char **argv)
{
	BonoboObject *context;

        g_thread_init (NULL);

	if (!bonobo_init (&argc, argv))
		g_error ("Could not initialize Bonobo");

	orb = bonobo_orb ();

	create_bag ();

	print_props ();

	/* FIXME: this is unusual, with a factory you normally
	 * want to use bonobo_running_context_auto_exit_unref */
	context = bonobo_context_running_get ();
	g_signal_connect_data (
		G_OBJECT (context), "last_unref",
		G_CALLBACK (quit_main), NULL, NULL, 0);
	bonobo_object_unref (context);

	bonobo_main ();

	return bonobo_debug_shutdown ();
}
예제 #2
0
void RandomForest::build() {
   srand(9167543);
   vector<pair<double,double> > oob_predict_acum(pXD_->trn_rows,pair<double,double>(0,0));
   vector<pair<double,double> > validate_predict_acum(pXD_->trn_rows,pair<double,double>(0,0));
   vector<pair<double,double> > tst_predict_acum(pXD_->tst_rows,pair<double,double>(0,0));
   vector<double> tst_predict(pXD_->tst_rows,0);
   vector<pair<int,int> > tst;
   for(int tst_idx=0; tst_idx < pXD_->tst_rows; tst_idx++) {
      tst.push_back(pair<int,int>(tst_idx,1));
   }
   int good_tree_cnt = 0;

   // the below is setup for the key press checking
   char c;
   int n, tem;
   int cnt = 0;
   tem = fcntl(0, F_GETFL, 0);
   fcntl (0, F_SETFL, (tem | O_NDELAY));
   // end key press setup

   vector<int> train_idx;
   vector<pair<int,int> > validate;
   for(int i=0; i<pXD_->trn_labl.size(); i++) {
      if((rand() % folds_) == fold_) validate.push_back(pair<int,int>(i,1));
      else train_idx.push_back(i);
   }

   int tree = 0;
   while(1) {
      //base_feature_sample_cnt_ = (int)(pow((double)(rand() % 1000)/1000.0,2) * 400); //(int)((double)train_idx.size()/3.0);
      vector<pair<int,int> > bag, oob;
      vector<int> w(train_idx.size(),0);
      for(int idx=0; idx<train_idx.size(); idx++) {
         //w[idx]=(int)(338 / pXD_->trn_attr[train_idx[idx]][(pXD_->trn_cols)-1]);
         w[idx]=1;
         //cerr << pXD_->trn_attr[train_idx[idx]][(pXD_->trn_cols)-1] << "," << w[idx] << endl;
      }
      create_bag(bag,oob,train_idx,w);
      //Node* root = new Node;
      Node root;
      int node_cnt=0; 
      double tree_gini=0;
      recAssignNode(&root, bag, "", 0, node_cnt, tree_gini);

      /*
      vector<double> v_predict(pXD_->trn_rows,0);
      recPredict(&root, validate, &(pXD_->trn_attr), v_predict, 0, 0);
      double d2 = 0;
      for(int i=0; i < validate.size(); i++) {
         d2+=pow(pXD_->trn_labl[validate[i].first] - v_predict[validate[i].first],2);
      }
      d2 /= (double)validate.size();
      */

      cerr << tree+1 << "/" << trees_ << "; M: " << base_feature_sample_cnt_ << "; bag_size: " << bag.size() \
           << "; oob_size: " << oob.size() /*<< "; d2: " << d2  */<< "; leaf_node_cnt: " << node_cnt \
           << "; tree_gini: " << tree_gini << endl;

      if(/*d2 < 0.3 || */1) {
         //double weight = 1 / pow((double)node_cnt,20);
         double weight = 1;
         predict(&root, &(pXD_->trn_attr), oob, oob_predict_acum, weight);
         predict(&root, &(pXD_->trn_attr), validate, validate_predict_acum, weight);
         predict(&root, &(pXD_->tst_attr), tst, tst_predict_acum, weight);
         good_tree_cnt++;
      }
      cerr << "good_tree_cnt: " << good_tree_cnt << endl;
      eval(oob_predict_acum,good_tree_cnt,"oob");
      eval(validate_predict_acum,good_tree_cnt,"validate");
      tree++;
      n=0;
      //n = read(0, &c, 1);
      if (n > 0 || good_tree_cnt == trees_) {
         cerr << "Key pressed: " << c << endl;
         break;
      }
   }
   cerr << endl;
   eval(oob_predict_acum,good_tree_cnt,"oob");
   eval(validate_predict_acum,good_tree_cnt,"validate");
   cerr << "writing tst set predictions..." << endl;

   char tstFile[100];
   sprintf(tstFile, "workspace/tst_predict_%d_%d_%d_%d.csv", fold_, folds_, base_feature_sample_cnt_, trees_);
   ofstream fTest(tstFile);
   if(!fTest.is_open()) {
      cerr << "Cannot open " << tstFile << " for writing" << endl;
      return;
   }
   for(int tst_idx=0; tst_idx < pXD_->tst_rows; tst_idx++) {
      double p = tst_predict_acum[tst_idx].first/(double)tst_predict_acum[tst_idx].second;
      p = (p*(double)good_tree_cnt + (double)ls1_)/(double)(good_tree_cnt+ls2_);
      fTest << p << endl;
   }
   fTest.close();

   char oobFile[100];
   sprintf(oobFile, "workspace/oob_predict_%d_%d_%d_%d.csv", fold_, folds_, base_feature_sample_cnt_, trees_);
   ofstream fOob(oobFile);
   if(!fOob.is_open()) {
      cerr << "Cannot open " << oobFile << " for writing" << endl;
      return;
   }
   for(int trn_idx=0;trn_idx < pXD_->trn_rows; trn_idx++) {
      double p = oob_predict_acum[trn_idx].first/(double)oob_predict_acum[trn_idx].second;
      fOob << pXD_->trn_labl[trn_idx] << "," << p << endl;
   }
   fOob.close();
}