Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ishwor2048/e1a774946e272bec0ab870989df26c8c to your computer and use it in GitHub Desktop.
Save ishwor2048/e1a774946e272bec0ab870989df26c8c to your computer and use it in GitHub Desktop.
# Thanks to Jose Portilla on Udemy for giving me amazing idea for this model
# Performing keras basics based on Iris dataset
# First of all, importing basic libraries, numpy and pandas
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import keras
# Importing the Iris dataset
from sklearn.datasets import load_iris
# Initializing dataset
iris = load_iris()
# Checking the type of dataset
type(iris)
# checking the description of the dataset
print(iris.DESCR)
# Grabbing features and labels
X = iris.data
y = iris.target
from keras.utils import to_categorical
y = to_categorical(y)
y.shape
# Importing train_test_split module
from sklearn.model_selection import train_test_split
# Splitting the dataset into training and test set with the help of train_test_split module of sklearn.
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
# Importing MinMaxScaler from scikit-learn and initiating it
from sklearn.preprocessing import MinMaxScaler
scaler_object = MinMaxScaler()
# Fitting the scaler object to training data to scale the dataset into same level
scaler_object.fit(X_train)
# splitting the train and test dataset after scaling and transforming
scaled_X_train = scaler_object.transform(X_train)
scaled_X_test = scaler_object.transform(X_test)
# Importing sequential and dense from keras
from keras.models import Sequential
from keras.layers import Dense
model = Sequential()
model.add(Dense(8,input_dim=4, activation='relu'))
model.add(Dense(8,input_dim=4, activation='relu'))
model.add(Dense(3, activation='softmax'))
model.compile(loss = 'categorical_crossentropy', optimizer='adam', metrics = ['accuracy'])
# Checking the summary of the model
model.summary()
# Fitting the model with 150 epochs
model.fit(scaled_X_train, y_train, epochs=150, verbose=2)
# Need to scale new incoming data
model.predict_classes(scaled_X_test)
predictions = model.predict_classes(scaled_X_test)
y_test.argmax(axis=1)
# Importing confusion matrix, classification report and the accuracy score module from keras
from sklearn.metrics import confusion_matrix, classification_report, accuracy_score
# Printing confusion matrix
confusion_matrix(y_test.argmax(axis=1), predictions)
# Printing classification report
print(classification_report(y_test.argmax(axis=1), predictions))
# accuracy score
accuracy_score(y_test.argmax(axis=1), predictions)
# Saving the model in the format of h5 file in order to run in the future of deploy it
model.save('ishwor_basic_keras_model_on_Iris_Dataset.h5')
# If I want to run the model again
my_new_model = load_model('ishwor_basic_keras_model_on_Iris_Dataset.h5')
# If I want to make some predictions into trained model above, let's say
model.predict_classes(scaled_X_test)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment