0% found this document useful (0 votes)
9 views9 pages

DL Experiment 2

The document outlines an experiment using an artificial neural network (ANN) on the IRIS dataset, detailing the data preprocessing, model architecture, and training process. It includes steps for data loading, encoding species labels, splitting the dataset, and defining a sequential model with multiple dense layers. The model is compiled, trained, and evaluated, achieving a test accuracy of 1.0.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views9 pages

DL Experiment 2

The document outlines an experiment using an artificial neural network (ANN) on the IRIS dataset, detailing the data preprocessing, model architecture, and training process. It includes steps for data loading, encoding species labels, splitting the dataset, and defining a sequential model with multiple dense layers. The model is compiled, trained, and evaluated, achieving a test accuracy of 1.0.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Experiment - 2

ANN_on_IRIS_Dataset

import pandas as pd

from google.colab import drive


drive.mount('/content/drive')

Df = pd.read_csv("/content/drive/MyDrive/Iris.csv")
df

output:-
df["Species"] = df["Species"].replace("Iris-setosa", 0)
df["Species"] = df["Species"].replace("Iris-versicolor", 1)
df["Species"] = df["Species"].replace("Iris-virginica", 2)

print(df.tail())

output:-

df.shape
output:- (150,6)

X=df.values[:,1:5]
X
output:-
Y = df.values[:,5]
Y

Output:-

from sklearn.model_selection import train_test_split


from sklearn.metrics import accuracy_score

X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size =


0.25, random_state = 100)
print(y_train.shape)
(112,)

import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import RMSprop
from tensorflow.keras.optimizers import Adam

first_layer_size = 32
num_classes=3
model = Sequential()
model.add(Dense(first_layer_size, activation='sigmoid', input_shape=(4,
)))
model.add(Dense(32, activation='sigmoid'))
model.add(Dense(32, activation='sigmoid'))
model.add(Dense(num_classes, activation='softmax'))
model.summary()
Output:-

Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense (Dense) (None, 32) 160

dense_1 (Dense) (None, 32) 1056

dense_2 (Dense) (None, 32) 1056

dense_3 (Dense) (None, 3) 99

=================================================================
Total params: 2371 (9.26 KB)
Trainable params: 2371 (9.26 KB)
Non-trainable params: 0 (0.00 Byte)

print(y_test)
y_train = keras.utils.to_categorical(y_train, num_classes ,dtype ="float")
y_test = keras.utils.to_categorical(y_test, num_classes, dtype ="float")
y_test
Output:-

model.compile(loss='categorical_crossentropy',
optimizer=RMSprop(),
metrics=['accuracy'])
import numpy as np
X_train = np.asarray(X_train).astype('float32')
X_test = np.asarray(X_test).astype('float32')
history = model.fit(X_train,y_train,
batch_size=10,
epochs=100,
verbose=1)
Output:-
………….

model.compile(loss='mean_squared_error',
optimizer='Adam',
metrics=['accuracy'])
import numpy as np
X_train = np.asarray(X_train).astype('float32')
X_test = np.asarray(X_test).astype('float32')
history = model.fit(X_train,y_train,
batch_size=10,
epochs=100,
verbose=1)

Output:-

Write the testing input and output variables


score = model.evaluate(X_test, y_test, verbose=1)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

output:-2/2 [==============================] - 0s 14ms/step - loss:


0.0233 - accuracy: 1.0000
Test loss: 0.023274414241313934
Test accuracy: 1.0
prediction = model.predict(X_test[1:25])

print('Prediction\n',prediction)
print('\nThresholded output\n',(prediction > 0.5).astype(int))
#prediction_new = keras.utils.to_categorical(prediction, num_classes,
dtype ="float")
#print(prediction_new)

Output:-
# check that it has worked right or not
#print(X_test[23:25])
print(y_test[1:25])

Output:-

You might also like