Loading...

Classification

Classifications are ML algorithms used to predict categorical response columns. For predicting more than two categories, these are called Multiclass Classifications. Examples of classification are predicting the flower species using specific characteristics or predicting whether Telco customers will churn.

To understand how to create a classification model, let’s predict the species of flowers with the Iris dataset.

We’ll start by importing the Random Forest Classifier.

from verticapy.machine_learning.vertica import RandomForestClassifier

Next, we’ll create a model object.

model = RandomForestClassifier()

Let’s use the iris dataset.

from verticapy.datasets import load_iris

iris = load_iris()

Now that the data is loaded, we can fit the model.

model.fit(iris, ["PetalLengthCm", "SepalLengthCm"], "Species")


===========
call_string
===========
SELECT rf_classifier('"public"."_verticapy_tmp_randomforestclassifier_v_mldb_4088742697be11efa8720242ac120002_"', '"public"."_verticapy_tmp_view_v_mldb_41344fc697be11efa8720242ac120002_"', 'species', '"PetalLengthCm", "SepalLengthCm"' USING PARAMETERS exclude_columns='', ntree=10, mtry=1, sampling_size=0.632, max_depth=5, max_breadth=1000000000, min_leaf_size=1, min_info_gain=0, nbins=32);

=======
details
=======
  predictor  |      type      
-------------+----------------
petallengthcm|float or numeric
sepallengthcm|float or numeric


===============
Additional Info
===============
       Name       |Value
------------------+-----
    tree_count    | 10  
rejected_row_count|  0  
accepted_row_count| 250 

We have many metrics to evaluate the model.

model.report()
...
avg_weighted
avg_micro
auc...0.9978533333333333[null]
prc_auc...0.9949743862969087[null]
accuracy...0.98320000000000010.9813333333333333
log_loss...0.019874674780651157[null]
precision...0.97269841269841260.972
recall...0.9720.972
f1_score...0.9716046213093710.972
mcc...0.95938438911232720.958
informedness...0.9550.958
markedness...0.96408613347217220.958
csi...0.94613392526822050.9455252918287937

You can add the predictions to your dataset.

model.predict(iris, name = "prediction")
123
SepalLengthCm
Numeric(5,2)
100%
...
123
SepalWidthCm
Numeric(5,2)
100%
Abc
prediction
Varchar(128)
100%
14.6...3.6Iris-setosa
24.7...3.2Iris-setosa
34.7...3.2Iris-setosa
44.8...3.0Iris-setosa
54.8...3.1Iris-setosa
64.8...3.4Iris-setosa
74.9...3.0Iris-setosa
84.9...3.1Iris-setosa
94.9...3.1Iris-setosa
104.9...3.1Iris-setosa
115.0...2.3Iris-versicolor
125.0...3.4Iris-setosa
135.1...3.5Iris-setosa
145.4...3.0Iris-versicolor
155.4...3.4Iris-setosa
165.4...3.9Iris-setosa
175.5...2.4Iris-versicolor
185.5...2.4Iris-versicolor
195.6...2.7Iris-versicolor
205.7...3.0Iris-versicolor

You can also add the probabilities.

model.predict_proba(iris, name = "prob")
123
SepalLengthCm
Numeric(5,2)
100%
...
Abc
prob_irisversicolor
Varchar(128)
100%
Abc
prob_irisvirginica
Varchar(128)
100%
14.6...00
24.7...00
34.7...00
44.8...00
54.8...00
64.8...00
74.9...00
84.9...00
94.9...00
104.9...00
115.0...10
125.0...00
135.1...00
145.4...0.9222220.0388889
155.4...00
165.4...00
175.5...10
185.5...10
195.6...10
205.7...10

Our example forgoes splitting the data into training and testing, which is important for real-world work. Our main goal in this lesson is to look at the metrics used to evaluate classifications. The most famous metric is accuracy: generally speaking, the closer accuracy is to 1, the better the model is. However, taking metrics at face value can lead to incorrect interpretations.

For example, let’s say our goal is to identify bank fraud. Fraudulent activity is relatively rare, so let’s say that they represent less than 1% of the data. If we were to predict that there are no frauds in the dataset, we’d end up with an accuracy of 99%. This is why ROC AUC and PRC AUC are more robust metrics.

That said, a good model is simply a model that might solve a the given problem. In that regard, any model is better than a random one.

In the next lesson, we’ll go over Time Series