11 #include <shogun/lib/config.h> 22 using namespace Eigen;
67 SG_ERROR(
"Specified features are not of type CDotFeatures\n")
73 REQUIRE(data,
"Features have not been provided.\n")
83 return CLDA::train_machine_templated<float32_t>(train_labels, data);
85 return CLDA::train_machine_templated<float64_t>(train_labels, data);
87 return CLDA::train_machine_templated<floatmax_t>(train_labels, data);
92 template <
typename ST>
96 ->get_feature_matrix();
97 int32_t num_feat=feature_matrix.
num_rows;
98 int32_t num_vec=feature_matrix.
num_cols;
107 for(i=0; i<train_labels.
vlen; i++)
109 if (train_labels.
vector[i]==-1)
110 classidx_neg[num_neg++]=i;
112 else if(train_labels.
vector[i]==+1)
113 classidx_pos[num_pos++]=i;
125 for(i=0; i<num_neg; i++)
126 mean_neg+=fmatrix.col(classidx_neg[i]);
127 mean_neg/=(ST)num_neg;
130 for(i=0; i<num_neg; i++)
131 fmatrix.col(classidx_neg[i])-=mean_neg;
134 for(i=0; i<num_pos; i++)
135 mean_pos+=fmatrix.col(classidx_pos[i]);
136 mean_pos/=(ST)num_pos;
139 for(i=0; i<num_pos; i++)
140 fmatrix.col(classidx_pos[i])-=mean_pos;
149 cov_mat=fmatrix*fmatrix.transpose();
150 scatter=cov_mat/(num_vec-1);
151 ST trace=scatter.
trace();
154 scatter.diagonal()+=Eigen::DenseBase<typename SGVector<ST>::EigenVectorXt>::Constant(num_feat, trace*((ST)
m_gamma)/num_feat);
164 LLT<typename SGMatrix<ST>::EigenMatrixXt> decomposition(scatter);
165 x=decomposition.solve(mean_pos-mean_neg);
172 bias=((
float64_t)(0.5*(w_neg.dot(mean_neg)-w_pos.dot(mean_pos))));
186 for(i=0; i<num_pos;i++)
187 cen_pos.col(i)=fmatrix.col(classidx_pos[i]);
189 for(i=0; i<num_neg;i++)
190 cen_neg.col(i)=fmatrix.col(classidx_neg[i]);
193 #if EIGEN_WITH_OPERATOR_BUG 194 cen_pos=cen_pos*cen_pos.transpose();
195 cen_pos/=(ST)(num_pos-1);
197 cen_pos=cen_pos*cen_pos.transpose()/((ST)(num_pos-1));
201 #if EIGEN_WITH_OPERATOR_BUG 202 cen_neg=cen_neg*cen_neg.transpose();
203 cen_neg/=(ST)(num_neg-1);
205 cen_neg=cen_neg*cen_neg.transpose()/((ST)(num_neg-1));
213 Sw.diagonal()+=Eigen::DenseBase<typename SGVector<ST>::EigenVectorXt>::Constant(num_feat, trace*((ST)
m_gamma)/num_feat);
220 Sb.col(0)=sqrt(num_pos)*(mean_pos-mean_total);
221 Sb.col(1)=sqrt(num_neg)*(mean_neg-mean_total);
223 JacobiSVD<typename SGMatrix<ST>::EigenMatrixXt> svd(fmatrix1, ComputeThinU);
228 Sb=Q.transpose()*(Sb*(Sb.transpose()))*Q;
231 Sw=Q.transpose()*Sw*Q;
236 HouseholderQR<typename SGMatrix<ST>::EigenMatrixXt> decomposition(Sw.llt().matrixU().transpose());
242 JacobiSVD<typename SGMatrix<ST>::EigenMatrixXt> svd2(decomposition.solve((decomposition.solve(Sb))
243 .transpose()).transpose(), ComputeThinU);
248 x=Q*(svd2.matrixU().col(0));
258 for(i = 0; i < w.size(); ++i)
virtual const char * get_name() const =0
virtual bool train_machine(CFeatures *data=NULL)
bool train_machine_templated(SGVector< int32_t > train_labels, CFeatures *features)
virtual ELabelType get_label_type() const =0
Eigen::Map< EigenMatrixXt, 0, Eigen::Stride< 0, 0 > > EigenMatrixXtMap
virtual void set_w(const SGVector< float64_t > src_w)
The class Labels models labels, i.e. class assignments of objects.
bool has_property(EFeatureProperty p) const
virtual int32_t get_num_vectors() const =0
virtual void set_features(CDotFeatures *feat)
virtual CDotFeatures * get_features()
Features that support dot products among other operations.
CLDA(float64_t gamma=0, ELDAMethod method=AUTO_LDA)
static float64_t trace(float64_t *mat, int32_t cols, int32_t rows)
Class LinearMachine is a generic interface for all kinds of linear machines like classifiers.
all of classes and functions are contained in the shogun namespace
The class Features is the base class of all feature objects.
Binary Labels for binary classification.
virtual void set_labels(CLabels *lab)
virtual EFeatureType get_feature_type() const =0