"git@developer.sourcefind.cn:OpenDAS/nerfacc.git" did not exist on "ef812085d6a4e34066e175b97e63ee04dfb6fe89"
other.cpp 1.9 KB
Newer Older
Davis King's avatar
Davis King committed
1
2
3
#include <boost/python.hpp>
#include <boost/shared_ptr.hpp>
#include <dlib/matrix.h>
4
5
#include <dlib/data_io.h>
#include <dlib/sparse_vector.h>
Davis King's avatar
Davis King committed
6
7
8
9
10

using namespace dlib;
using namespace std;
using namespace boost::python;

11
12
typedef std::vector<std::pair<unsigned long,double> > sparse_vect;

Davis King's avatar
Davis King committed
13
14
15
16
17
18
19
tuple get_training_data()
{
    typedef matrix<double,0,1> sample_type;
    std::vector<sample_type> samples;
    std::vector<double> labels;

    sample_type samp(3);
20
21
22
23
24
25
26
27
28
29
30

    for (int i = 0; i < 10; ++i)
    {
        samp = 1,2,3;
        samples.push_back(samp);
        labels.push_back(+1);

        samp = -1,-2,-3;
        samples.push_back(samp);
        labels.push_back(-1);
    }
Davis King's avatar
Davis King committed
31
32
33
34

    return make_tuple(samples, labels);
}

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
void _make_sparse_vector (
    sparse_vect& v
)
{
    make_sparse_vector_inplace(v);
}

void _make_sparse_vector2 (
    std::vector<sparse_vect>& v
)
{
    for (unsigned long i = 0; i < v.size(); ++i)
        make_sparse_vector_inplace(v[i]);
}

tuple _load_libsvm_formatted_data (
    const std::string& file_name
) 
{ 
    std::vector<sparse_vect> samples;
    std::vector<double> labels;
    load_libsvm_formatted_data(file_name, samples, labels); 
    return make_tuple(samples, labels);
}

void _save_libsvm_formatted_data (
    const std::string& file_name,
    const std::vector<sparse_vect>& samples,
    const std::vector<double>& labels
) { save_libsvm_formatted_data(file_name, samples, labels); }

Davis King's avatar
Davis King committed
66
67
68
void bind_other()
{
    def("get_training_data",get_training_data);
69
70
71
72
    def("make_sparse_vector", _make_sparse_vector , "This function modifies its argument so that it is a properly sorted sparse vector.");
    def("make_sparse_vector", _make_sparse_vector2 , "This function modifies a sparse_vectors object so that all elements it contains are properly sorted sparse vectors.");
    def("load_libsvm_formatted_data",_load_libsvm_formatted_data);
    def("save_libsvm_formatted_data",_save_libsvm_formatted_data);
Davis King's avatar
Davis King committed
73
74
}