Feature Selection Toolbox FST4
UTIA · Czech Academy of Sciences
Loading...
Searching...
No Matches
dense_train_tools.hpp
Go to the documentation of this file.
1#ifndef FSTDENSETRAINTOOLS_H
2#define FSTDENSETRAINTOOLS_H
3 /*
25=========================================================================
26Copyright and license:
27 * FST4 software (with the exception of any bundled or externally linked
28 third-party components) is Copyright (C) 2000-present Institute of
29 Information Theory and Automation of the Czech Academy of Sciences (UTIA).
30 * FST4 is free software: you can redistribute it and/or modify it under
31 the terms of the GNU Affero General Public License as published by the
32 Free Software Foundation, version 3. The full text of the license is in
33 the file LICENSE in the distribution root and at
34 https://www.gnu.org/licenses/agpl-3.0.html.
35 * FST4 is distributed in the hope that it will be useful, but WITHOUT ANY
36 WARRANTY; without even the implied warranty of MERCHANTABILITY or
37 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
38 License for more details.
39 * Closed-source use without the AGPL's copyleft obligations requires a
40 commercial license from UTIA; in case of interest write to
41 utia@utia.cas.cz. LICENSING.md in the distribution root explains both
42 options, the contributor license agreement that every contribution
43 requires, and the contacts (technical questions and pull requests:
44 somol@utia.cas.cz, with "FST" in the subject line).
45========================================================================== */
46
47#include <cmath>
48#include <cstddef>
49#include <memory>
50#include <vector>
51#include "data_accessor.hpp"
52
53namespace FST {
54
83template<typename REALTYPE, typename IDXTYPE, typename DIMTYPE, class DATAACCESSOR, class AFTERPATTERN>
84std::size_t standardize_design(const std::shared_ptr<DATAACCESSOR> &da, const DIMTYPE classes,
85 const std::size_t d, const std::size_t zstride, const std::vector<DIMTYPE> &index,
86 std::vector<REALTYPE> &Z, std::vector<REALTYPE> &mean, std::vector<REALTYPE> &scale,
87 AFTERPATTERN after_pattern)
88{
89 const DIMTYPE stride=da->getNoOfFeatures();
90 mean.assign(d,0.0); scale.assign(d,0.0);
91 Z.clear();
92 typename DATAACCESSOR::PPattern p;
93 IDXTYPE s,i;
94 std::size_t N=0;
95 for(DIMTYPE c=0;c<classes;c++)
96 {
97 da->setClass(c);
98 for(const auto &blk : da->blocks(TRAIN)) for(i=0,p=blk.data,s=blk.size;i<s;i++)
99 {
100 const auto *pat=&p[(std::size_t)i*stride];
101 for(std::size_t f=0;f<d;f++) {
102 const REALTYPE v=(REALTYPE)pat[index[f]];
103 Z.push_back(v);
104 mean[f]+=v;
105 }
106 after_pattern(pat,c);
107 ++N;
108 }
109 }
110 if(N==0) return 0;
111 for(std::size_t f=0;f<d;f++) mean[f]/=(REALTYPE)N;
112 // second pass over the stored raw values: centered sums of squares. The one-pass form
113 // E[x^2]-mean^2 cancels catastrophically once |mean| >> sd (features offset by 1e8 changed
114 // logreg/linsvm/rbfsvm/ridge results although standardization is translation-invariant);
115 // the design matrix is in memory anyway, so the extra pass costs one sweep
116 for(std::size_t n=0;n<N;n++)
117 for(std::size_t f=0;f<d;f++) {const REALTYPE dev=Z[n*zstride+f]-mean[f]; scale[f]+=dev*dev;}
118 for(std::size_t f=0;f<d;f++)
119 {
120 const REALTYPE var=scale[f]/(REALTYPE)N;
121 scale[f]=(var>0.0)?std::sqrt(var):1.0; // constant feature -> scale 1, stays inert
122 }
123 for(std::size_t n=0;n<N;n++)
124 for(std::size_t f=0;f<d;f++) Z[n*zstride+f]=(Z[n*zstride+f]-mean[f])/scale[f];
125 return N;
126}
127
132template<typename REALTYPE>
133bool cholesky_factor(std::vector<REALTYPE> &A, const std::size_t m)
134{
135 for(std::size_t j=0;j<m;j++)
136 {
137 REALTYPE diag=A[j*m+j];
138 for(std::size_t k=0;k<j;k++) diag-=A[j*m+k]*A[j*m+k];
139 if(!(diag>0.0) || !std::isfinite(diag)) return false;
140 diag=std::sqrt(diag);
141 A[j*m+j]=diag;
142 for(std::size_t i=j+1;i<m;i++)
143 {
144 REALTYPE v=A[i*m+j];
145 for(std::size_t k=0;k<j;k++) v-=A[i*m+k]*A[j*m+k];
146 A[i*m+j]=v/diag;
147 }
148 }
149 return true;
150}
151
155template<typename REALTYPE>
156void cholesky_solve(const std::vector<REALTYPE> &A, const std::size_t m,
157 const REALTYPE *rhs, const bool negate_rhs, REALTYPE *out)
158{
159 for(std::size_t i=0;i<m;i++)
160 {
161 REALTYPE v=negate_rhs?-rhs[i]:rhs[i];
162 for(std::size_t k=0;k<i;k++) v-=A[i*m+k]*out[k];
163 out[i]=v/A[i*m+i];
164 }
165 for(std::size_t ii=m;ii>0;ii--)
166 {
167 const std::size_t i=ii-1;
168 REALTYPE v=out[i];
169 for(std::size_t k=i+1;k<m;k++) v-=A[k*m+i]*out[k];
170 out[i]=v/A[i*m+i];
171 }
172}
173
174} // namespace FST
175#endif // FSTDENSETRAINTOOLS_H
Defines interface for data access implementations.
The Feature Selection Toolbox 4 library: every library component – data access, criteria and classifi...
Definition data_accessor.hpp:80
std::size_t standardize_design(const std::shared_ptr< DATAACCESSOR > &da, const DIMTYPE classes, const std::size_t d, const std::size_t zstride, const std::vector< DIMTYPE > &index, std::vector< REALTYPE > &Z, std::vector< REALTYPE > &mean, std::vector< REALTYPE > &scale, AFTERPATTERN after_pattern)
Definition dense_train_tools.hpp:84
@ TRAIN
the split's training part
Definition data_accessor.hpp:85
bool cholesky_factor(std::vector< REALTYPE > &A, const std::size_t m)
Definition dense_train_tools.hpp:133
void cholesky_solve(const std::vector< REALTYPE > &A, const std::size_t m, const REALTYPE *rhs, const bool negate_rhs, REALTYPE *out)
Definition dense_train_tools.hpp:156
Copyright © 2000–present ÚTIA, Institute of Information Theory and Automation of the Czech Academy of Sciences · Contacts · This site sets no cookies and uses no analytics.