[go: up one dir, main page]

0% found this document useful (0 votes)
64 views7 pages

SC Assignment Q2

This document summarizes the steps taken to create a multilayer perceptron model to classify iris species using the Iris dataset. It loads and explores the data, normalizes the features, splits the data into train and test sets, and defines the neural network model with Keras. Key steps include preprocessing the data, one-hot encoding the labels, defining the network architecture with dense and activation layers, and preparing the data for training and evaluation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views7 pages

SC Assignment Q2

This document summarizes the steps taken to create a multilayer perceptron model to classify iris species using the Iris dataset. It loads and explores the data, normalizes the features, splits the data into train and test sets, and defines the neural network model with Keras. Key steps include preprocessing the data, one-hot encoding the labels, defining the network architecture with dense and activation layers, and preparing the data for training and evaluation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

A multilayer perceptrons to predict the

species of the Iris dataset


In [1]:
#Import required libraries

import keras #library for neural network

import pandas as pd #loading data in table form

import seaborn as sns #visualisation

import matplotlib.pyplot as plt #visualisation

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

from sklearn.preprocessing import normalize #machine learning algorithm library

In [2]:
#Reading data

data=pd.read_csv('Iris.csv', index_col=False)

print("\nDescribing the data: ")

data.describe()

Describing the data:

Out[2]: Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm

count 150.000000 150.000000 150.000000 150.000000 150.000000

mean 75.500000 5.843333 3.054000 3.758667 1.198667

std 43.445368 0.828066 0.433594 1.764420 0.763161

min 1.000000 4.300000 2.000000 1.000000 0.100000

25% 38.250000 5.100000 2.800000 1.600000 0.300000

50% 75.500000 5.800000 3.000000 4.350000 1.300000

75% 112.750000 6.400000 3.300000 5.100000 1.800000

max 150.000000 7.900000 4.400000 6.900000 2.500000

In [3]:
print("\nInfo of the data:\n")

data.info()

Info of the data:

<class 'pandas.core.frame.DataFrame'>

RangeIndex: 150 entries, 0 to 149

Data columns (total 6 columns):

# Column Non-Null Count Dtype

--- ------ -------------- -----

0 Id 150 non-null int64

1 SepalLengthCm 150 non-null float64

2 SepalWidthCm 150 non-null float64

3 PetalLengthCm 150 non-null float64

4 PetalWidthCm 150 non-null float64

5 Species 150 non-null object

dtypes: float64(4), int64(1), object(1)

memory usage: 7.2+ KB

In [4]:
print("\n10 first samples of the dataset:")

data.head(10)

10 first samples of the dataset:

Out[4]: Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species

0 1 5.1 3.5 1.4 0.2 Iris-setosa

1 2 4.9 3.0 1.4 0.2 Iris-setosa

2 3 4.7 3.2 1.3 0.2 Iris-setosa

3 4 4.6 3.1 1.5 0.2 Iris-setosa

4 5 5.0 3.6 1.4 0.2 Iris-setosa

5 6 5.4 3.9 1.7 0.4 Iris-setosa

6 7 4.6 3.4 1.4 0.3 Iris-setosa

7 8 5.0 3.4 1.5 0.2 Iris-setosa

8 9 4.4 2.9 1.4 0.2 Iris-setosa

9 10 4.9 3.1 1.5 0.1 Iris-setosa

In [5]:
print("\n10 last samples of the dataset:")

data.tail(10)

10 last samples of the dataset:

Out[5]: Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species

140 141 6.7 3.1 5.6 2.4 Iris-virginica

141 142 6.9 3.1 5.1 2.3 Iris-virginica

142 143 5.8 2.7 5.1 1.9 Iris-virginica

143 144 6.8 3.2 5.9 2.3 Iris-virginica

144 145 6.7 3.3 5.7 2.5 Iris-virginica

145 146 6.7 3.0 5.2 2.3 Iris-virginica

146 147 6.3 2.5 5.0 1.9 Iris-virginica

