F_VN_CreateKnnModel
Create a k-nearest neighbors model of the specified type. The initial reference count is set to one if a new model is created and kept, otherwise. Models of this type support on-line training (sample by sample) and retraining. Predictions can only be scalar.
Syntax
Definition:
FUNCTION F_VN_CreateKnnModel : HRESULT
VAR_INPUT
ipMlModel : Reference To ITcVnMlModel;
eKnnType : ETcVnKnn;
nK : UDINT;
hrPrev : HRESULT;
END_VAR
Inputs
Name |
Type |
Description |
---|---|---|
ipMlModel |
Reference To ITcVnMlModel |
Returns the created model (Non-zero interface pointers are reused.) |
eKnnType |
k-nearest neighbors model type | |
nK |
UDINT |
Parameter k used for prediction (number of considered neighbors) |
hrPrev |
HRESULT indicating the result of previous operations (If SUCCEEDED(hrPrev) equals false, no operation is executed.) |
Further information
The function F_VN_CreateKnnModel
creates a K-Nearest Neighbor (KNN) model.
K-Nearest Neighbor models
When training K-Nearest Neighbor models, all training samples are stored 1:1 in the model. The idea of data points within a multidimensional space (corresponding to the dimensionality of the samples) helps to understand this. A number nK
of data points that are closest to the input sample (Euclidean distance) are used for prediction. These data points are called "nearest neighbors". All other data points are ignored in the prediction. The prediction result is calculated from all nearest neighbors, whereby these are weighted equally regardless of the actual distance to the input sample.
Parameter
Model
The created model is returned in the interface pointer ipMlModel
.
Model type
eKnnType
indicates whether the model is used as a classifier (TCVN_KNN_CLASSIFIER
), as anomaly detection (TCVN_KNN_NOVELTY_DETECTOR
) or as a regressor (TCVN_KNN_REGRESSOR
).
Number of neighbors used
nK
specifies how many data points are used as nearest neighbors in the prediction. For anomaly detection, nK
is irrelevant because only the nearest neighbor is required. The value 1 should therefore be used in this case.
Application
For example, a K-nearest Neighbor model for classification using 5 nearest neighbors is created like this:
hr := F_VN_CreateKnnModel(
ipMlModel := ipMlModel,
eKnnType := TCVN_KNN_CLASSIFIER,
nK := 5,
hrPrev := hr);
Required License
TC3 Vision Machine Learning
System Requirements
Development environment | Target platform | PLC libraries to include |
---|---|---|
TwinCAT V3.1.4024.54 or later | PC or CX (x64) with PL50, e.g. Intel 4-core Atom CPU | Tc3_Vision |