Permalink
Browse files

Merge branch 'develop' of https://github.com/shogun-toolbox/shogun in…

…to vgorbati-develop
  • Loading branch information...
vladislav-horbatiuk committed Apr 21, 2013
2 parents c20178b + d30f205 commit 2d7d5744afab843af1b3adb0e289b659d9114aa2
@@ -20,11 +20,8 @@ void test_sigmoid_fitting()
labels->get_values().display_vector("scores");
labels->scores_to_probabilities();
labels->get_values().display_vector("probabilities");
/* only two probabilities will be the result, repeatedly,
* assert against reference implementation */
ASSERT(CMath::abs(labels->get_value(0)-0.8571428439385661)<10E-15);
ASSERT(CMath::abs(labels->get_value(1)-0.14285715606143384)<10E-15);
SG_UNREF(labels);
}
@@ -83,7 +83,10 @@ float64_t CROCEvaluation::evaluate_roc(CLabels* predicted, CLabels* ground_truth
}
// assure both number of positive and negative examples is >0
ASSERT(pos_count>0 && neg_count>0)
REQUIRE(pos_count>0, "%s::evaluate_roc(): Number of positive labels is "
"zero, ROC fails!\n", get_name());
REQUIRE(neg_count>0, "%s::evaluate_roc(): Number of negative labels is "
"zero, ROC fails!\n", get_name());
int32_t j = 0;
float64_t label;
@@ -57,36 +57,40 @@ CBinaryLabels* CBinaryLabels::obtain_from_generic(CLabels* base_labels)
void CBinaryLabels::ensure_valid(const char* context)
{
CDenseLabels::ensure_valid(context);
bool found_plus_one=false;
bool found_minus_one=false;
int32_t subset_size=get_num_labels();
for (int32_t i=0; i<subset_size; i++)
{
int32_t real_i=m_subset_stack->subset_idx_conversion(i);
if (m_labels[real_i]==+1.0)
found_plus_one=true;
else if (m_labels[real_i]==-1.0)
found_minus_one=true;
else
{
SG_ERROR("%s%sNot a two class labeling label[%d]=%f (only +1/-1 "
"allowed)\n", context?context:"", context?": ":"", i, m_labels[real_i]);
}
}
if (!found_plus_one)
{
SG_ERROR("%s%sNot a two class labeling - no positively labeled examples found\n",
context?context:"", context?": ":"");
}
if (!found_minus_one)
{
SG_ERROR("%s%sNot a two class labeling - no negatively labeled examples found\n",
context?context:"", context?": ":"");
}
CDenseLabels::ensure_valid(context);
bool found_plus_one=false;
bool found_minus_one=false;
int32_t subset_size=get_num_labels();
for (int32_t i=0; i<subset_size; i++)
{
int32_t real_i=m_subset_stack->subset_idx_conversion(i);
if (m_labels[real_i]==+1.0)
found_plus_one=true;
else if (m_labels[real_i]==-1.0)
found_minus_one=true;
else
{
SG_ERROR(
"%s%s%s::ensure_valid(): Not a two class labeling label[%d]=%f (only +1/-1 "
"allowed)\n", context ? context : "",
context ? ": " : "", get_name(), i, m_labels[real_i]);
}
}
if (!found_plus_one)
{
SG_ERROR(
"%s%s%s::ensure_valid(): Not a two class labeling - no positively labeled examples found\n",
context ? context : "", context ? ": " : "", get_name());
}
if (!found_minus_one)
{
SG_ERROR(
"%s%s%s::ensure_valid): Not a two class labeling - no negatively labeled examples found\n",
context ? context : "", context ? ": " : "", get_name());
}
}
ELabelType CBinaryLabels::get_label_type()
@@ -31,6 +31,7 @@ CDenseLabels::CDenseLabels(int32_t num_lab)
{
init();
m_labels = SGVector<float64_t>(num_lab);
m_current_values=SGVector<float64_t>(num_lab);
}
CDenseLabels::CDenseLabels(CFile* loader)
@@ -47,6 +48,7 @@ CDenseLabels::~CDenseLabels()
void CDenseLabels::init()
{
SG_ADD(&m_labels, "labels", "The labels.", MS_NOT_AVAILABLE);
SG_ADD(&m_current_values, "values", "Active values.", MS_NOT_AVAILABLE);
}
void CDenseLabels::set_to_one()
@@ -64,7 +66,10 @@ void CDenseLabels::set_to_const(float64_t c)
ASSERT(m_labels.vector)
index_t subset_size=get_num_labels();
for (int32_t i=0; i<subset_size; i++)
{
m_labels.vector[m_subset_stack->subset_idx_conversion(i)]=c;
m_current_values.vector[m_subset_stack->subset_idx_conversion(i)]=c;
}
}
void CDenseLabels::set_labels(SGVector<float64_t> v)
@@ -5,6 +5,7 @@
* (at your option) any later version.
*
* Written (W) 2013 Viktor Gal
* Written (W) 2013 Heiko Strathmann
*/
#include <shogun/base/init.h>
@@ -16,16 +17,20 @@ using namespace shogun;
TEST(ROCEvaluation,one)
{
int num_labels = 10;
CBinaryLabels* gt = new CBinaryLabels(num_labels);
CROCEvaluation* roc = new CROCEvaluation();
index_t num_labels=10;
CBinaryLabels* gt=new CBinaryLabels(num_labels);
CROCEvaluation* roc=new CROCEvaluation();
for (int i = 0; i < num_labels; i++) {
int l = (CMath::random(-1.0, 1.0) < 0 ? -1 : 1);
for (index_t i=0; i<num_labels; i++)
{
float64_t l=i%2==0 ? -1 : 1;
gt->set_value(l, i);
gt->set_label(i, l);
}
roc->evaluate(gt, gt);
float64_t auc=roc->evaluate(gt, gt);
EXPECT_EQ(auc, 1);
EXPECT_EQ(roc->get_auROC(), 1);
}
SG_UNREF(roc);
SG_UNREF(gt);
}
@@ -0,0 +1,33 @@
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2012-2013 Heiko Strathmann
*/
#include <shogun/base/init.h>
#include <shogun/labels/BinaryLabels.h>
#include <gtest/gtest.h>
using namespace shogun;
TEST(BinaryLabels,scores_to_probabilities)
{
CBinaryLabels* labels=new CBinaryLabels(10);
labels->set_values(SGVector<float64_t>(labels->get_num_labels()));
for (index_t i=0; i<labels->get_num_labels(); ++i)
labels->set_value(i%2==0 ? 1 : -1, i);
labels->get_values().display_vector("scores");
labels->scores_to_probabilities();
/* only two probabilities will be the result. Results from implementation that
* comes with the original paper, see BinaryLabels documentation */
EXPECT_NEAR(labels->get_value(0), 0.8571428439385661, 10E-15);
EXPECT_NEAR(labels->get_value(1), 0.14285715606143384, 10E-15);
SG_UNREF(labels);
}
@@ -67,37 +67,39 @@ TEST(Statistics, log_det_test_3)
// initialize the matrix
SGSparseMatrix<float64_t> M(size, size);
typedef SGSparseVectorEntry<float64_t> Entry;
SGSparseVector<float64_t> *vec=SG_MALLOC(SGSparseVector<float64_t>, size);
// for first row
Entry *first=new Entry[size];
Entry *first=SG_MALLOC(Entry, size);
first[0].feat_index=0; // the digonal index for row #1
first[0].entry=100;
for( index_t i=1; i<size; ++i )
{
first[i].feat_index=i; // fill the index for row #1
first[i].entry=1;
}
SGSparseVector<float64_t> vec(first, size);
M[0]=vec.get();
vec[0].features=first;
vec[0].num_feat_entries=size;
M[0]=vec[0].get();
// fill the rest of the rows
Entry* rest[size];
SGSparseVector<float64_t> v[size-1];
for( index_t i=1; i<size; ++i )
Entry* rest[size-1];
for( index_t i=0; i<size-1; ++i )
{
rest[i-1]=new Entry[2];
rest[i-1][0].feat_index=0; // the first column
rest[i-1][0].entry=1;
rest[i-1][1].feat_index=i; // the diagonal element
rest[i-1][1].entry=100;
v[i-1].features=rest[i-1];
v[i-1].num_feat_entries=2;
M[i]=v[i-1].get();
rest[i]=SG_MALLOC(Entry, 2);
rest[i][0].feat_index=0; // the first column
rest[i][0].entry=1;
rest[i][1].feat_index=i+1; // the diagonal element
rest[i][1].entry=100;
vec[i+1].features=rest[i];
vec[i+1].num_feat_entries=2;
M[i+1]=vec[i+1].get();
}
// check if log_det is equal to log(det(M))
EXPECT_NEAR(CStatistics::log_det(M), 4605.0649365774307, 1E-10);
SG_FREE(vec);
}
// TEST 4 - Sampling from Multivariate Gaussian distribution with Dense

0 comments on commit 2d7d574

Please sign in to comment.