Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "lib/common.h"
00012 #include "kernel/Chi2Kernel.h"
00013 #include "features/Features.h"
00014 #include "features/SimpleFeatures.h"
00015 #include "lib/io.h"
00016
00017 using namespace shogun;
00018
00019 CChi2Kernel::CChi2Kernel(int32_t size, float64_t w)
00020 : CSimpleKernel<float64_t>(size), width(w)
00021 {
00022 }
00023
00024 CChi2Kernel::CChi2Kernel(
00025 CSimpleFeatures<float64_t>* l, CSimpleFeatures<float64_t>* r, float64_t w, int32_t size)
00026 : CSimpleKernel<float64_t>(size), width(w)
00027 {
00028 init(l,r);
00029 }
00030
00031 CChi2Kernel::~CChi2Kernel()
00032 {
00033 cleanup();
00034 }
00035
00036 bool CChi2Kernel::init(CFeatures* l, CFeatures* r)
00037 {
00038 bool result=CSimpleKernel<float64_t>::init(l,r);
00039 init_normalizer();
00040 return result;
00041 }
00042
00043 float64_t CChi2Kernel::compute(int32_t idx_a, int32_t idx_b)
00044 {
00045 int32_t alen, blen;
00046 bool afree, bfree;
00047
00048 float64_t* avec=
00049 ((CSimpleFeatures<float64_t>*) lhs)->get_feature_vector(idx_a, alen, afree);
00050 float64_t* bvec=
00051 ((CSimpleFeatures<float64_t>*) rhs)->get_feature_vector(idx_b, blen, bfree);
00052 ASSERT(alen==blen);
00053
00054 float64_t result=0;
00055 for (int32_t i=0; i<alen; i++)
00056 {
00057 float64_t n=avec[i]-bvec[i];
00058 float64_t d=avec[i]+bvec[i];
00059 if (d!=0)
00060 result+=n*n/d;
00061 }
00062
00063 result=exp(-result/width);
00064
00065 ((CSimpleFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
00066 ((CSimpleFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
00067
00068 return result;
00069 }