几个内核函数

#pragma once

namespace _datum_analysis
{
    /* gaussian kernel */
    template <typename _Ty>
    class gaussian_kernel
    {
    public:
        double operator () (_Ty _distance, const double _lambda = 0.2)
        {
            double m_t = _distance / _lambda;
            return (exp(-0.5 * m_t * m_t));
        }
    };
    /* 二次方 Epanechnikov kernel */
    template <typename _Ty>
    class epanechnikov_kernel
    {
    public:
        double operator () (_Ty _distance, const double _lambda = 0.2)
        {
            double m_t = _distance / _lambda;
            if (abs(m_t) < 1.0)
                return (0.75 * (1 - m_t * m_t));
            else
                return 0;
        }
    };
    /* 三次方 Epanechnikov kernel */
    template <typename _Ty>
    class epanechnikov_pan_kernel
    {
    public:
        double operator () (_Ty _distance, const double _lambda = 0.2)
        {
            double m_t = _distance / _lambda;
            if (abs(m_t) < 1.0)
            {
                double m_meta = 1 - m_t * m_t * m_t;
                return (m_meta * m_meta * m_meta);
            }
            else
                return 0;
        }
    };

    /* Nadaraya - Watson - kernel 加权平均 */
    template <class _Mat_Type, class _mean = epanechnikov_pan_kernel<double> >
    class kernel_mean_probability
    {
        typedef typename _Mat_Type::value_type value_type;
        typedef typename _Mat_Type::const_reference const_reference;
        typedef typename _Mat_Type::const_iterator const_iterator;
    public:
        double operator () (const _Mat_Type& _matt, const int _main_class, const double _lambda = 0.2)
        {
            double m_predict_count = 0;
            double m_predict_sum   = 0;
/*  epanechnikov_pan_kernel 更加紧致核  */
            std::for_each(_matt.begin(), _matt.end(), [&](const_reference _meta) -> void
            {
                double weight = _mean()(_meta.m_distance, _lambda);
                if (_meta.m_instance._class_type == _main_class)
                    m_predict_count += weight;
                m_predict_sum += weight;
            });
            if (abs(m_predict_sum) < equate_zero) return 0;
            return (100.0 * m_predict_count / m_predict_sum);
        }
    };

    /* Nadaraya - Watson - kernel 加权平均 */
    template <class _Mat_Type, class _mean = epanechnikov_pan_kernel<double> >
    class kernel_mean_regression
    {
        typedef typename _Mat_Type::value_type value_type;
        typedef typename _Mat_Type::const_reference const_reference;
        typedef typename _Mat_Type::const_iterator const_iterator;
    public:
        double operator () (const _Mat_Type& _matt, const double _lambda = 0.2)
        {
            double m_predict_rise = 0;
            double m_predict_sum = 0;
            /*  epanechnikov_pan_kernel 更加紧致核  */
            std::for_each(_matt.begin(), _matt.end(), [&](const_reference _meta) -> void
            {
                double weight = _mean()(_meta.m_distance, _lambda);
                m_predict_rise += weight * _meta.m_instance._actual_rise;
                m_predict_sum += weight;
            });
            if (abs(m_predict_sum) < equate_zero) return 0;
            return (m_predict_rise / m_predict_sum);
        }
    };
}

参考:
范明 柴玉梅 昝红英 等译(2003) 统计学习基础—数据挖掘、推理与预测 电子工业出版社 115~133

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值