Classification Of Iris Flower using Python

This is a very basic machine learning program that is may be called the “Hello World” program of machine learning. So here I am going to discuss what are the basic steps of machine learning and how to approach it. Let’s learn Classification Of Iris Flower using Python.

Basic Steps of machine learning

  1. Find a valid problem
  2. Collect data from various sources about that problem
  3. Evaluate the algorithms that you are gonna use
  4. See if there are ways to improve your result
  5. Present the results you have got

These are the fundamental steps that we follow for any machine learning process. Seems easy right?

Well, then let’s see how to write the first code in machine learning.

Python Code: Classification Of Iris Flower

from pandas import read_csv
from matplotlib import pyplot
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import StratifiedKFold
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC

url = "Url of the Site you are going to fetch data from."
names = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'class']
dataset = read_csv(url, names=names)

array = dataset.values
X = array[:,0:4]
y = array[:,4]
X_train, X_validation, Y_train, Y_validation = train_test_split(X, y, test_size=0.20, random_state=1, shuffle=True)

models = []
models.append(('LR', LogisticRegression(solver='liblinear', multi_class='ovr')))
models.append(('LDA', LinearDiscriminantAnalysis()))
models.append(('KNN', KNeighborsClassifier()))
models.append(('CART', DecisionTreeClassifier()))
models.append(('NB', GaussianNB()))
models.append(('SVM', SVC(gamma='auto')))

results = []
names = []
for name, model in models:
  kfold = StratifiedKFold(n_splits=10, random_state=1)
  cv_results = cross_val_score(model, X_train, Y_train, cv=kfold, scoring='accuracy')
  results.append(cv_results)
  names.append(name)
  print('%s: %f (%f)' % (name, cv_results.mean(), cv_results.std()))

pyplot.boxplot(results, labels=names)
pyplot.title('Algorithm Comparison')
pyplot.show()

Understanding the Code:

At first, see we are importing some libraries to the program. These are very essential for machine learning. You need these libraries time and over again. Example: Scipy, Numpy, Matplot, Scikit, etc.
Next, I am going to need the data from the website or the place where I have stored all the data about the Iris flower. After which we load the datasheet present there, which I am doing in the three-line block code.
Next, we have to summarize the datasheet. At times we can peek(see) the data we have collected. Then I have created models out of the data I have received from the datasheet.
And finally, we are plotting the collected data using pyplot. We do this after the statistical analysis I have done in the for loop for the best model.

Output:
LR: 0.950000 (0.055277) 
LDA: 0.975000 (0.038188) 
KNN: 0.958333 (0.041667) 
CART: 0.958333 (0.041667) 
NB: 0.950000 (0.055277) 
SVM: 0.983333 (0.033333)

Classification Of Iris Flower using Python

Also read: predict_proba for classification problem in Python

Leave a Reply