642 |
07 Sep 06 |
peter |
// $Id$ |
642 |
07 Sep 06 |
peter |
2 |
|
675 |
10 Oct 06 |
jari |
3 |
/* |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2006 Jari Häkkinen, Peter Johansson |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2007 Peter Johansson |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2008 Jari Häkkinen, Peter Johansson |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2012 Peter Johansson |
675 |
10 Oct 06 |
jari |
8 |
|
1437 |
25 Aug 08 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
675 |
10 Oct 06 |
jari |
10 |
|
675 |
10 Oct 06 |
jari |
The yat library is free software; you can redistribute it and/or |
675 |
10 Oct 06 |
jari |
modify it under the terms of the GNU General Public License as |
1486 |
09 Sep 08 |
jari |
published by the Free Software Foundation; either version 3 of the |
675 |
10 Oct 06 |
jari |
License, or (at your option) any later version. |
675 |
10 Oct 06 |
jari |
15 |
|
675 |
10 Oct 06 |
jari |
The yat library is distributed in the hope that it will be useful, |
675 |
10 Oct 06 |
jari |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
675 |
10 Oct 06 |
jari |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
675 |
10 Oct 06 |
jari |
General Public License for more details. |
675 |
10 Oct 06 |
jari |
20 |
|
675 |
10 Oct 06 |
jari |
You should have received a copy of the GNU General Public License |
1487 |
10 Sep 08 |
jari |
along with yat. If not, see <http://www.gnu.org/licenses/>. |
675 |
10 Oct 06 |
jari |
23 |
*/ |
675 |
10 Oct 06 |
jari |
24 |
|
2881 |
18 Nov 12 |
peter |
25 |
#include <config.h> |
2881 |
18 Nov 12 |
peter |
26 |
|
642 |
07 Sep 06 |
peter |
27 |
#include "FeatureSelectorRandom.h" |
642 |
07 Sep 06 |
peter |
28 |
#include "MatrixLookup.h" |
642 |
07 Sep 06 |
peter |
29 |
#include "MatrixLookupWeighted.h" |
642 |
07 Sep 06 |
peter |
30 |
#include "Target.h" |
675 |
10 Oct 06 |
jari |
31 |
#include "yat/random/random.h" |
642 |
07 Sep 06 |
peter |
32 |
|
642 |
07 Sep 06 |
peter |
33 |
#include <algorithm> |
781 |
05 Mar 07 |
peter |
34 |
#include <cassert> |
642 |
07 Sep 06 |
peter |
35 |
|
642 |
07 Sep 06 |
peter |
36 |
namespace theplu { |
680 |
11 Oct 06 |
jari |
37 |
namespace yat { |
642 |
07 Sep 06 |
peter |
38 |
namespace classifier { |
642 |
07 Sep 06 |
peter |
39 |
|
642 |
07 Sep 06 |
peter |
40 |
|
642 |
07 Sep 06 |
peter |
41 |
FeatureSelectorRandom::FeatureSelectorRandom(size_t N) |
642 |
07 Sep 06 |
peter |
42 |
: FeatureSelector(N) |
642 |
07 Sep 06 |
peter |
43 |
{ |
642 |
07 Sep 06 |
peter |
44 |
} |
642 |
07 Sep 06 |
peter |
45 |
|
642 |
07 Sep 06 |
peter |
46 |
|
642 |
07 Sep 06 |
peter |
47 |
|
4200 |
19 Aug 22 |
peter |
48 |
void FeatureSelectorRandom::update(const MatrixLookup& data, |
642 |
07 Sep 06 |
peter |
49 |
const Target& target) |
642 |
07 Sep 06 |
peter |
50 |
{ |
642 |
07 Sep 06 |
peter |
51 |
assert(data.columns()==target.size()); |
642 |
07 Sep 06 |
peter |
52 |
update(data.rows()); |
642 |
07 Sep 06 |
peter |
53 |
} |
642 |
07 Sep 06 |
peter |
54 |
|
642 |
07 Sep 06 |
peter |
55 |
|
4200 |
19 Aug 22 |
peter |
56 |
void FeatureSelectorRandom::update(const MatrixLookupWeighted& data, |
642 |
07 Sep 06 |
peter |
57 |
const Target& target) |
642 |
07 Sep 06 |
peter |
58 |
{ |
642 |
07 Sep 06 |
peter |
59 |
assert(data.columns()==target.size()); |
642 |
07 Sep 06 |
peter |
60 |
update(data.rows()); |
642 |
07 Sep 06 |
peter |
61 |
} |
642 |
07 Sep 06 |
peter |
62 |
|
642 |
07 Sep 06 |
peter |
63 |
|
642 |
07 Sep 06 |
peter |
64 |
void FeatureSelectorRandom::update(size_t total_N) |
642 |
07 Sep 06 |
peter |
65 |
{ |
1134 |
23 Feb 08 |
peter |
66 |
std::vector<size_t>* features = new std::vector<size_t>; |
1134 |
23 Feb 08 |
peter |
67 |
features->reserve(total_N); |
642 |
07 Sep 06 |
peter |
68 |
for (size_t i=0; i<total_N; ++i) |
1134 |
23 Feb 08 |
peter |
69 |
features->push_back(i); |
642 |
07 Sep 06 |
peter |
// Peter should use random_sample here, but not included in std |
1134 |
23 Feb 08 |
peter |
71 |
random::random_shuffle(features->begin(), features->end()); |
1134 |
23 Feb 08 |
peter |
72 |
features->resize(N_); |
4200 |
19 Aug 22 |
peter |
73 |
features_ = |
1134 |
23 Feb 08 |
peter |
74 |
utility::Index(utility::SmartPtr<const std::vector<size_t> >(features)); |
642 |
07 Sep 06 |
peter |
75 |
} |
642 |
07 Sep 06 |
peter |
76 |
|
680 |
11 Oct 06 |
jari |
77 |
}}} // of namespace classifier, yat, and theplu |