[642] | 1 | // $Id: FeatureSelectorRandom.cc 1392 2008-07-28 19:35:30Z peter $ |
---|
| 2 | |
---|
[675] | 3 | /* |
---|
[1275] | 4 | Copyright (C) 2006, 2007 Jari Häkkinen, Peter Johansson |
---|
| 5 | Copyright (C) 2008 Peter Johansson |
---|
[675] | 6 | |
---|
[1392] | 7 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
[675] | 8 | |
---|
| 9 | The yat library is free software; you can redistribute it and/or |
---|
| 10 | modify it under the terms of the GNU General Public License as |
---|
| 11 | published by the Free Software Foundation; either version 2 of the |
---|
| 12 | License, or (at your option) any later version. |
---|
| 13 | |
---|
| 14 | The yat library is distributed in the hope that it will be useful, |
---|
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 17 | General Public License for more details. |
---|
| 18 | |
---|
| 19 | You should have received a copy of the GNU General Public License |
---|
| 20 | along with this program; if not, write to the Free Software |
---|
| 21 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
| 22 | 02111-1307, USA. |
---|
| 23 | */ |
---|
| 24 | |
---|
[642] | 25 | #include "FeatureSelectorRandom.h" |
---|
| 26 | #include "MatrixLookup.h" |
---|
| 27 | #include "MatrixLookupWeighted.h" |
---|
| 28 | #include "Target.h" |
---|
[675] | 29 | #include "yat/random/random.h" |
---|
[642] | 30 | |
---|
| 31 | #include <algorithm> |
---|
[781] | 32 | #include <cassert> |
---|
[642] | 33 | |
---|
| 34 | namespace theplu { |
---|
[680] | 35 | namespace yat { |
---|
[642] | 36 | namespace classifier { |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | FeatureSelectorRandom::FeatureSelectorRandom(size_t N) |
---|
| 40 | : FeatureSelector(N) |
---|
| 41 | { |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | |
---|
| 45 | |
---|
| 46 | void FeatureSelectorRandom::update(const MatrixLookup& data, |
---|
| 47 | const Target& target) |
---|
| 48 | { |
---|
| 49 | assert(data.columns()==target.size()); |
---|
| 50 | update(data.rows()); |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | |
---|
| 54 | void FeatureSelectorRandom::update(const MatrixLookupWeighted& data, |
---|
| 55 | const Target& target) |
---|
| 56 | { |
---|
| 57 | assert(data.columns()==target.size()); |
---|
| 58 | update(data.rows()); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | |
---|
| 62 | void FeatureSelectorRandom::update(size_t total_N) |
---|
| 63 | { |
---|
[1134] | 64 | std::vector<size_t>* features = new std::vector<size_t>; |
---|
| 65 | features->reserve(total_N); |
---|
[642] | 66 | for (size_t i=0; i<total_N; ++i) |
---|
[1134] | 67 | features->push_back(i); |
---|
[642] | 68 | // Peter should use random_sample here, but not included in std |
---|
[1134] | 69 | random::random_shuffle(features->begin(), features->end()); |
---|
| 70 | features->resize(N_); |
---|
| 71 | features_ = |
---|
| 72 | utility::Index(utility::SmartPtr<const std::vector<size_t> >(features)); |
---|
[642] | 73 | } |
---|
| 74 | |
---|
[680] | 75 | }}} // of namespace classifier, yat, and theplu |
---|