Skip to content

Commit

Permalink
Refactor a bit Hierarchical clustering to fix clone.
Browse files Browse the repository at this point in the history
  • Loading branch information
iglesias committed Apr 16, 2018
1 parent 9ac2c55 commit 8da4063
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
32 changes: 22 additions & 10 deletions src/shogun/clustering/Hierarchical.cpp
Expand Up @@ -26,31 +26,42 @@ struct pair
#endif // DOXYGEN_SHOULD_SKIP_THIS

CHierarchical::CHierarchical()
: CDistanceMachine(), merges(3), dimensions(0), assignment(NULL),
table_size(0), pairs(NULL), merge_distance(NULL)
: CDistanceMachine()
{
init();
register_parameters();
}

CHierarchical::CHierarchical(int32_t merges_, CDistance* d)
: CDistanceMachine(), merges(merges_), dimensions(0), assignment(NULL),
table_size(0), pairs(NULL), merge_distance(NULL)
: CDistanceMachine()
{
init();
merges = merges_;
set_distance(d);
register_parameters();
}

static int PAIRS_DIM_0 = 2;
void CHierarchical::init()
{
merges = 3;
dimensions = 0;
assignment = NULL;
assignment_len = 0;
table_size = 0;
pairs = NULL;
pairs_len = 0;
merge_distance = NULL;
merge_distance_len = 0;
}

void CHierarchical::register_parameters()
{
watch_param("merges", &merges);
watch_param("dimensions", &dimensions);
watch_param("assignment_size", &assignment_size);
watch_param("assignment", &assignment, &table_size);
watch_param("assignment", &assignment, &assignment_len);
watch_param("table_size", &table_size);
watch_param("pairs", &pairs, &PAIRS_DIM_0, &merges);
watch_param("merge_distance", &merge_distance, &merges);
watch_param("pairs", &pairs, &pairs_len);
watch_param("merge_distance", &merge_distance, &merge_distance_len);
}

CHierarchical::~CHierarchical()
Expand Down Expand Up @@ -82,10 +93,12 @@ bool CHierarchical::train_machine(CFeatures* data)

SG_FREE(merge_distance);
merge_distance=SG_MALLOC(float64_t, num);
merge_distance_len=num;
SGVector<float64_t>::fill_vector(merge_distance, num, -1.0);

SG_FREE(assignment);
assignment=SG_MALLOC(int32_t, num);
assignment_len = num;
SGVector<int32_t>::range_fill_vector(assignment, num);

SG_FREE(pairs);
Expand Down Expand Up @@ -155,7 +168,6 @@ bool CHierarchical::train_machine(CFeatures* data)
}
pb.complete();

assignment_size=num;
table_size=l-1;
ASSERT(table_size>0)
SG_FREE(distances);
Expand Down
11 changes: 7 additions & 4 deletions src/shogun/clustering/Hierarchical.h
Expand Up @@ -122,7 +122,10 @@ class CHierarchical : public CDistanceMachine
virtual bool train_require_labels() const { return false; }

private:
/** Register all parameters */
/** Initialize attributes */
void init();

/** Register all parameters (aka this class' attributes) */
void register_parameters();

protected:
Expand All @@ -132,20 +135,20 @@ class CHierarchical : public CDistanceMachine
/// number of dimensions
int32_t dimensions;

/// size of assignment table
int32_t assignment_size;

/// cluster assignment for the num_points
int32_t* assignment;
int32_t assignment_len;

/// size of the below tables
int32_t table_size;

/// tuples of i/j
int32_t* pairs;
int32_t pairs_len;

/// distance at which pair i/j was added
float64_t* merge_distance;
int32_t merge_distance_len;
};
}
#endif
2 changes: 1 addition & 1 deletion src/shogun/metric/LMNNImpl.h
Expand Up @@ -81,7 +81,7 @@ class CLMNNImpl
static void check_training_setup(CFeatures* features, const CLabels* labels, SGMatrix<float64_t>& init_transform);

/**
* for each feature in x, find its target neighbors; this is, its k
* for each feature in x, find its target neighbors; that is, its k
* nearest neighbors with the same label as indicated by y
*/
static SGMatrix<index_t> find_target_nn(CDenseFeatures<float64_t>* x, CMulticlassLabels* y, int32_t k);
Expand Down

0 comments on commit 8da4063

Please sign in to comment.