Skip to main content

Multiple Classification using Iris Dataset

Multiple Classification using Iris Dataset ; this dataset is come in built in sklearn library.

Here we have got three classes of Iris flower; you can read fully article using dataset['DESCR']

Although you can read comments for better understanding or you can just copy it.



# %%  Import libraries

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# %%  Load dataset

from sklearn.datasets import load_iris

dataset = load_iris()
print(dataset.keys())

# %%  Assign the dataset to features and outputs

x = dataset.data
y = dataset.target

print(x.shape , y.shape)

# %%  Data Spliting

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)

# %%  Import tensorflow keras libraries

from keras.models import Sequential
from keras.layers import Dense

# %%  Making Nueral network Architecture 

model = Sequential()  # intializing the model

# Here only one or two dense layer is enough
model.add(Dense(4, input_shape=(4,), activation='relu')) # (4,) because we need one
 row of all of four columns 
model.add(Dense(8, activation='relu')) 
# Last layer using Softmax for multiple classification
model.add(Dense(3, activation='softmax')) # dense 3 because here is three categories

# %%  Compiling and fitting model

model.compile(
   optimizer='adam',
   loss='sparse_categorical_crossentropy',
   metrics=['accuracy']

model.fit(x_train, y_train, batch_size=10, epochs=100)

# %%  Prediction

y_pred = model.predict(x_test)

y_pred_class = np.argmax(y_pred, axis=1)

# %%

from sklearn.metrics import classification_report, confusion_matrix

cr = classification_report(y_test, y_pred_class)
cm = confusion_matrix(y_test, y_pred_class)

print(cr)
print(cm)

# %%  Accuracy

Accuracy = np.round((cm[0,0] + cm[1,1] + cm[2,2])*100 / len(y_pred_class))
print(Accuracy)
# %%  Heatmap visualization

plt.imshow(cm, cmap='hot')
# %%

Comments

Popular posts from this blog

Gradient Descent with RSME

Optimization Alorithms Ex : G.D. , S.D. ,  Adam, RMS prop , momentum , adelta Gradient Descent is an  optimization algorithm that find a best fit line and local minima of a differentiable function for given training data set. S imply used to find the coefficients (weights) and intercept (bias) that minimize a cost function as far as possible.  There are three types of  g radient descent techniques:   Regular Batch GD (Gradient Descent) -  Studiously descend the curve in one path towards one minima ; every hop calculates the cost function for entire training data. If training data is large, one should not use this. Random GD (Stochastic GD) -   Calculates the Cost function for only one (randomly selected) training data per hop ; tend to jump all over the place due to randomness but due to it actually jump across minima’s.  Mini Batch gradient descent - Somewhere midway between the above 2. Does the calculation for a bunch of random data poin...

Why python ? What is Python?

Python is a generally interpreted and  interactive dynamic symmetric   high-level  object oriented programming language. It is widely used in Machine Learning today. Pretty easy to understand, learn, code and explain because it has very crisp and clear syntaxes than other languages.  Guido van Rossum made Python in 1991, named his programming language after the television show Monty Python's Flying Circus. Python has got features or derived features from ABC named programming language. Interactive - The result will be printed on the screen, immediately return, in the next line as we entered. High-level - Humans can easy to interpret; the source code contains easy-to-read syntax that is later converted into a low-level language (0 , 1) Dynamic-symmetric – Don’t need to clarify the data type. It Allows the type casting. Type Casting –  We can transform the one data type in another data type Object Oriented – language is focused on Object...