47 SG_ERROR(
"Specified features are not of type CDotFeatures\n")
58 int32_t* classidx_neg=SG_MALLOC(int32_t, num_vec);
59 int32_t* classidx_pos=SG_MALLOC(int32_t, num_vec);
65 for (i=0; i<train_labels.
vlen; i++)
67 if (train_labels.
vector[i]==-1)
68 classidx_neg[num_neg++]=i;
69 else if (train_labels.
vector[i]==+1)
70 classidx_pos[num_pos++]=i;
73 SG_ERROR(
"found label != +/- 1 bailing...")
78 if (num_neg<=0 || num_pos<=0)
80 SG_ERROR(
"whooooo ? only a single class found\n")
87 memset(mean_neg,0,num_feat*
sizeof(
float64_t));
90 memset(mean_pos,0,num_feat*
sizeof(
float64_t));
93 double* scatter=SG_MALLOC(
double, num_feat*num_feat);
94 double* buffer=SG_MALLOC(
double, num_feat*
CMath::max(num_neg, num_pos));
95 int nf = (int) num_feat;
99 for (i=0; i<num_neg; i++)
107 for (j=0; j<vlen; j++)
110 buffer[num_feat*i+j]=vec[j];
116 for (j=0; j<num_feat; j++)
117 mean_neg[j]/=num_neg;
119 for (i=0; i<num_neg; i++)
121 for (j=0; j<num_feat; j++)
122 buffer[num_feat*i+j]-=mean_neg[j];
124 cblas_dgemm(CblasColMajor, CblasNoTrans, CblasTrans, nf, nf,
125 (
int) num_neg, 1.0, buffer, nf, buffer, nf, 0, scatter, nf);
128 for (i=0; i<num_pos; i++)
136 for (j=0; j<vlen; j++)
139 buffer[num_feat*i+j]=vec[j];
145 for (j=0; j<num_feat; j++)
146 mean_pos[j]/=num_pos;
148 for (i=0; i<num_pos; i++)
150 for (j=0; j<num_feat; j++)
151 buffer[num_feat*i+j]-=mean_pos[j];
153 cblas_dgemm(CblasColMajor, CblasNoTrans, CblasTrans, nf, nf, (
int) num_pos,
154 1.0/(train_labels.
vlen-1), buffer, nf, buffer, nf,
155 1.0/(train_labels.
vlen-1), scatter, nf);
160 for (i=0; i<num_feat*num_feat; i++)
163 for (i=0; i<num_feat; i++)
164 scatter[i*num_feat+i]+= trace*
m_gamma/num_feat;
167 scatter, num_feat, num_feat, NULL);
172 cblas_dsymv(CblasColMajor, CblasUpper, nf, 1.0, inv_scatter, nf,
173 (
double*) mean_pos, 1, 0., (
double*) w_pos, 1);
174 cblas_dsymv(CblasColMajor, CblasUpper, nf, 1.0, inv_scatter, nf,
175 (
double*) mean_neg, 1, 0, (
double*) w_neg, 1);
178 for (i=0; i<num_feat; i++)
193 SG_FREE(inv_scatter);
194 SG_FREE(classidx_neg);
195 SG_FREE(classidx_pos);
virtual bool train_machine(CFeatures *data=NULL)
static float64_t dot(const bool *v1, const bool *v2, int32_t n)
compute dot product between v1 and v2 (blas optimized)
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)
Features that support dot products among other operations.
ST * get_feature_vector(int32_t num, int32_t &len, bool &dofree)
virtual int32_t get_dim_feature_space() const =0
void free_feature_vector(ST *feat_vec, int32_t num, bool dofree)
static float64_t trace(float64_t *mat, int32_t cols, int32_t rows)
static T max(T a, T b)
return the maximum of two integers
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)
void display_vector(const char *name="vector", const char *prefix="") const