Decision Tree Analysis and K-Means Clustering Using the Iris Data Set.

Contents

This article was published as part of the Data Science Blogathon

Introduction:

As we all know, Artificial Intelligence is being widely used around us: from reading the news on your mobile device or analyzing that complex data at your workplace, AI has improved speed, precision and effectiveness of human effort. Advances in AI have helped us achieve things we previously thought were not possible. Even having a pizza from your favorite restaurant at home is just a click away, thanks to AI.

In a nutshell, artificial intelligence means a computer or computer program that mimics human intelligence. It is achieved by learning how you think, learn, decides and works the human brain while solving a problem. The results of this study are then used as the basis for developing intelligent systems and software..

There is 4 types of learning:

Supervised learning.

Unsupervised learning.

Semi-supervised learning.

Reinforced learning.

Supervised Unsupervised Semi-supervised Reinforced
Supervised learning is when the model is trained on a labeled data set. The Unsupervised learning is when the model is trained on an unlabeled dataset, it is up to the algorithm to find underlying patterns in the data. It falls between supervised and unsupervised learning, in this, some learning data is labeled and some is not. The algorithm evaluates its performance based on feedback responses and reacts accordingly.

This blog covers supervised and unsupervised AI learning, using Python and Iris dataset.

Table of Contents:

  1. Introduction
  2. Iris data set
  3. Supervised learning
  4. Decision tree
  5. Logistic regression
  6. Unsupervised learning
  7. Grouping of K-stockings
  8. Conclusion and references

Iris data set:

The data set contains 3 classes with 50 instances each and 150 total instances, where each class refers to a type of iris plant.

Class: Silky Iris, Iris Versicolor, Iris Virginica

The data format: (sepal length, sepal width, petal length, petal width)

20733iris-3539434

We will train our models based on these parameters and we will use them to predict the kinds of flowers.

Understanding the data:

Download the Iris dataset from https://www.kaggle.com/uciml/iris

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
iris =   pd.read_csv("Iris.csv") #Iris.csv is now a pandas dataframe
print(iris.head()) #prints first 5 values
print(iris.describe()) #prints some basic statistical details like percentile,mean, std etc. of the data frame
16566iris-head_-2786086
iris.head ()
42140iris-describe-3459342
iris.describe ()

Visualizing the data using matplotlib:

iris.plot(kind="scatter", x="SepalLengthCm",   y ="SepalWidthCm")
plt.show()
75894scatter_plot-6136253
Scatter plot

Data visualization using andrew's curves from pandas:

Andrews curves have the functional form:

f

x_4 without (2t) + x_5 cos (2t) +…

Where the coefficients x correspond to the values of each dimension y t is linearly spaced between -pi and + pi. Each row of the frame corresponds to a single curve.

from pandas.plotting import andrews_curves
andrews_curves(iris.drop("Id", axis=1), "Species")
plt.show()
74761andrews20curves-3533825
Andrews curve graph

Dataset pre-processing:

Using a built-in library called 'train_test_split', which divides our data set into a proportion of 80:20. The 80% will be used to training, evaluation and selection between our models and the 20% will be retained as a validation dataset.

from sklearn.model_selection import train_test_split
x = iris.iloc[:, :-1].values #last column values excluded
y = iris.iloc[:,   -1].values #last column value
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test = train_test_split(x,Y,test_size=0.2,random_state=0)  #Splitting the dataset into the Training set and Test set

Supervised learning :

Supervised machine learning algorithms are trained to find patterns using a data set. The process is simple, takes what has been learned in the past and then applies it to new data. Supervised learning uses labeled examples to predict future patterns and events.

For instance, when we teach a child that 2 + 2 = 4 or we point to the image of any animal so that you know its name.

Supervised learning is in turn divided into:

Classification: Classification predicts categorical class labels, that are discreet and messy. It is a two step process, consisting of a learning step and a classification step. There are various classification algorithms like: “Decision tree classifier”, “Random forest”, “Naive Bayes classifier”, etc.

Regression: Regression is generally described as determining a relationship between two or more variables, how to predict a person's work based on input data X. Some of the regression algorithms are: “Logistic regression”, “Loop regression”, “Ridge regression”, etc. .

21093capture-5016285
supervised learning example

Decision tree classifier:

The general reason for using a decision tree is to create a training model that can be used to predict the class or value of the target variables by learning decision rules inferred from previous data. (training data).

Try to solve the problem using tree representation. Every node The tree's inner node corresponds to an attribute, and each leaf node corresponds to a class label.

Using the decision tree in the Iris dataset:

