21 using namespace shogun;
46 SG_ERROR(
"Specified features are not of type CDotFeatures\n");
57 int32_t* classidx_neg=
SG_MALLOC(int32_t, num_vec);
58 int32_t* classidx_pos=
SG_MALLOC(int32_t, num_vec);
64 for (i=0; i<train_labels.
vlen; i++)
66 if (train_labels.
vector[i]==-1)
67 classidx_neg[num_neg++]=i;
68 else if (train_labels.
vector[i]==+1)
69 classidx_pos[num_pos++]=i;
72 SG_ERROR(
"found label != +/- 1 bailing...");
77 if (num_neg<=0 && num_pos<=0)
79 SG_ERROR(
"whooooo ? only a single class found\n");
88 memset(mean_neg,0,num_feat*
sizeof(
float64_t));
91 memset(mean_pos,0,num_feat*
sizeof(
float64_t));
94 double* scatter=
SG_MALLOC(
double, num_feat*num_feat);
96 int nf = (int) num_feat;
100 for (i=0; i<num_neg; i++)
108 for (j=0; j<vlen; j++)
111 buffer[num_feat*i+j]=vec[j];
117 for (j=0; j<num_feat; j++)
118 mean_neg[j]/=num_neg;
120 for (i=0; i<num_neg; i++)
122 for (j=0; j<num_feat; j++)
123 buffer[num_feat*i+j]-=mean_neg[j];
125 cblas_dgemm(CblasColMajor, CblasNoTrans, CblasTrans, nf, nf,
126 (
int) num_neg, 1.0, buffer, nf, buffer, nf, 0, scatter, nf);
129 for (i=0; i<num_pos; i++)
137 for (j=0; j<vlen; j++)
140 buffer[num_feat*i+j]=vec[j];
146 for (j=0; j<num_feat; j++)
147 mean_pos[j]/=num_pos;
149 for (i=0; i<num_pos; i++)
151 for (j=0; j<num_feat; j++)
152 buffer[num_feat*i+j]-=mean_pos[j];
154 cblas_dgemm(CblasColMajor, CblasNoTrans, CblasTrans, nf, nf, (
int) num_pos,
155 1.0/(train_labels.
vlen-1), buffer, nf, buffer, nf,
156 1.0/(train_labels.
vlen-1), scatter, nf);
161 for (i=0; i<num_feat*num_feat; i++)
164 for (i=0; i<num_feat; i++)
165 scatter[i*num_feat+i]+= trace*
m_gamma/num_feat;
168 scatter, num_feat, num_feat, NULL);
173 cblas_dsymv(CblasColMajor, CblasUpper, nf, 1.0, inv_scatter, nf,
174 (
double*) mean_pos, 1, 0., (
double*) w_pos, 1);
175 cblas_dsymv(CblasColMajor, CblasUpper, nf, 1.0, inv_scatter, nf,
176 (
double*) mean_neg, 1, 0, (
double*) w_neg, 1);
179 for (i=0; i<num_feat; i++)
180 w[i]=w_pos[i]-w_neg[i];
bool has_property(EFeatureProperty p)
virtual bool train_machine(CFeatures *data=NULL)
The class Labels models labels, i.e. class assignments of objects.
static float64_t * pinv(float64_t *matrix, int32_t rows, int32_t cols, float64_t *target=NULL)
ST * get_feature_vector(int32_t num, int32_t &len, bool &dofree)
virtual int32_t get_num_vectors() const =0
virtual void set_features(CDotFeatures *feat)
void free_feature_vector(ST *feat_vec, int32_t num, bool dofree)
Features that support dot products among other operations.
static float64_t trace(float64_t *mat, int32_t cols, int32_t rows)
virtual int32_t get_dim_feature_space() const =0
virtual void free_vector()
SGVector< int32_t > get_int_labels()
static void display_vector(const T *vector, int32_t n, const char *name="vector")
display vector (useful for debugging)
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.
static float64_t dot(const bool *v1, const bool *v2, int32_t n)
compute dot product between v1 and v2 (blas optimized)
The class Features is the base class of all feature objects.
virtual void set_labels(CLabels *lab)
#define SG_MALLOC(type, len)