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

Classification & Confusion Matrix & Accuracy Paradox

Classification  work on voting the object belongs from which classes has more probability  There are two types of classification : Binary classification : There are two classes we have ex: male-female , cat-dog , yes-not  Multiple classification :   There are classes more than two we have ex: traffic signs , face recognition , flower race  , Digit Recognition Confusion matrix :  Confusion matrix is one type of technique to evaluate the model accuracy for classification problem. In this technique we consider how many of positive and negative data points we predict correctly. The main consideration terms are accuracy, precision and recall The accuracy was an appealing matric, because it was a single number. Here precision and recall(sensitivity) are two numbers. So to get the final score (accuracy) of our model we use F1 score, so that we have a single number. Here is the F1 score's mathematical formula: F1 = 2x precision x recall / (precision ...

Python program to check if variable is of integer or string

Let's say if you want to input something of any datatype and want to get datatype only of it. So... Whenever you input some data whether it is string, integer or float like this: i = input('enter something here: ') means without int, str or float put before the syntax, that time your given input is always consider as string  or if you make it like this to add int before syntax; i = int(input('enter something here: '))  it always consider as integer and gives value error when you input string and same thing happens with float, So here is a program to solve this problem of input and get datatype var = input('input to check if variable is of integer or string: ') if var.isdigit() == False:     print(type(var)) else:     var1 = int(var)     print(type(var1))

Multiple classification from many of directories

  # %%  Import nessacary libraries import  numpy  as  np import  pandas  as  pd import  cv2 import  matplotlib.pyplot  as  plt import  os import  glob # %%   Keras Tensorflow libraries from  keras  import  layers from  keras.models  import  Model from  keras.optimizers  import  RMSprop , Adam , Nadam from  keras.preprocessing.image  import  ImageDataGenerator from  keras.layers  import  Input, BatchNormalization, Dense, Dropout, Conv2D, Flatten, GlobalAveragePooling2D, LeakyReLU from  keras.preprocessing.image  import  ImageDataGenerator, img_to_array, load_img # %%  Path path  =   r 'G:/Machine Learning/Project/Lego Mnifigures Classification/dataset' open_dir  =  os....