source: trunk/yat/classifier/FeatureSelectorRandom.cc @ 1706

Last change on this file since 1706 was 1487, checked in by Jari Häkkinen, 13 years ago

Addresses #436. GPL license copy reference should also be updated.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 1.9 KB
Line 
1// $Id: FeatureSelectorRandom.cc 1487 2008-09-10 08:41:36Z jari $
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 3 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 yat. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23#include "FeatureSelectorRandom.h"
24#include "MatrixLookup.h"
25#include "MatrixLookupWeighted.h"
26#include "Target.h"
27#include "yat/random/random.h"
28
29#include <algorithm>
30#include <cassert>
31
32namespace theplu {
33namespace yat {
34namespace classifier {
35
36
37  FeatureSelectorRandom::FeatureSelectorRandom(size_t N)
38    : FeatureSelector(N)
39  {
40  }
41
42
43
44  void FeatureSelectorRandom::update(const MatrixLookup& data, 
45                                     const Target& target)
46  {
47    assert(data.columns()==target.size());
48    update(data.rows());
49  }
50
51
52  void FeatureSelectorRandom::update(const MatrixLookupWeighted& data, 
53                                 const Target& target)
54  {
55    assert(data.columns()==target.size());
56    update(data.rows());
57  }
58
59
60  void FeatureSelectorRandom::update(size_t total_N)
61  {
62    std::vector<size_t>* features = new std::vector<size_t>;
63    features->reserve(total_N);
64    for (size_t i=0; i<total_N; ++i)
65      features->push_back(i);
66    // Peter should use random_sample here, but not included in std
67    random::random_shuffle(features->begin(), features->end());
68    features->resize(N_);
69    features_ = 
70      utility::Index(utility::SmartPtr<const std::vector<size_t> >(features));
71  }
72
73}}} // of namespace classifier, yat, and theplu
Note: See TracBrowser for help on using the repository browser.