147 148 6.5 3.0 5.2 2.0 Iris-virginica

148 149 6.2 3.4 5.4 2.3 Iris-virginica

149 150 5.9 3.0 5.1 1.8 Iris-virginica

In [6]:
sns.lmplot(x ='SepalLengthCm', y ='SepalWidthCm',

data=data,

fit_reg=False,

hue="Species",

scatter_kws={"marker": "D",

"s": 50})
plt.title('SepalLength vs SepalWidth')

sns.lmplot(x='PetalLengthCm', y='PetalWidthCm',

data=data,

fit_reg=False,

hue="Species",

scatter_kws={"marker": "D",

"s": 50})
plt.title('PetalLength vs PetalWidth')

sns.lmplot(x='SepalLengthCm', y='PetalLengthCm',

data=data,

fit_reg=False,

hue="Species",

scatter_kws={"marker": "D",

"s": 50})
plt.title('SepalLength vs PetalLength')

sns.lmplot(x='SepalWidthCm', y='PetalWidthCm',

data=data,

fit_reg=False,

hue="Species",

scatter_kws={"marker": "D",

"s": 50},)

plt.title('SepalWidth vs PetalWidth')

plt.show()

In [7]:
print(data["Species"].unique())

['Iris-setosa' 'Iris-versicolor' 'Iris-virginica']

In [8]:
data.loc[data["Species"]=="Iris-setosa","Species"]=0

data.loc[data["Species"]=="Iris-versicolor","Species"]=1

data.loc[data["Species"]=="Iris-virginica","Species"]=2

data.head()

Out[8]: Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species

0 1 5.1 3.5 1.4 0.2 0

1 2 4.9 3.0 1.4 0.2 0

2 3 4.7 3.2 1.3 0.2 0


Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species

3 4 4.6 3.1 1.5 0.2 0

4 5 5.0 3.6 1.4 0.2 0

In [9]:
data=data.iloc[np.random.permutation(len(data))]

print(data.head())

Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species

10 11 5.4 3.7 1.5 0.2 0

46 47 5.1 3.8 1.6 0.2 0

31 32 5.4 3.4 1.5 0.4 0

107 108 7.3 2.9 6.3 1.8 2

94 95 5.6 2.7 4.2 1.3 1

In [10]:
X=data.iloc[:,1:5].values

y=data.iloc[:,5].values

print("Shape of X",X.shape)

print("Shape of y",y.shape)

print("Examples of X\n",X[:3])

print("Examples of y\n",y[:3])

Shape of X (150, 4)

Shape of y (150,)

Examples of X

[[5.4 3.7 1.5 0.2]

[5.1 3.8 1.6 0.2]

[5.4 3.4 1.5 0.4]]

Examples of y

[0 0 0]

In [11]:
X_normalized=normalize(X,axis=0)

print("Examples of X_normalised\n",X_normalized[:3])

Examples of X_normalised

[[0.07471338 0.09794497 0.02951407 0.01150299]

[0.07056264 0.10059213 0.03148167 0.01150299]

[0.07471338 0.09000348 0.02951407 0.02300599]]

In [12]:
#Creating train,test and validation data

'''

80% -- train data

20% -- test data

'''

total_length=len(data)

train_length=int(0.8*total_length)

test_length=int(0.2*total_length)

X_train=X_normalized[:train_length]

X_test=X_normalized[train_length:]

y_train=y[:train_length]

y_test=y[train_length:]

print("Length of train set x:",X_train.shape[0],"y:",y_train.shape[0])

print("Length of test set x:",X_test.shape[0],"y:",y_test.shape[0])

Length of train set x: 120 y: 120

Length of test set x: 30 y: 30

In [13]: #Neural network module

from keras.models import Sequential

from keras.layers import Dense,Activation,Dropout

# from keras.layers.normalization import BatchNormalization

from keras.utils import np_utils

In [14]:
#Change the label to one hot vector

'''

[0]--->[1 0 0]

[1]--->[0 1 0]

[2]--->[0 0 1]

'''

