With the kNN algorithm

The Shogun library also has an implementation of the kNN algorithm, and it is placed in the CKNN class. Before using this algorithm, we have to calculate the distance between all features in the training dataset. This operation can be done, for example, with the instance of the CEuclideanDistance class (or an alternative), which implements the CDistance interface. There are many distance implementation classes in the Shogun library, such as cosine similarity, and Manhattan and Hamming distances. After we have the object containing the distances for our training set, we can initialize the object of the CKNN class, which takes the distance object, training labels, and the k parameter (which is the number of searched-for neighbors). This object uses the train() method to perform model training. The following code shows this approach:


void KNNClassification(Some<CDenseFeatures<DataType>> features,
Some<CMulticlassLabels> labels,
Some<CDenseFeatures<DataType>> test_features,
Some<CMulticlassLabels> test_labels) {
int32_t k = 3;
auto distance = some<CEuclideanDistance>(features, features);
auto knn = some<CKNN>(k, distance, labels);
knn->train();

// evaluate model on test data
auto new_labels = wrap(knn->apply_multiclass(test_features));

// estimate accuracy
auto eval_criterium = some<CMulticlassAccuracy>();
auto accuracy = eval_criterium->evaluate(new_labels, test_labels);


// process results
auto feature_matrix = test_features->get_feature_matrix();
for (index_t i = 0; i < new_labels->get_num_labels(); ++i) {
auto label_idx_pred = new_labels->get_label(i);
...
}
}

As we can see, the code is pretty simple. After the model is trained, we can use the already known apply_multiclass() method for evaluation.

The following screenshot shows the results of applying the Shogun implementation of the kNN algorithm to our datasets:

Notice that the kNN algorithm classified all datasets almost correctly.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.195.111