Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Some labels refactoring
  • Loading branch information
lisitsyn committed May 20, 2012
1 parent fa50f74 commit fc7021d
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 46 deletions.
25 changes: 16 additions & 9 deletions src/shogun/labels/BinaryLabels.cpp
Expand Up @@ -3,41 +3,48 @@

using namespace shogun;

CBinaryLabels::CBinaryLabels() : CDenseLabels()
CBinaryLabels::CBinaryLabels() : CDenseLabels(), m_threshold(0.0)
{
}

CBinaryLabels::CBinaryLabels(int32_t num_labels) : CDenseLabels(num_labels)
CBinaryLabels::CBinaryLabels(int32_t num_labels) : CDenseLabels(num_labels),
m_threshold(0.0)
{
}

CBinaryLabels::CBinaryLabels(const SGVector<float64_t> src) : CDenseLabels()
CBinaryLabels::CBinaryLabels(SGVector<float64_t> src) : CDenseLabels(),
m_threshold(0.0)
{
set_labels(src);
SGVector<float64_t> labels(src.vlen);
for (int32_t i=0; i<labels.vlen; i++)
labels[i] = CMath::sign(src[i]+m_threshold);
set_labels(labels);
set_confidences(src);
}

CBinaryLabels::CBinaryLabels(CFile* loader) : CDenseLabels(loader)
CBinaryLabels::CBinaryLabels(CFile* loader) : CDenseLabels(loader),
m_threshold(0.0)
{
}

bool CBinaryLabels::is_valid()
{
ASSERT(labels.vector);
ASSERT(m_labels.vector);
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 (labels.vector[real_i]==+1.0)
if (m_labels[real_i]==+1.0)
found_plus_one=true;
else if (labels.vector[real_i]==-1.0)
else if (m_labels[real_i]==-1.0)
found_minus_one=true;
else
{
SG_ERROR("Not a two class labeling label[%d]=%f (only +1/-1 "
"allowed)\n", i, labels.vector[real_i]);
"allowed)\n", i, m_labels[real_i]);
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/shogun/labels/BinaryLabels.h
Expand Up @@ -43,7 +43,7 @@ class CBinaryLabels : public CDenseLabels
*
* @param src labels to set
*/
CBinaryLabels(const SGVector<float64_t> src);
CBinaryLabels(SGVector<float64_t> src);

/** constructor
*
Expand All @@ -64,6 +64,11 @@ class CBinaryLabels : public CDenseLabels
* @return label type binary
*/
virtual ELabelType get_label_type();

protected:

/** threshold */
float64_t m_threshold;
};
}
#endif
56 changes: 28 additions & 28 deletions src/shogun/labels/DenseLabels.cpp
Expand Up @@ -30,7 +30,7 @@ CDenseLabels::CDenseLabels(int32_t num_lab)
: CLabels()
{
init();
labels=SGVector<float64_t>(num_lab);
m_labels = SGVector<float64_t>(num_lab);
}

CDenseLabels::CDenseLabels(CFile* loader)
Expand All @@ -46,7 +46,7 @@ CDenseLabels::~CDenseLabels()

void CDenseLabels::init()
{
SG_ADD(&labels, "labels", "The labels.", MS_NOT_AVAILABLE);
SG_ADD(&m_labels, "labels", "The labels.", MS_NOT_AVAILABLE);
}

void CDenseLabels::set_to_one()
Expand All @@ -61,18 +61,18 @@ void CDenseLabels::zero()

void CDenseLabels::set_to_const(float64_t c)
{
ASSERT(labels.vector);
ASSERT(m_labels.vector);
index_t subset_size=get_num_labels();
for (int32_t i=0; i<subset_size; i++)
labels.vector[m_subset_stack->subset_idx_conversion(i)]=c;
m_labels.vector[m_subset_stack->subset_idx_conversion(i)]=c;
}

void CDenseLabels::set_labels(SGVector<float64_t> v)
{
if (m_subset_stack->has_subsets())
SG_ERROR("A subset is set, cannot set labels\n");

labels=v;
m_labels = v;

is_valid();
}
Expand All @@ -82,30 +82,30 @@ SGVector<float64_t> CDenseLabels::get_labels()
if (m_subset_stack->has_subsets())
SG_ERROR("get_labels() is not possible on subset");

return labels;
return m_labels;
}

SGVector<float64_t> CDenseLabels::get_labels_copy()
{
if (!m_subset_stack->has_subsets())
return labels.clone();
return m_labels.clone();

index_t num_labels=get_num_labels();
SGVector<float64_t> result(SG_MALLOC(float64_t, num_labels), num_labels);
index_t num_labels = get_num_labels();
SGVector<float64_t> result(num_labels);

/* copy element wise because of possible subset */
for (index_t i=0; i<num_labels; i++)
result[i]=get_label(i);
result[i] = get_label(i);

return result;
}

SGVector<int32_t> CDenseLabels::get_int_labels()
{
SGVector<int32_t> intlab(get_num_labels(), true);
SGVector<int32_t> intlab(get_num_labels());

for (int32_t i=0; i<get_num_labels(); i++)
intlab.vector[i]= get_int_label(i);
intlab.vector[i] = get_int_label(i);

return intlab;
}
Expand All @@ -115,7 +115,7 @@ void CDenseLabels::set_int_labels(SGVector<int32_t> lab)
if (m_subset_stack->has_subsets())
SG_ERROR("set_int_labels() is not possible on subset");