from sklearn.tree import   DecisionTreeClassifier
from sklearn.metrics import accuracy_score
classifier   = DecisionTreeClassifier()
classifier.fit(x_train,   y_train) #training the classifier
y_pred   = classifier.predict(x_test) #making precdictions
print(classification_report(y_test,   y_pred)) #Summary of the predictions made by the   classifier
print(confusion_matrix(y_test, y_pred)) #to evaluate the quality of the output
print('accuracy   is',accuracy_score(y_pred,y_test)) #Accuracy   score

Precision: accuracy of positive predictions.

Recovery: fraction of positives that were correctly identified.

F1 score: What percentage of positive predictions were correct?

macro avg – averaging the unweighted mean per label

weighted average: averaging the weighted average of support per label

Heat map for the confusion matrix:

import seaborn as sns
cm  = confusion_matrix(y_test, y_pred) #Transform to df
cm_df = pd.DataFrame(cm,index = ['setosa','versicolor','virginica'], columns = ['setosa','versicolor','virginica'])
plt.figure(figsize=(5.5,4))
sns.heatmap(cm_df,   annot=True)
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.show()
65665heatmap-1821488
Confusion matrix heat map

One idea we can get from the matrix is ​​that the model was very accurate when classifying Setosa and Virginica (True positive / All = 1.0). But nevertheless, Versicolor's accuracy was lower (13/14 = 0,928).

Unsupervised learning:

Unsupervised learning is used against data without historical labels. The system is not subject to a predetermined set of outputs, correlations between inputs and outputs or a “correct answer”. The algorithm must figure out what it is seeing for itself, since it doesn't have any waypoint storage. The goal is to explore the data and find some kind of patterns or structures.

Unsupervised learning can be classified into:

Grouping: Clustering is the task of dividing the population or data points into multiple groups, so that the data points of one group are homogeneous with each other than those of different groups. There are numerous grouping algorithms, some of them are: “K-means clustering algorithms”, “medium change”, “hierarchical grouping”, etc.

Association: An association rule is an unsupervised learning method used to find the relationships between variables in a large database. Determine the set of elements that occur together in the data set.

98361unsuper-8669081
example of unsupervised learning

Grouping of K-stockings:

The goal of the K-means clustering algorithm is to find clusters in the data, with the number of groups represented by the variable K. The algorithm works iteratively to assign each data point to one of the K groups based on the characteristics that are provided.. .

The results of running a K-means on a data set are:

Centroides K: centroids for each of the K groups identified in the data set.

Labels for training data: full data set labeled to ensure that each data point is mapped to one of the clusters.

Using K-means clustering on the Iris dataset:

from sklearn.datasets import load_iris
from sklearn.cluster import KMeans
iris_data=load_iris()   #loading iris dataset from sklearn.datasets
iris_df = pd.DataFrame(iris_data.data, columns = iris_data.feature_names) #creating dataframe
kmeans = KMeans(n_clusters=3,init="k-means++",   max_iter = 100, n_init = 10, random_state = 0) #Applying Kmeans classifier
y_kmeans = kmeans.fit_predict(x)
print(kmeans.cluster_centers_) #display cluster centers
plt.scatter(x[y_kmeans == 0, 0], x[y_kmeans == 0, 1],s = 100, c="red", label="Iris-silky")
plt.scatter(x[y_kmeans == 1, 0], x[y_kmeans == 1, 1],s = 100, c="blue", label="Iris versicolor")
plt.scatter(x[y_kmeans == 2, 0], x[y_kmeans == 2, 1],s = 100, c="green", label="Iris-virginica")   #Visualising the clusters - On the first two columns
plt.scatter(kmeans.cluster_centers_[:,   0], kmeans.cluster_centers_[:,1],s = 100, c="black", label="Centroids")   #plotting the centroids of the clusters
plt.legend()
plt.show()
11191kmc-2567246
K-means clustering scatter plot

An idea that we can get from the Dispersion diagram is that the accuracy of the model to determine Setosa and Virginica is comparatively more to Versicolour.

Conclution:

We have explored and pre-processed the Iris dataset using sklearn. data set, as well as use the Iris.csv file. What's more, I learned about supervised and unsupervised learning and implemented the decision tree algorithm and the grouping by K-means.

References:

https://www.kaggle.com/sixteenpython/machine-learning-with-iris-dataset

https://towardsdatascience.com/exploring-classifiers-with-python-scikit-learn-iris-dataset-2bcb490d2e1b

https://scikit-learn.org/stable/

https://certes.co.uk/types-of-artificial-intelligence-a-detailed-guide/

About the Author:

Hello reader, i'm yashi saxena, and I currently work at TCS as a systems engineer. TO THE, ML and NLP have always been my interest, so here I am making an effort to learn more about this field. You can connect with me on Linkedin: https://www.linkedin.com/in/yashi-saxena-7a9522194/

The media shown in this article is not the property of DataPeaker and is used at the author's discretion.

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.

Datapeaker