| 753 | } |
| 754 | |
| 755 | void test_lda () |
| 756 | { |
| 757 | // This test makes sure we pick the right direction in a simple 2D -> 1D LDA |
| 758 | typedef matrix<double,2,1> sample_type; |
| 759 | |
| 760 | std::vector<unsigned long> labels; |
| 761 | std::vector<sample_type> samples; |
| 762 | for (int i=0; i<4; i++) |
| 763 | { |
| 764 | sample_type s; |
| 765 | s(0) = i; |
| 766 | s(1) = i+1; |
| 767 | samples.push_back(s); |
| 768 | labels.push_back(1); |
| 769 | |
| 770 | sample_type s1; |
| 771 | s1(0) = i+1; |
| 772 | s1(1) = i; |
| 773 | samples.push_back(s1); |
| 774 | labels.push_back(2); |
| 775 | } |
| 776 | |
| 777 | matrix<double> X; |
| 778 | X.set_size(8,2); |
| 779 | for (int i=0; i<8; i++){ |
| 780 | X(i,0) = samples[i](0); |
| 781 | X(i,1) = samples[i](1); |
| 782 | } |
| 783 | |
| 784 | matrix<double,0,1> mean; |
| 785 | |
| 786 | dlib::compute_lda_transform(X,mean,labels,1); |
| 787 | |
| 788 | std::vector<double> vals1, vals2; |
| 789 | for (unsigned long i = 0; i < samples.size(); ++i) |
| 790 | { |
| 791 | double val = X*samples[i]-mean; |
| 792 | if (i%2 == 0) |
| 793 | vals1.push_back(val); |
| 794 | else |
| 795 | vals2.push_back(val); |
| 796 | dlog << LINFO << "1D LDA output: " << val; |
| 797 | } |
| 798 | |
| 799 | if (vals1[0] > vals2[0]) |
| 800 | swap(vals1, vals2); |
| 801 | |
| 802 | const double err = equal_error_rate(vals1, vals2).first; |
| 803 | dlog << LINFO << "LDA ERR: " << err; |
| 804 | DLIB_TEST(err == 0); |
| 805 | DLIB_TEST(equal_error_rate(vals2, vals1).first == 1); |
| 806 | } |
| 807 | |
| 808 | void test_equal_error_rate() |
| 809 | { |
nothing calls this directly
no test coverage detected