Skip to content

select_feature_set_knnSelectFeatureSetKnnSelectFeatureSetKnnselect_feature_set_knnT_select_feature_set_knn🔗

Short description🔗

select_feature_set_knnSelectFeatureSetKnnSelectFeatureSetKnnselect_feature_set_knnT_select_feature_set_knn — Selects an optimal subset from a set of features to solve a certain classification problem.

Signature🔗

select_feature_set_knn( class_train_data ClassTrainDataHandle, string SelectionMethod, string GenParamName, number GenParamValue, out class_knn KNNHandle, out string SelectedFeatureIndices, out real Score )void SelectFeatureSetKnn( const HTuple& ClassTrainDataHandle, const HTuple& SelectionMethod, const HTuple& GenParamName, const HTuple& GenParamValue, HTuple* KNNHandle, HTuple* SelectedFeatureIndices, HTuple* Score )static void HOperatorSet.SelectFeatureSetKnn( HTuple classTrainDataHandle, HTuple selectionMethod, HTuple genParamName, HTuple genParamValue, out HTuple KNNHandle, out HTuple selectedFeatureIndices, out HTuple score )def select_feature_set_knn( class_train_data_handle: HHandle, selection_method: str, gen_param_name: MaybeSequence[str], gen_param_value: MaybeSequence[Union[int, str, float]] ) -> Tuple[HHandle, Sequence[str], Sequence[float]]

Herror T_select_feature_set_knn( const Htuple ClassTrainDataHandle, const Htuple SelectionMethod, const Htuple GenParamName, const Htuple GenParamValue, Htuple* KNNHandle, Htuple* SelectedFeatureIndices, Htuple* Score )

HTuple HClassKnn::SelectFeatureSetKnn( const HClassTrainData& ClassTrainDataHandle, const HString& SelectionMethod, const HTuple& GenParamName, const HTuple& GenParamValue, HTuple* Score )

HTuple HClassKnn::SelectFeatureSetKnn( const HClassTrainData& ClassTrainDataHandle, const HString& SelectionMethod, const HString& GenParamName, double GenParamValue, HTuple* Score )

HTuple HClassKnn::SelectFeatureSetKnn( const HClassTrainData& ClassTrainDataHandle, const char* SelectionMethod, const char* GenParamName, double GenParamValue, HTuple* Score )

HTuple HClassKnn::SelectFeatureSetKnn( const HClassTrainData& ClassTrainDataHandle, const wchar_t* SelectionMethod, const wchar_t* GenParamName, double GenParamValue, HTuple* Score ) (Windows only)

HClassKnn HClassTrainData::SelectFeatureSetKnn( const HString& SelectionMethod, const HTuple& GenParamName, const HTuple& GenParamValue, HTuple* SelectedFeatureIndices, HTuple* Score ) const

HClassKnn HClassTrainData::SelectFeatureSetKnn( const HString& SelectionMethod, const HString& GenParamName, double GenParamValue, HTuple* SelectedFeatureIndices, HTuple* Score ) const

HClassKnn HClassTrainData::SelectFeatureSetKnn( const char* SelectionMethod, const char* GenParamName, double GenParamValue, HTuple* SelectedFeatureIndices, HTuple* Score ) const

HClassKnn HClassTrainData::SelectFeatureSetKnn( const wchar_t* SelectionMethod, const wchar_t* GenParamName, double GenParamValue, HTuple* SelectedFeatureIndices, HTuple* Score ) const (Windows only)

HTuple HClassKnn.SelectFeatureSetKnn( HClassTrainData classTrainDataHandle, string selectionMethod, HTuple genParamName, HTuple genParamValue, out HTuple score )

HTuple HClassKnn.SelectFeatureSetKnn( HClassTrainData classTrainDataHandle, string selectionMethod, string genParamName, double genParamValue, out HTuple score )

HClassKnn HClassTrainData.SelectFeatureSetKnn( string selectionMethod, HTuple genParamName, HTuple genParamValue, out HTuple selectedFeatureIndices, out HTuple score )

