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