top of page
  • Writer's pictureTim Burns

Exploring Classifiers in Machine Learning

Updated: Jul 5, 2021

Today, my ML path has me learning Decision Tree Classifiers. All the visualizations I did yesterday focused on looking at flower data and visualizing how the data mapped to Iris species.

I use a handy-dandy set_colors function to label all my work with Darkula colors in PyCharm.

def set_colors(current_axis, x_axis="X-axis", y_axis="Y-axis"):
    The set_colors is a standard setting I use to give nice colors when
    running Jupyter in Darkula mode on PyCharm.

    :param current_axis:
    :param x_axis:
    :param y_axis:
    current_axis.tick_params(axis='x', colors='peru')
    current_axis.tick_params(axis='y', colors='peru')

Assign the column names and plot historgraphs on the diagonal for each of the metrics, then compare metrics to see how visually, the different properties of each species separate one from another.

graph = sns.pairplot(train, hue="label", height = 2, palette = 'colorblind');
for ax in range(0, 4):
    for ay in range(0, 4):
        set_colors(graph.axes[ax,ay], x_axis=column_names[ax], y_axis=column_names[ay])

The Decision Tree Classifier

The Decision Tree Classifier runs iteratively through the data set and divides it into boxes that minimize Entropy and maximize Information Gain at every step.

Entropy (see the previous blog article) measures the randomness of a system. Information Gain is a term for picking a set of samples in an iteration to lose the most randomness and get the most Information.

I use Pandas to run the decision tree algorithm, and it will provide an absolute measure of how each property contributes to the classification on a scale from 0 to 1.

import matplotlib.pyplot as plt
from sklearn.tree import plot_tree

fig, axes = plt.subplots(1, 1)
plot_tree(mod_dt, feature_names = column_names, class_names = label_names, filled = True);

The visualize tool provides an excellent resource for QA on the model. Finally, after four iterations on the model, the classification finds the important metrics to determine the Iris flower species.

Run the Confusion Matrix to QA the model and determine if it misclassifies any data points.

# The confusion matrix helps to show how our model will handle new values
from sklearn.metrics import plot_confusion_matrix
fig, ax = plt.subplots(figsize=(8, 8))
ax.set_title('Decision Tree Confusion matrix, without normalization');
plot_confusion_matrix(mod_dt, X_test, y_test,

We can do better. The workhorse for the data classification is the Support Vector Machine or SVM set of algorithms. A good video describing Support Vector algorithms is here. Essentially, it boils our previous iterative problem into maximizes the distance between the classification borders.

Applying the SVM Support Vector Classifier to the data gives a better model that doesn't misclassify the row in the previous model.

from sklearn.svm import SVC

svc_model = SVC(gamma='auto'),y_train)
print("The accuracy of the SVC Tree is","{:.3f}".format(metrics.accuracy_score(prediction,y_test)))

# The confusion matrix helps to show how our model will handle new values
fig, ax = plt.subplots(figsize=(8, 8))
ax.set_title('SVC Confusion matrix, without normalization');
plot_confusion_matrix(svc_model, X_test, y_test,

Voila- all points predicted correctly.

Concluding my exploration, analyzing data with Pandas and Scikit, visualizing data helps to perform quality analysis on a data model. Many methods exist and determining the model accuracy, and finding the best model in an interactive environment leads to a foundational understanding you can use to implement ML in an industrial-scale environments like Spark or DataBricks.

17 views0 comments

Recent Posts

See All

Carto, Snowflake, and Data Management

A basic principle of data management: Don't move data unless you have to. Moving data is expensive and error-prone. Data Egress Cost: How To Take Back Control And Reduce Egress Charges Archiving to S

Music Festivals 2024

I am browsing music festivals I'd like to attend in 2024. I work for which manages vacation properties and my search came up with this site from one of our biggest competitors. Vacasa: US

bottom of page