Classification and Learning Inference Using K-Nearest Neighbor (KNN)


This article describes how to build and test a model using the K-Nearest Neighbor (KNN) algorithm in Fortanix Confidential AI.


  • A user signed in to a Fortanix Confidential AI account.

For instructions to sign up and log in, refer to our User’s guide: Sign up for Confidential AI.

Building the Model

Data Ingestion

  1. On the Data Ingestion page, click CREATE DATASET, and select CSV Dataset.
  2. Dataset name - Enter a name for your dataset. 
  3. Select the Upload a file option if you want to upload your data directly to the Fortanix Confidential AI platform.
    1. In the File Upload section, upload the file. In a CSV dataset, notice that after the file is uploaded, the headers (column names) are detected and displayed. For example: UserID, Gender, Age, and so on. The number of rows is also detected and displayed.
  4. To track what the data is used for; you can optionally add Labels in the form of “Key:Value” pairs.
  5. Click CREATE DATASET to save the data. KNN_CreateDataset.pngFigure 1: CSV details
  6. You will now see the saved dataset in the dataset table. 

Data Preparation

In this phase, you can choose the column names as a set of features and a target for the tabular dataset. you can choose multiple combinations of features and targets (collectively called Variables).

  1. In the Data Preparation page, click ADD VARIABLES to select the features and target. KNN_AddVariables.pngFigure 2: Add variables
  2. Select one or more features from the SET A FEATURE column and select one target from the SET A TARGET column.
  3. Click ADD to add the variables. KNN_AddFeatures.pngFigure 3: Select the features and target
  4. Click SAVE to save the variables and proceed to the next phase, that is, build a model. KNN_SaveVariables.pngFigure 4: Variables added

Build Model

In the build a model stage, you can select the KNN algorithm to run on the dataset defined in the previous phases, to analyze and build AI models.

In the “Build a Model” form:

  1. Select the BUILD A MODEL tab and click BUILD MODEL to build a training model for the dataset created in the previous phase. 
  2. Enter the Training flow name, that is, the name of the model.
  3. In the Training Dataset field, select the training dataset on which you want to run the KNN algorithm and build a trained model.
  4. In the Algorithm field, select the k-Nearest Neighbors algorithm.
  5. Select ML variables that you created in the Data Preparation phase.
  6. Enter the Neighbor count which is the number of nearest neighbors (k) to use in the kNN algorithm.
  7. In the Model name field, enter the name of the output dataset. This is the output model that will be used in the data inference phase.
  8. Click BUILD MODEL to run the selected algorithm on the training data and build the model for inference. KNN_BuildModel.pngFigure 5: Build a model
  9. After the training model is built, you will see the model created under the Training flows. To run the training model, click the RUN button below the model.
    KNN_RunModel.pngFigure 6: Run training model
  10. If the model was executed successfully, you would see the status of the execution under the Execution Log. Click the Execution log link to view the log details. KNN_RunModelSuccess.pngFigure 7: Model training success
  11. Click the download report icon to download the execution log report. KNN_BuildExecutionLog.pngFigure 8: Execution log
  12. After the execution is completed successfully, the model is now trained and ready for inference where it will be passed through a machine learning model for output data prediction.

Data Inference

In this stage, the data is passed through a machine learning model to identify and predict the output from the data.

  1. In the INFERENCE tab, click BUILD INFERENCE to predict the data output.
  2. In the Build Inference form, enter the Inference flow name, that is, the name of the inference model.
  3. In the Select model section, select TRAINED, and select the trained model that was built in the “build a model” stage from the drop-down.
  4. In the Select input dataset field, select the input dataset you created in the first stage that you want to pass through a machine learning model.
  5. In the Select inference application section, select the prediction algorithm.
  6. Select the ML variables.
  7. In the Output Configuration field, enter the name of the output dataset that will contain the predicted output.
  8. The Encrypt Dataset option is selected by default to generate an encryption key and add an extra layer of protection to the output data. Copy or download the key to decrypt the output data for viewing.
  9. Click CREATE INFERENCE FLOW to pass the data through a machine learning model and predict the output. KNN_CreateInference1.pngKNN_CreateInference2.png
    Figure 9: Build inference
  10. The inference is successfully created. Click RUN below the inference workflow to run the model and predict the output. KNN_RunInference.pngFigure 10: Run inference
  11. If the model was executed successfully, you would see the status of the execution under the Execution Log. Click the Execution Log link to view the log details. KNN_InferenceSuccess.pngFigure 11: Inference success
  12. After the execution is completed successfully, the output is now predicted and ready to be viewed. To view the output, click the DOWNLOAD button. KNN_DownloadInference.pngFigure 12: Download output
  13. In the DOWNLOAD dialog box, enter the Encryption key to decrypt the output. CAI_DecryptionKey.pngFigure 13: Decrypt output
  14. A *.tar.gz file is generated on your local machine. Extract the contents of the file. A snapshot of the output appears as shown below.
    Figure 14: Sample Output


Please sign in to leave a comment.

Was this article helpful?
0 out of 0 found this helpful