-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathrandomforest.cpp
More file actions
109 lines (80 loc) · 3.15 KB
/
randomforest.cpp
File metadata and controls
109 lines (80 loc) · 3.15 KB
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
// Copyright (c) 2018 Christopher Taylor
//
// Distributed under the Boost Software License, Version 1.0.0. (See accompanying
// file LICENSE_1_0.0.txt or copy at http://www.boost.org/LICENSE_1_0.0.txt)
#include <phylanx/phylanx.hpp>
#include <phylanx/plugins/algorithms/impl/randomforest.hpp>
#include <phylanx/version.hpp>
#include <phylanx/config.hpp>
#include <phylanx/config/version.hpp>
#include <blaze/Blaze.h>
#include <hpx/hpx_init.hpp>
#include <hpx/include/agas.hpp>
#include <hpx/runtime_fwd.hpp>
#include <blaze/Math.h>
#include <boost/program_options.hpp>
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>
#include <iostream>
#include <phylanx/ir/node_data.hpp>
#include "impl/randomforest.hpp"
using namespace phylanx::algorithms::impl;
int hpx_main(boost::program_options::variables_map& vm)
{
// evaluate generated execution tree
auto ntrees = vm["trees"].as<std::uint64_t>();
auto mnsize = vm["minsize"].as<std::uint64_t>();
auto mxdepth = vm["maxdepth"].as<std::uint64_t>();
auto samplesize = vm["samples"].as<double>();
using namespace phylanx::algorithms::impl;
blaze::DynamicMatrix<double> train{ { 1.0, 1.0, 1.0, 1.0, 0.0 }
, { 1.0, 1.0, 1.0, 1.0, 0.0 }
, { 1.0, 1.0, 1.0, 1.0, 1.0 }
, { 1.0, 1.0, 1.0, 1.0, 1.0 }
};
blaze::DynamicVector<double> labels { 1.0, 1.0, 1.0, 1.0 };
auto const train_submat_data = blaze::submatrix( train, 0UL
, 0UL, train.rows(), train.columns()-1UL );
randomforest_impl rf(ntrees);
// Measure execution time
hpx::util::high_resolution_timer traintimer;
rf.fit(train, labels, mxdepth, mnsize, samplesize);
auto trainelapsed = traintimer.elapsed();
blaze::DynamicVector<double> results(train.rows());
// Make sure all counters are properly initialized,
// don't reset current counter values
hpx::reinit_active_counters(false);
hpx::util::high_resolution_timer predicttimer;
rf.predict(train, results);
auto predictelapsed = predicttimer.elapsed();
// Make sure all counters are properly initialized, don't reset current
// counter values
hpx::reinit_active_counters(false);
std::cout << "fit lapsed\t" << trainelapsed << std::endl;
std::cout << "predict lapsed\t" << predictelapsed << std::endl;
for(auto & r : results) {
std::cout << r << std::endl;
}
return hpx::finalize();
}
int main(int argc, char* argv[])
{
// command line handling
boost::program_options::options_description desc(
"usage: randomforest [options]");
desc.add_options()("trees,t",
boost::program_options::value<std::uint64_t>()->default_value(5),
"number of trees (default: 5)")("samples,s",
boost::program_options::value<double>()->default_value(1.0),
"ratio of sample size (default: 1.0)")("minsize,m",
boost::program_options::value<std::uint64_t>()->default_value(1),
"min size (default: 1")("maxdepth,d",
boost::program_options::value<std::uint64_t>()->default_value(10),
"max depth (default: 10)");
return hpx::init(desc, argc, argv);
}