Boost logo

Boost :

From: Niitsuma Hirotaka (hirotaka.niitsuma_at_[hidden])
Date: 2007-01-11 07:38:56


I made k-nearest neighbor program based on Boost.Accumulators
plz see attached file.

usage

        ublas::vector<double> v1(3),v2(3),v3(3), v0(3);

                v1(0)=3; v1(1)=1; v1(2)=2;
                v2(0)=1; v2(1)=20; v2(2)=0;
                v3(0)=1; v3(1)=1; v3(2)=3;
                v0=ublas::zero_vector<double>(3);

        struct norm_2_diff_functor
        {
                double
                operator ()(const boost::numeric::ublas::vector<double> &e1 , const
boost::numeric::ublas::vector<double> &e2)
                {
                        return(boost::numeric::ublas::norm_2(e1-e2) );
                }
        };

        accumulators::accumulator_set<
                ublas::vector<double>
                , accumulators::stats<
                        accumulators::tag::k_nearest_neighbor
>
> acc(
                accumulators::k_nearest_neighbor_distance = norm_2_diff_functor()
                ,accumulators::sample =v0
        );
                acc(v1);
                acc(v2);
                acc(v3);

                typedef ublas::vector<double> float_type;
                typedef double distance_type;
                typedef std::vector<float_type> array_type;
                typedef boost::iterator_range<typename array_type::iterator>
array_range_type;
                typedef std::pair<distance_type, typename array_type::iterator> dist_it_type;
                typedef std::vector< dist_it_type > dist_it_array_type;
                typedef boost::iterator_range<typename dist_it_array_type::iterator>
dist_it_array_range_type;
                typedef std::pair< dist_it_array_range_type , array_range_type > result_type;

                result_type result;

        result =accumulators::k_nearest_neighbor(
                acc
                 ,accumulators::k_nearest_neighbor_k=1
                 ,accumulators::k_nearest_neighbor_target=v0
                );

        std::cout << result.first[0].first << " " << *result.first[0].second
 << std::endl;

        result =accumulators::k_nearest_neighbor(
                acc
                 ,accumulators::k_nearest_neighbor_k=3
                 ,accumulators::k_nearest_neighbor_target=v0
                );

        std::cout << result.first[0].first << " " << result.first[1].first
<< " " << result.first[2].first;




Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk