Single-Table Tutorial with the Scikit-learn API
On this tutorial we'll a classifier on a single table dataset.
The Iris Dataset
We'll train a classifier for the Iris dataset. This is a classical dataset containing data of different plants belonging to the genus Iris. It contains 150 records, 50 for each of the three Iris's variants: Setosa, Virginica and Versicolor. Each record contains the length and the width of both the petal and the sepal of the plant. The standard task, when using this dataset, is to construct a classifier for the type of the Iris, based on the petal and sepal characteristics.
To train a classifier with Khiops, we only need a dataframe containing the Iris data:
# Method 1: Load data directly from GitHub (recommended for quick tests or small datasets)
url = "https://raw.githubusercontent.com/KhiopsML/khiops-samples/11.0.0/Iris/Iris.txt"
iris_df = pd.read_csv(url, delimiter='\t')
# Method 2: Load data locally after downloading all Khiops samples (best for offline use)
# from khiops.tools import download_datasets
# download_datasets()
# iris_path = f"{kh.get_samples_dir()}/Iris/Iris.txt"
# iris_df = pd.read_csv(iris_path, sep="\t")
# Display the first 10 records from the dataset
iris_df[:10]
| SepalLength | SepalWidth | PetalLength | PetalWidth | Class | |
|---|---|---|---|---|---|
| 0 | 5.1 | 3.5 | 1.4 | 0.2 | Iris-setosa |
| 1 | 4.9 | 3.0 | 1.4 | 0.2 | Iris-setosa |
| 2 | 4.7 | 3.2 | 1.3 | 0.2 | Iris-setosa |
| 3 | 4.6 | 3.1 | 1.5 | 0.2 | Iris-setosa |
| 4 | 5.0 | 3.6 | 1.4 | 0.2 | Iris-setosa |
| 5 | 5.4 | 3.9 | 1.7 | 0.4 | Iris-setosa |
| 6 | 4.6 | 3.4 | 1.4 | 0.3 | Iris-setosa |
| 7 | 5.0 | 3.4 | 1.5 | 0.2 | Iris-setosa |
| 8 | 4.4 | 2.9 | 1.4 | 0.2 | Iris-setosa |
| 9 | 4.9 | 3.1 | 1.5 | 0.1 | Iris-setosa |
Training the Classifier
Before training the classifier, we split the data into the feature matrix (sepal length, width, etc) and the target vector containing the labels (the Class column).
Then we can construct our final train/test dataset
Let's check the contents of the feature matrix and the target vector:
| SepalLength | SepalWidth | PetalLength | PetalWidth | |
|---|---|---|---|---|
| 16 | 5.4 | 3.9 | 1.3 | 0.4 |
| 82 | 5.8 | 2.7 | 3.9 | 1.2 |
| 60 | 5.0 | 2.0 | 3.5 | 1.0 |
| 35 | 5.0 | 3.2 | 1.2 | 0.2 |
| 143 | 6.8 | 3.2 | 5.9 | 2.3 |
| ... | ... | ... | ... | ... |
| 17 | 5.1 | 3.5 | 1.4 | 0.3 |
| 98 | 5.1 | 2.5 | 3.0 | 1.1 |
| 66 | 5.6 | 3.0 | 4.5 | 1.5 |
| 126 | 6.2 | 2.8 | 4.8 | 1.8 |
| 109 | 7.2 | 3.6 | 6.1 | 2.5 |
112 rows × 4 columns
We are ready to train the KhiopsClassifier: We use the fit method on the training data. After its execution, the KhiopsClassifier instance is ready to classify new Iris plants:
KhiopsClassifier()In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
KhiopsClassifier()
Displaying the Classifiers’ Training Accuracy and AUC
The fit method calculates evaluation metrics on the training dataset. We access them via the estimator's attribute model_report_ which is an instance of the AnalysisResults class. Let's check this out:
This object train_performance is of class PredictorPerformance and has accuracy and auc attributes:
Iris train accuracy: 0.964286
Iris train AUC : 0.99755
The PredictorPerformance object has also a confusion matrix attribute:
| Iris-setosa | Iris-versicolor | Iris-virginica | |
|---|---|---|---|
| Iris-setosa | 34 | 0 | 0 |
| Iris-versicolor | 0 | 41 | 3 |
| Iris-virginica | 0 | 1 | 33 |
If you have installed the Khiops Visualization app you may explore the full learning report by executing the code below.
Deploying the Classifier and Displaying Its Test Performance
Now that we have a fitted KhiopsClassifier, we are now going to deploy it on the test split.
This can be done in two different ways:
- to predict a class that can be obtained using its
predict. - to predict class probabilities that can be obtained using its
predict_proba.
Let's first predict the Iris labels:
Classes:
array(['Iris-setosa', 'Iris-versicolor', 'Iris-virginica'], dtype='<U15')
Predictions (first 10 values):
array(['Iris-versicolor', 'Iris-virginica', 'Iris-virginica',
'Iris-versicolor', 'Iris-setosa', 'Iris-virginica',
'Iris-versicolor', 'Iris-setosa', 'Iris-setosa', 'Iris-versicolor'],
dtype=object)
Probabilities (first 10 rows):
array([[0.00152548, 0.80072032, 0.19775421],
[0.00150198, 0.02233054, 0.97616748],
[0.00150198, 0.02233054, 0.97616748],
[0.00170069, 0.96904427, 0.02925505],
[0.99593086, 0.00241699, 0.00165216],
[0.00252304, 0.41073845, 0.5867385 ],
[0.00170069, 0.96904427, 0.02925505],
[0.99593086, 0.00241699, 0.00165216],
[0.99593086, 0.00241699, 0.00165216],
[0.00170069, 0.96904427, 0.02925505]])
From these predictions we compute the test accuracy and AUC (One-vs-Rest) scores using sklearn.metrics
Iris test accuracy: 0.9473684210526315
Iris test AUC : 1.0