HClassKnn HClassTrainData.SelectFeatureSetKnn( string selectionMethod, string genParamName, double genParamValue, out HTuple selectedFeatureIndices, out HTuple score )

Description🔗

select_feature_set_knnSelectFeatureSetKnn selects an optimal subset from a set of features to solve a certain classification problem. The classification problem has to be specified with annotated training data in ClassTrainDataHandleclassTrainDataHandleclass_train_data_handle and will be classified by a a k-nearest neighbors classifier. Details of the properties of this classifier can be found in create_class_knnCreateClassKnn.

The result of the operator is a trained classifier that is returned in KNNHandleKNNHandleknnhandle. Additionally, the list of indices or names of the selected features is returned in SelectedFeatureIndicesselectedFeatureIndicesselected_feature_indices. To use this classifier, calculate for new input data all features mentioned in SelectedFeatureIndicesselectedFeatureIndicesselected_feature_indices and pass them to the classifier.

A possible application of this operator can be a comparison of different parameter sets for certain feature extraction techniques. Another application is to search for a property that is discriminating between different classes of parts or classes of errors.

To define the features that should be selected from ClassTrainDataHandleclassTrainDataHandleclass_train_data_handle, the dimensions of the feature vectors in ClassTrainDataHandleclassTrainDataHandleclass_train_data_handle can be grouped into subfeatures by calling set_feature_lengths_class_train_dataSetFeatureLengthsClassTrainData. A subfeature can contain several subsequent elements of a feature vector. The operator decides for each of these subfeatures, if it is better to use it for the classification or leave it out.

The indices of the selected subfeatures are returned in SelectedFeatureIndicesselectedFeatureIndicesselected_feature_indices. If names were set in set_feature_lengths_class_train_dataSetFeatureLengthsClassTrainData, these names are returned instead of the indices. If set_feature_lengths_class_train_dataSetFeatureLengthsClassTrainData was not called for ClassTrainDataHandleclassTrainDataHandleclass_train_data_handle before, each element of the feature vector is considered as a subfeature.

The selection method SelectionMethodselectionMethodselection_method is either a greedy search 'greedy'"greedy" (iteratively add the feature with highest gain) or the dynamically oscillating search 'greedy_oscillating'"greedy_oscillating" (add the feature with highest gain and test then if any of the already added features can be left out without great loss). The method 'greedy'"greedy" is generally preferable, since it is faster. Only in cases when the subfeatures are low-dimensional or redundant, the method 'greedy_oscillating'"greedy_oscillating" should be chosen.

The optimization criterion is the classification rate of a two-fold cross-validation of the training data. The best achieved value is returned in Scorescorescore.

The k-NN classifier can be parameterized using the following values in GenParamNamegenParamNamegen_param_name and GenParamValuegenParamValuegen_param_value:

  • 'num_neighbors'"num_neighbors": The number of minimally evaluated nodes, increase this value for high dimensional data.

    Suggested values: '1'"1", '2'"2", '5'"5", '10'"10"

    Default: '1'"1"

  • 'num_trees'"num_trees": Number of search trees in the k-NN classifier

    Suggested values: '1'"1", '4'"4", '10'"10"

    Default: '4'"4"

Attention🔗

This operator may take considerable time, depending on the size of the data set in the training file, and the number of features.

Please note, that this operator should not be called, if only a small set of training data is available. Due to the risk of overfitting the operator select_feature_set_knnSelectFeatureSetKnn may deliver a classifier with a very high score. However, the classifier may perform poorly when tested.

Execution information🔗

Execution information
  • Multithreading type: reentrant (runs in parallel with non-exclusive operators).

  • Multithreading scope: global (may be called from any thread).

  • Automatically parallelized on internal data level.

This operator returns a handle. Note that the state of an instance of this handle type may be changed by specific operators even though the handle is used as an input parameter by those operators.

Parameters🔗

ClassTrainDataHandleclassTrainDataHandleclass_train_data_handle (input_control) class_train_data → (handle)HTuple (HHandle)HClassTrainData, HTuple (IntPtr)HHandleHtuple (handle)

Handle of the training data.

SelectionMethodselectionMethodselection_method (input_control) string → (string)HTuple (HString)HTuple (string)strHtuple (char*)

Method to perform the selection.

Default: 'greedy'"greedy"
List of values: 'greedy', 'greedy_oscillating'"greedy", "greedy_oscillating"

GenParamNamegenParamNamegen_param_name (input_control) string(-array) → (string)HTuple (HString)HTuple (string)MaybeSequence[str]Htuple (char*)

Names of generic parameters to configure the selection process and the classifier.

Default: [][]
List of values: 'num_neighbors', 'num_trees'"num_neighbors", "num_trees"

GenParamValuegenParamValuegen_param_value (input_control) number(-array) → (real / integer / string)HTuple (double / Hlong / HString)HTuple (double / int / long / string)MaybeSequence[Union[int, str, float]]Htuple (double / Hlong / char*)

Values of generic parameters to configure the selection process and the classifier.

Default: [][]
Suggested values: 1, 2, 31, 2, 3

KNNHandleKNNHandleknnhandle (output_control) class_knn → (handle)HTuple (HHandle)HClassKnn, HTuple (IntPtr)HHandleHtuple (handle)

A trained k-NN classifier using only the selected features.

SelectedFeatureIndicesselectedFeatureIndicesselected_feature_indices (output_control) string-array → (string)HTuple (HString)HTuple (string)Sequence[str]Htuple (char*)

The selected feature set, contains indices or names.

Scorescorescore (output_control) real-array → (real)HTuple (double)HTuple (double)Sequence[float]Htuple (double)

The achieved score using two-fold cross-validation.

Example🔗

(HDevelop)

* Find out which of the two features distinguishes two Classes
NameFeature1 := 'Good Feature'
NameFeature2 := 'Bad Feature'
LengthFeature1 := 3
LengthFeature2 := 2
* Create training data
create_class_train_data (LengthFeature1+LengthFeature2,\
  ClassTrainDataHandle)
* Define the features which are in the training data
set_feature_lengths_class_train_data (ClassTrainDataHandle, [LengthFeature1,\
  LengthFeature2], [NameFeature1, NameFeature2])
* Add training data
*                                                         |Feat1| |Feat2|
add_sample_class_train_data (ClassTrainDataHandle, 'row', [1,1,1,  2,1  ], 0)
add_sample_class_train_data (ClassTrainDataHandle, 'row', [2,2,2,  2,1  ], 1)
add_sample_class_train_data (ClassTrainDataHandle, 'row', [1,1,1,  3,4  ], 0)
add_sample_class_train_data (ClassTrainDataHandle, 'row', [2,2,2,  3,4  ], 1)
add_sample_class_train_data (ClassTrainDataHandle, 'row', [0,0,1,  5,6  ], 0)
add_sample_class_train_data (ClassTrainDataHandle, 'row', [2,3,2,  5,6  ], 1)
* Add more data
* ...
* Select the better feature with the k-NN classifier
select_feature_set_knn (ClassTrainDataHandle, 'greedy', [], [], KNNHandle,\
  SelectedFeatureKNN, Score)
* Use the classifier
* ...

Result🔗

If the parameters are valid, the operator select_feature_set_knnSelectFeatureSetKnn returns the value 2 (H_MSG_TRUE). If necessary, an exception is raised.

Combinations with other operators🔗

Combinations

Possible predecessors

create_class_train_dataCreateClassTrainData, add_sample_class_train_dataAddSampleClassTrainData, set_feature_lengths_class_train_dataSetFeatureLengthsClassTrainData

Possible successors

classify_class_knnClassifyClassKnn

Alternatives

select_feature_set_mlpSelectFeatureSetMlp, select_feature_set_svmSelectFeatureSetSvm, select_feature_set_gmmSelectFeatureSetGmm

See also

select_feature_set_trainf_knnSelectFeatureSetTrainfKnn, gray_featuresGrayFeatures, region_featuresRegionFeatures

Module🔗

Foundation