source: branches/0.4-stable/yat/classifier/FeatureSelectorRandom.cc @ 1392

Last change on this file since 1392 was 1392, checked in by Peter, 15 years ago

trac has moved

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.0 KB
Line 
1// $Id: FeatureSelectorRandom.cc 1392 2008-07-28 19:35:30Z peter $
2
3/*
4  Copyright (C) 2006, 2007 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2008 Peter Johansson
6
7  This file is part of the yat library, http://dev.thep.lu.se/yat
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
25#include "FeatureSelectorRandom.h"
26#include "MatrixLookup.h"
27#include "MatrixLookupWeighted.h"
28#include "Target.h"
29#include "yat/random/random.h"
30
31#include <algorithm>
32#include <cassert>
33
34namespace theplu {
35namespace yat {
36namespace 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  {
64    std::vector<size_t>* features = new std::vector<size_t>;
65    features->reserve(total_N);
66    for (size_t i=0; i<total_N; ++i)
67      features->push_back(i);
68    // Peter should use random_sample here, but not included in std
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));
73  }
74
75}}} // of namespace classifier, yat, and theplu
Note: See TracBrowser for help on using the repository browser.