labels = SGVector<float64_t>(lab.vlen);
m_labels = SGVector<float64_t>(lab.vlen);

for (int32_t i=0; i<lab.vlen; i++)
set_int_label(i, lab.vector[i]);
Expand All @@ -126,9 +126,9 @@ void CDenseLabels::load(CFile* loader)
remove_subset();

SG_SET_LOCALE_C;
labels=SGVector<float64_t>();
m_labels = SGVector<float64_t>();
ASSERT(loader);
loader->get_vector(labels.vector, labels.vlen);
loader->get_vector(m_labels.vector, m_labels.vlen);
SG_RESET_LOCALE;
}

Expand All @@ -139,17 +139,17 @@ void CDenseLabels::save(CFile* writer)

SG_SET_LOCALE_C;
ASSERT(writer);
ASSERT(labels.vector && labels.vlen>0);
writer->set_vector(labels.vector, labels.vlen);
ASSERT(m_labels.vector && m_labels.vlen>0);
writer->set_vector(m_labels.vector, m_labels.vlen);
SG_RESET_LOCALE;
}

bool CDenseLabels::set_label(int32_t idx, float64_t label)
{
int32_t real_num=m_subset_stack->subset_idx_conversion(idx);
if (labels.vector && real_num<get_num_labels())
if (m_labels.vector && real_num<get_num_labels())
{
labels.vector[real_num]=label;
m_labels.vector[real_num]=label;
return true;
}
else
Expand All @@ -159,9 +159,9 @@ bool CDenseLabels::set_label(int32_t idx, float64_t label)
bool CDenseLabels::set_int_label(int32_t idx, int32_t label)
{
int32_t real_num=m_subset_stack->subset_idx_conversion(idx);
if (labels.vector && real_num<get_num_labels())
if (m_labels.vector && real_num<get_num_labels())
{
labels.vector[real_num]= (float64_t) label;
m_labels.vector[real_num] = (float64_t)label;
return true;
}
else
Expand All @@ -171,22 +171,22 @@ bool CDenseLabels::set_int_label(int32_t idx, int32_t label)
float64_t CDenseLabels::get_label(int32_t idx)
{
int32_t real_num=m_subset_stack->subset_idx_conversion(idx);
ASSERT(labels.vector && idx<get_num_labels());
return labels.vector[real_num];
ASSERT(m_labels.vector && idx<get_num_labels());
return m_labels.vector[real_num];
}

int32_t CDenseLabels::get_int_label(int32_t idx)
{
int32_t real_num=m_subset_stack->subset_idx_conversion(idx);
ASSERT(labels.vector && idx<get_num_labels());
if (labels.vector[real_num] != float64_t((int32_t(labels.vector[real_num]))))
SG_ERROR("label[%d]=%g is not an integer\n", labels.vector[real_num]);
ASSERT(m_labels.vector && idx<get_num_labels());
if (m_labels.vector[real_num] != float64_t((int32_t(m_labels.vector[real_num]))))
SG_ERROR("label[%d]=%g is not an integer\n", m_labels.vector[real_num]);

return int32_t(labels.vector[real_num]);
return int32_t(m_labels.vector[real_num]);
}

int32_t CDenseLabels::get_num_labels()
{
return m_subset_stack->has_subsets()
? m_subset_stack->get_size() : labels.vlen;
? m_subset_stack->get_size() : m_labels.vlen;
}
2 changes: 1 addition & 1 deletion src/shogun/labels/DenseLabels.h
Expand Up @@ -194,7 +194,7 @@ class CDenseLabels : public CLabels

protected:
/** the label vector */
SGVector<float64_t> labels;
SGVector<float64_t> m_labels;
};
}
#endif
8 changes: 4 additions & 4 deletions src/shogun/labels/MulticlassLabels.cpp
Expand Up @@ -23,15 +23,15 @@ CMulticlassLabels::CMulticlassLabels(CFile* loader) : CDenseLabels(loader)

bool CMulticlassLabels::is_valid()
{
ASSERT(labels.vector);
ASSERT(m_labels.vector);

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);
int32_t label= int32_t(labels.vector[real_i]);
int32_t real_i = m_subset_stack->subset_idx_conversion(i);
int32_t label = int32_t(m_labels[real_i]);

if (label<0 || float64_t(label)!=labels.vector[real_i])
if (label<0 || float64_t(label)!=m_labels[real_i])
{
SG_ERROR("Multiclass Labels must be in range 0...<nr_classes-1> and integers!\n");
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/labels/MulticlassLabels.h
Expand Up @@ -46,7 +46,7 @@ class CMulticlassLabels : public CDenseLabels
*
* @param src labels to set
*/
CMulticlassLabels(const SGVector<float64_t> src);
CMulticlassLabels(SGVector<float64_t> src);

/** constructor
*
Expand Down
4 changes: 2 additions & 2 deletions src/shogun/labels/RealLabels.cpp
Expand Up @@ -22,8 +22,8 @@ CRealLabels::CRealLabels(CFile* loader) : CDenseLabels(loader)

bool CRealLabels::is_valid()
{
ASSERT(labels.vector);
return true;
ASSERT(m_labels.vector);
return true;
}

ELabelType CRealLabels::get_label_type()
Expand Down

0 comments on commit fc7021d

Please sign in to comment.