y_train=np_utils.to_categorical(y_train,num_classes=3)

y_test=np_utils.to_categorical(y_test,num_classes=3)

print("Shape of y_train",y_train.shape)

print("Shape of y_test",y_test.shape)

Shape of y_train (120, 3)

Shape of y_test (30, 3)

In [15]:
model=Sequential()

model.add(Dense(1000,input_dim=4,activation='relu'))

model.add(Dense(500,activation='relu'))

model.add(Dense(300,activation='relu'))

model.add(Dropout(0.2))

model.add(Dense(3,activation='softmax'))
model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])

In [16]:
model.summary()

Model: "sequential"

_________________________________________________________________

Layer (type) Output Shape Param #

=================================================================

dense (Dense) (None, 1000) 5000

_________________________________________________________________

dense_1 (Dense) (None, 500) 500500

_________________________________________________________________

dense_2 (Dense) (None, 300) 150300

_________________________________________________________________

dropout (Dropout) (None, 300) 0

_________________________________________________________________

dense_3 (Dense) (None, 3) 903

=================================================================

Total params: 656,703

Trainable params: 656,703

Non-trainable params: 0

_________________________________________________________________

In [19]:
model.fit(X_train,y_train,validation_data=(X_test,y_test),batch_size=20,epochs=10,ve

Epoch 1/10

6/6 [==============================] - 0s 14ms/step - loss: 0.1523 - accuracy: 0.933


3 - val_loss: 0.1340 - val_accuracy: 0.9667

Epoch 2/10

6/6 [==============================] - 0s 8ms/step - loss: 0.1271 - accuracy: 0.9500


- val_loss: 0.0884 - val_accuracy: 0.9667

Epoch 3/10

6/6 [==============================] - 0s 11ms/step - loss: 0.1857 - accuracy: 0.916


7 - val_loss: 0.0578 - val_accuracy: 1.0000

Epoch 4/10

6/6 [==============================] - 0s 10ms/step - loss: 0.1698 - accuracy: 0.900


0 - val_loss: 0.1535 - val_accuracy: 0.9333

Epoch 5/10

6/6 [==============================] - 0s 11ms/step - loss: 0.1500 - accuracy: 0.925


0 - val_loss: 0.1398 - val_accuracy: 0.9333

Epoch 6/10

6/6 [==============================] - 0s 9ms/step - loss: 0.1403 - accuracy: 0.9500


- val_loss: 0.2163 - val_accuracy: 0.8333

Epoch 7/10

6/6 [==============================] - 0s 10ms/step - loss: 0.1326 - accuracy: 0.941


7 - val_loss: 0.1073 - val_accuracy: 0.9333

Epoch 8/10

6/6 [==============================] - 0s 8ms/step - loss: 0.1698 - accuracy: 0.9250


- val_loss: 0.0924 - val_accuracy: 1.0000

Epoch 9/10

6/6 [==============================] - 0s 10ms/step - loss: 0.0910 - accuracy: 0.975


0 - val_loss: 0.0471 - val_accuracy: 1.0000

Epoch 10/10

6/6 [==============================] - 0s 10ms/step - loss: 0.1190 - accuracy: 0.950


0 - val_loss: 0.0408 - val_accuracy: 1.0000

<keras.callbacks.History at 0x1f4bfc61bb0>
Out[19]:

In [20]:
prediction=model.predict(X_test)

length=len(prediction)

y_label=np.argmax(y_test,axis=1)

predict_label=np.argmax(prediction,axis=1)

accuracy=np.sum(y_label==predict_label)/length * 100

print("Accuracy of the dataset",accuracy )

Accuracy of the dataset 100.0

An accuracy of 100% is achieved in this dataset.It can be asserted that for each
epoch, the neural network is trying to learn from its existing feature and
predict it by its weights and biases. For each epoch, the weights and biases
and changed by subtracting its rate to get a better accuracy each time.
In [ ]:

You might also like