Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 22

KLE Technological University

MACHINE LEARNING HANDS-ON PROGRAMS

Program 1: Linear Regression – Single Variable Linear Regression

Description About Dataset :

Performing the Linear regression for Single variable by using Salary_Data.csv which
is consists of two features that is Salary, YearsExperience.Each column contains 30 rows of
information. Feature “Salary” describes Each person salary according to his/her year of
experience.

//Python code to perform Single variable linear regression


from google.colab import drive
drive.mount('/content/drive')

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

"""**Importing the dataset**"""

dataset=pd.read_csv('/content/drive/My Drive/Machine Learning/Chapter1/Salary_Data.csv')


X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, -1].values

print(y)
print(X)

"""**Splitting the dataset into the Training set and Test set**"""

from sklearn.model_selection import train_test_split


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 1/3, random_state = 0)

"""**Training the Simple Linear Regression model on the Training set**"""

from sklearn.linear_model import LinearRegression


regressor = LinearRegression()
regressor.fit(X_train, y_train)

y_pred = regressor.predict(X_test)
print(y_pred)

Dept. of MCA
KLE Technological University

plt.scatter(X_train, y_train, color = 'red')


plt.plot(X_train, regressor.predict(X_train), color = 'blue')
plt.title('Salary vs Experience (Training set)')
plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.show()

"""**Visualising the Test set results**"""

plt.scatter(X_test, y_test, color = 'red')


plt.plot(X_train, regressor.predict(X_train), color = 'blue')
plt.title('Salary vs Experience (Test set)')
plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.show()

Dept. of MCA
KLE Technological University

Program 2: Linear Regression – Multi Variable Linear Regression


Description About Dataset:
Performing the linear regression using multi linear regression on 50_Startups.csv which is consist
of 5 features that is R&D, Marketing, Spend, Administration, Spend, State, Profit.
Each column contains 50 rows of information.

from google.colab import drive


drive.mount('/content/drive')
# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Importing the dataset
dataset = pd.read_csv('/content/drive/My Drive/Machine Learning/Chapter1/MachineLearning-
master/Multiple Linear Regression Code  Dataset-20230331.zip (Unzipped
Files)/50_Startups.csv')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, -1].values
# Encoding categorical data
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
ct = ColumnTransformer(transformers=[('encoder', OneHotEncoder(), [3])],
remainder='passthrough')
X = np.array(ct.fit_transform(X))
print(X)
#Splitting the dataset into the Training set and Test set

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)
# Training the Multiple Linear Regression model on the Training set
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train, y_train)
# Predicting the Test set results
y_pred = regressor.predict(X_test)
np.set_printoptions(precision=2)
print(np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1))

Dept. of MCA
KLE Technological University

Dept. of MCA
KLE Technological University

Program 3: Classification – Logistic Regression

Description About Dataset:

Performing the Logistic Regression on Social_Network_Ads. csv which is consists of three


features that is Age, EstimatedSalary, purchased. Each column contains 400 rows of information.
Feature “Age” describes Each person age and according to his/ Estimated Salary . depending on
which he/she will decide to purchase a particular item or not.

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from google.colab import drive
drive.mount('/content/drive')
# Importing the dataset
dataset = pd.read_csv('/content/drive/My Drive/Machine Learning/Chapter1/MachineLearning-
master/Social_Network_Ads.csv')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, -1].values
# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)
print(X_train)
print(y_train)
print(X_test)
print(y_test)
# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
print(X_train)
print(X_test)
# Training the Logistic Regression model on the Training set
from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression(random_state=0)
classifier.fit(X_train, y_train)

#Predict the result for Age = 30 and EstimatedSalary = 87000


print(classifier.predict(sc.transform([[30, 87000]])))

#Predicting the Test Set results


y_pred = classifier.predict(X_test)
print(y_pred)
print(np.concatenate((y_pred.reshape(len(y_pred),1),
                      y_test.reshape(len(y_test),1)),1))

Dept. of MCA
KLE Technological University

# Making the Confusion Matrix


from sklearn.metrics import confusion_matrix, accuracy_score
cm = confusion_matrix(y_test, y_pred)
print(cm)
accuracy_score(y_test, y_pred)
# Visualising the Training set results
from matplotlib.colors import ListedColormap
X_set, y_set = sc.inverse_transform(X_train), y_train
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 10, stop = X_set[:, 0].max() + 10, step
= 0.25),
 np.arange(start = X_set[:, 1].min() - 1000, stop = X_set[:, 1].max() + 1000, step = 0.25))
plt.contourf(X1, X2, classifier.predict(sc.transform(np.array([X1.ravel(),
X2.ravel()]).T)).reshape(X1.shape),
             alpha = 0.75, cmap = ListedColormap(('red', 'green')))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
    plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1], c = ListedColormap(('red', 'green'))(i),
label = j)
plt.title('Logistic Regression (Training set)')
plt.xlabel('Age')
plt.ylabel('Estimated Salary')
plt.legend()
plt.show()

# Visualising the Test set results


from matplotlib.colors import ListedColormap
X_set, y_set = sc.inverse_transform(X_test), y_test
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 10, stop = X_set[:, 0].max() + 10, step
= 0.25),
                     np.arange(start = X_set[:, 1].min() - 1000, stop = X_set[:, 1].max() + 1000, step =
0.25))
plt.contourf(X1, X2, classifier.predict(sc.transform(np.array([X1.ravel(),
X2.ravel()]).T)).reshape(X1.shape),
             alpha = 0.75, cmap = ListedColormap(('red', 'green')))

Dept. of MCA
KLE Technological University

plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
    plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1], c = ListedColormap(('red', 'green'))(i),
label = j)
plt.title('Logistic Regression (Test set)')
plt.xlabel('Age')
plt.ylabel('Estimated Salary')
plt.legend()
plt.show()

Dept. of MCA
KLE Technological University

Program 4: Classification – Support Vector Machines (SVM)

Description About Dataset:


Performing the Support Vector Machines on Social_Network_Ads. csv which is consists of three
features that is Age, EstimatedSalary, purchased. Each column contains 400 rows of information.
Feature “Age” describes Each person age and according to his/ Estimated Salary . depending on
which he/she will decide to purchase a particular item or not.

from google.colab import drive


drive.mount('/content/drive')

# Support Vector Machine (SVM)

# Importing the libraries


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

# Importing the dataset


dataset = pd.read_csv('/content/drive/My Drive/Machine Learning/Chapter1/MachineLearning-
master/Social_Network_Ads.csv')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, -1].values

# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)
print(X_train)
print(y_train)
print(X_test)
print(y_test)

# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
print(X_train)
print(X_test)

# Training the SVM model on the Training set


from sklearn.svm import SVC
classifier = SVC(kernel = 'linear', random_state = 0)
classifier.fit(X_train, y_train)

# Predicting a new result

Dept. of MCA
KLE Technological University

print(classifier.predict(sc.transform([[30,87000]])))

# Predicting the Test set results


y_pred = classifier.predict(X_test)
print(np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1))

# Making the Confusion Matrix


from sklearn.metrics import confusion_matrix, accuracy_score
cm = confusion_matrix(y_test, y_pred)
print(cm)
accuracy_score(y_test, y_pred)

# Visualising the Training set results


from matplotlib.colors import ListedColormap
X_set, y_set = sc.inverse_transform(X_train), y_train
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 10, stop = X_set[:, 0].max() + 10, step
= 0.25),
                     np.arange(start = X_set[:, 1].min() - 1000, stop = X_set[:, 1].max() + 1000, step =
0.25))
plt.contourf(X1, X2, classifier.predict(sc.transform(np.array([X1.ravel(),
X2.ravel()]).T)).reshape(X1.shape),
             alpha = 0.75, cmap = ListedColormap(('red', 'green')))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
    plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1], c = ListedColormap(('red', 'green'))(i),
label = j)
plt.title('SVM (Training set)')
plt.xlabel('Age')
plt.ylabel('Estimated Salary')
plt.legend()
plt.show()

# Visualising the Test set results

Dept. of MCA
KLE Technological University

from matplotlib.colors import ListedColormap


X_set, y_set = sc.inverse_transform(X_test), y_test
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 10, stop = X_set[:, 0].max() + 10, step
= 0.25),
                     np.arange(start = X_set[:, 1].min() - 1000, stop = X_set[:, 1].max() + 1000, step =
0.25))
plt.contourf(X1, X2, classifier.predict(sc.transform(np.array([X1.ravel(),
X2.ravel()]).T)).reshape(X1.shape),
             alpha = 0.75, cmap = ListedColormap(('red', 'green')))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
    plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1], c = ListedColormap(('red', 'green'))(i),
label = j)
plt.title('SVM (Test set)')
plt.xlabel('Age')
plt.ylabel('Estimated Salary')
plt.legend()
plt.show()

Dept. of MCA
KLE Technological University

# Training the SVM model on the Training set


from sklearn.svm import SVC
classifier = SVC(kernel = 'rbf', random_state = 0)
classifier.fit(X_train, y_train)

# Predicting the Test set results


y_pred = classifier.predict(X_test)
print(np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1))

# Visualising the Training set results


from matplotlib.colors import ListedColormap
X_set, y_set = sc.inverse_transform(X_train), y_train
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 10, stop = X_set[:, 0].max() + 10, step
= 0.25),
                     np.arange(start = X_set[:, 1].min() - 1000, stop = X_set[:, 1].max() + 1000, step =
0.25))
plt.contourf(X1, X2, classifier.predict(sc.transform(np.array([X1.ravel(),
X2.ravel()]).T)).reshape(X1.shape),
             alpha = 0.75, cmap = ListedColormap(('red', 'green')))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
    plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1], c = ListedColormap(('red', 'green'))(i),
label = j)
plt.title('SVM (Training set)')
plt.xlabel('Age')
plt.ylabel('Estimated Salary')
plt.legend()
plt.show()

Dept. of MCA
KLE Technological University

# Visualising the Test set results


from matplotlib.colors import ListedColormap
X_set, y_set = sc.inverse_transform(X_test), y_test
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 10, stop = X_set[:, 0].max() + 10, step
= 0.25),
                     np.arange(start = X_set[:, 1].min() - 1000, stop = X_set[:, 1].max() + 1000, step =
0.25))
plt.contourf(X1, X2, classifier.predict(sc.transform(np.array([X1.ravel(),
X2.ravel()]).T)).reshape(X1.shape),
             alpha = 0.75, cmap = ListedColormap(('red', 'green')))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
    plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1], c = ListedColormap(('red', 'green'))(i),
label = j)
plt.title('SVM (Test set)')
plt.xlabel('Age')
plt.ylabel('Estimated Salary')
plt.legend()
plt.show()

Dept. of MCA
KLE Technological University

Dept. of MCA
KLE Technological University

Program 5: Classification using Neural Networks

Description About Dataset:


Performing the Classification using Neural Networks on pima-indians-diabetes.csv which is
consists of nine features that is X1,X2,X3,X4,X5,X6,X7,X8,X9 . Each column contains 767 rows
of information.

from google.colab import drive


drive.mount('/content/drive')
# first neural network with keras tutorial
from numpy import loadtxt
from keras.models import Sequential
from keras.layers import Dense
# load the dataset
dataset = loadtxt('/content/drive/My Drive/pima-indians-diabetes.csv', delimiter=',')
# split into input (X) and output (y) variables
X = dataset[:,0:8]
y = dataset[:,8]
dataset.shape
(768, 9)
X.shape
(768, 8)
y.shape
(768,)
# define the keras model
model = Sequential() #calling default constructor

#First Hidden Layer (along with input layer)


model.add(Dense(12, input_dim=8, activation='relu'))  #adding input layer along with the 1st
hidden layer
#1st hidden layer has 12 activation units, input layer has 8 units for 8 features
#activation function used in the 1st layer is ReLU

#Adding the 2nd Hidden layer with 8 activation nodes and ReLU function
model.add(Dense(12, activation='relu'))

#Adding output layer with 1 activation unit (binary classification) and with Sigmoid function
model.add(Dense(1, activation='sigmoid'))
# compile the keras model
#Loss means computing the error or cost function (using algorithm - binary_crossentropy)
#Algorithm used is 'adam' - Stochastic gradient descent algorithm
#Evaluating the performance of the model will be done using 'accuracy'

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


# fit the keras model on the dataset
# Batch size is for updating the weights. Weights are updated after every 10 rows are executed.
model.fit(X, y, epochs=200, batch_size=40)

Dept. of MCA
KLE Technological University

Epoch 1/200
77/77 [==============================] - 0s 1ms/step - loss: 0.5050 - accuracy: 0.7591
Epoch 2/200
77/77 [==============================] - 0s 963us/step - loss: 0.5148 - accuracy:
0.7435
Epoch 3/200
77/77 [==============================] - 0s 1ms/step - loss: 0.5155 - accuracy: 0.7396
Epoch 4/200
77/77 [==============================] - 0s 1ms/step - loss: 0.5197 - accuracy: 0.7565
Epoch 5/200
77/77 [==============================] - 0s 1ms/step - loss: 0.5281 - accuracy: 0.7578
Epoch 6/200
77/77 [==============================] - 0s 1ms/step - loss: 0.5223 - accuracy: 0.7526
Epoch 7/200
77/77 [==============================] - 0s 1ms/step - loss: 0.5092 - accuracy: 0.7513
Epoch 8/200
77/77 [==============================] - 0s 1ms/step - loss: 0.5072 - accuracy: 0.7617
Epoch 9/200
77/77 [==============================] - 0s 1ms/step - loss: 0.5241 - accuracy: 0.7552
Epoch 10/200
77/77 [==============================] - 0s 1ms/step - loss: 0.5311 - accuracy: 0.7292
Epoch 11/200
77/77 [==============================] - 0s 1ms/step - loss: 0.5194 - accuracy: 0.7539
Epoch 12/200
77/77 [==============================] - 0s 1ms/step - loss: 0.5436 - accuracy: 0.7292
Epoch 13/200
...
Epoch 199/200
77/77 [==============================] - 0s 1ms/step - loss: 0.4924 - accuracy: 0.7383
Epoch 200/200
77/77 [==============================] - 0s 1ms/step - loss: 0.4948 - accuracy: 0.7617

24/24 [==============================] - 0s 986us/step - loss: 0.4918 - accuracy:


0.7656
Accuracy: 76.56
# evaluate the keras model
_, accuracy = model.evaluate(X, y)
print('Accuracy: %.2f' % (accuracy*100))

# make probability predictions with the model


predictions = model.predict(X)
# round predictions
rounded = [round(x[0]) for x in predictions]
# make class predictions with the model
predictions = model.predict_classes(X)
# make class predictions with the model

Dept. of MCA
KLE Technological University

predictions = model.predict_classes(X)
# summarize the first 5 cases
for i in range(5):
    print('%s => %d (expected %d)' % (X[i].tolist(), predictions[i], y[i]))
Drive already mounted at /content/drive; to attempt to forcibly remount, call
drive.mount("/content/drive", force_remount=True).
(768, 9)
(768, 8)
(768,)
Output exceeds the size limit. Open the full output data in a text editor
Epoch 1/200
77/77 [==============================] - 0s 1ms/step - loss: 0.5050 - accuracy: 0.7591
Epoch 2/200
77/77 [==============================] - 0s 963us/step - loss: 0.5148 - accuracy:
0.7435
Epoch 3/200
77/77 [==============================] - 0s 1ms/step - loss: 0.5155 - accuracy: 0.7396
Epoch 4/200
77/77 [==============================] - 0s 1ms/step - loss: 0.5197 - accuracy: 0.7565
Epoch 5/200
77/77 [==============================] - 0s 1ms/step - loss: 0.5281 - accuracy: 0.7578
Epoch 6/200
77/77 [==============================] - 0s 1ms/step - loss: 0.5223 - accuracy: 0.7526
Epoch 7/200
77/77 [==============================] - 0s 1ms/step - loss: 0.5092 - accuracy: 0.7513
Epoch 8/200
77/77 [==============================] - 0s 1ms/step - loss: 0.5072 - accuracy: 0.7617
Epoch 9/200
77/77 [==============================] - 0s 1ms/step - loss: 0.5241 - accuracy: 0.7552
Epoch 10/200
77/77 [==============================] - 0s 1ms/step - loss: 0.5311 - accuracy: 0.7292
Epoch 11/200
77/77 [==============================] - 0s 1ms/step - loss: 0.5194 - accuracy: 0.7539
Epoch 12/200
77/77 [==============================] - 0s 1ms/step - loss: 0.5436 - accuracy: 0.7292
Epoch 13/200
...
Epoch 199/200
77/77 [==============================] - 0s 1ms/step - loss: 0.4924 - accuracy: 0.7383
Epoch 200/200
77/77 [==============================] - 0s 1ms/step - loss: 0.4948 - accuracy: 0.7617
<tensorflow.python.keras.callbacks.History at 0x7f04e7d2ffd0>
24/24 [==============================] - 0s 986us/step - loss: 0.4918 - accuracy:
0.7656
Accuracy: 76.56
# make probability predictions with the model
predictions = model.predict(X)

Dept. of MCA
KLE Technological University

# round predictions
rounded = [round(x[0]) for x in predictions]
# make class predictions with the model
predictions = model.predict_classes(X)
# make class predictions with the model
predictions = model.predict_classes(X)
# summarize the first 5 cases
for i in range(5):
print('%s => %d (expected %d)' % (X[i].tolist(), predictions[i], y[i]))

[6.0, 148.0, 72.0, 35.0, 0.0, 33.6, 0.627, 50.0] => 0 (expected 1)
[1.0, 85.0, 66.0, 29.0, 0.0, 26.6, 0.351, 31.0] => 0 (expected 0)
[8.0, 183.0, 64.0, 0.0, 0.0, 23.3, 0.672, 32.0] => 1 (expected 1)
[1.0, 89.0, 66.0, 23.0, 94.0, 28.1, 0.167, 21.0] => 0 (expected 0)
[0.0, 137.0, 40.0, 35.0, 168.0, 43.1, 2.288, 33.0] => 1 (expected 1)

Dept. of MCA
KLE Technological University

Program 6: Unsupervised Learning – K-Means Clustering

Description About Dataset:


Performing the K-Means Clustering which is the technique of unsupervised learning by using
Mall_Customers.csv which is consists of four e features that is CustomerID, Genre, Age, Annual
Income, Spending Score (1-100). Each column contains 200 rows of information. Features used
to describes whether person will purchase a particular item or not based on persons age, genre
and Annual Income.

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

from google.colab import drive


drive.mount('/content/drive')
dataset = pd.read_csv('/content/drive/My Drive/Machine Learning/Chapter1/MachineLearning-
master/Mall_Customers.csv')
X = dataset.iloc[:, [3, 4]].values
# y = dataset.iloc[:, 3].values
# Using the elbow method to find the optimal number of clusters
from sklearn.cluster import KMeans
wcss = []
for i in range(1, 11):
    kmeans = KMeans(n_clusters = i, init = 'k-means++', max_iter = 300, n_init = 10,
random_state = 42)
    kmeans.fit(X)
    wcss.append(kmeans.inertia_)
plt.plot(range(1, 11), wcss)
plt.title('The Elbow Method')
plt.xlabel('Number of clusters')
plt.ylabel('WCSS')
plt.show()

Dept. of MCA
KLE Technological University

# Fitting K-Means to the dataset


kmeans = KMeans(n_clusters = 5, init = 'k-means++', max_iter = 300, n_init = 10, random_state
= 42)
y_kmeans = kmeans.fit_predict(X)
# Visualising the clusters
plt.scatter(X[y_kmeans == 0, 0], X[y_kmeans == 0, 1], s = 100, c = 'red', label = 'Cluster 1')
plt.scatter(X[y_kmeans == 1, 0], X[y_kmeans == 1, 1], s = 100, c = 'blue', label = 'Cluster 2')
plt.scatter(X[y_kmeans == 2, 0], X[y_kmeans == 2, 1], s = 100, c = 'green', label = 'Cluster 3')
plt.scatter(X[y_kmeans == 3, 0], X[y_kmeans == 3, 1], s = 100, c = 'cyan', label = 'Cluster 4')
plt.scatter(X[y_kmeans == 4, 0], X[y_kmeans == 4, 1], s = 100, c = 'magenta', label = 'Cluster 5')
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], s = 300, c = 'yellow', label
= 'Centroids')
plt.title('Clusters of customers')
plt.xlabel('Annual Income (k$)')
plt.ylabel('Spending Score (1-100)')
plt.legend()
plt.show()

Dept. of MCA
KLE Technological University

Program 7: Convolution Neural Networks Application


Description About Dataset:( Red Chilies and Green Chilies)
Performing the Convolution Neural Networks Application on image dataset to predict between
Red Chilies and Green Chilies. Training_set contain the images of both the types of Chilies (Red
and Green Chilies). One folder containing image dataset of both the Chilies which is divided as
test and training dataset. Training_set folder contains Red Chilies (500) images and Green Chilies
(500) images and Test image dataset contains Red Chilies (60), Green Chilies (60) images.

# -*- coding: utf-8 -*-


"""Green_RedML.ipynb

Automatically generated by Colaboratory.

Original file is located at


https://colab.research.google.com/drive/1ArmwpdkxSVY7yYrrpFagEYAiwdI-FMlS
"""

from google.colab import drive


drive.mount('/content/drive')

import tensorflow as tf
from keras.preprocessing.image import ImageDataGenerator

## Part 1 - Data Preprocessing


### Preprocessing the Training set

train_datagen = ImageDataGenerator(rescale = 1./255,


shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)

### Preprocessing the Test set

training_set = train_datagen.flow_from_directory('/content/drive/My Drive/training_set',


target_size = (64, 64),
batch_size = 32,
class_mode = 'binary')

test_datagen = ImageDataGenerator(rescale = 1./255)


test_set = test_datagen.flow_from_directory('/content/drive/My Drive/test_set',
target_size = (64, 64),
batch_size = 32,
class_mode = 'binary')

## Part 2 - Building the CNN

Dept. of MCA
KLE Technological University

### Initialising the CNN

cnn = tf.keras.models.Sequential()

### Step 1 – Convolution

cnn.add(tf.keras.layers.Conv2D(filters=32, kernel_size=3, activation='relu', input_shape=[64, 64,


3]))

### Step 2 - Pooling

cnn.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2))

### Adding a second convolutional layer

cnn.add(tf.keras.layers.Conv2D(filters=32, kernel_size=3, activation='relu'))


cnn.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2))

### Step 3 – Flattening

cnn.add(tf.keras.layers.Flatten())

### Step 4 - Full Connection

cnn.add(tf.keras.layers.Dense(units=128, activation='relu'))

### Step 5 - Output Layer

cnn.add(tf.keras.layers.Dense(units=1, activation='sigmoid'))

## Part 3 - Training the CNN


### Compiling the CNN

cnn.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

### Training the CNN on the Training set and evaluating it on the Test set

cnn.fit(x = training_set, validation_data = test_set, epochs = 10)

Epoch 1/10
32/32 [==============================] - 121s 4s/step - loss: 0.0192 - accuracy: 0.9950
- val_loss: 0.0086 - val_accuracy: 1.0000
Epoch 2/10
32/32 [==============================] - 110s 3s/step - loss: 0.0150 - accuracy: 0.9960
- val_loss: 0.0021 - val_accuracy: 1.0000
Epoch 3/10

Dept. of MCA
KLE Technological University

32/32 [==============================] - 109s 3s/step - loss: 0.0227 - accuracy: 0.9900


- val_loss: 0.0013 - val_accuracy: 1.0000
Epoch 4/10
32/32 [==============================] - 107s 3s/step - loss: 0.0078 - accuracy: 0.9980
- val_loss: 0.0103 - val_accuracy: 0.9917
Epoch 5/10
32/32 [==============================] - 110s 3s/step - loss: 0.0079 - accuracy: 0.9960
- val_loss: 0.0103 - val_accuracy: 0.9917
Epoch 6/10
32/32 [==============================] - 110s 3s/step - loss: 0.0065 - accuracy: 0.9980
- val_loss: 0.0214 - val_accuracy: 0.9917
Epoch 7/10
32/32 [==============================] - 110s 3s/step - loss: 0.0040 - accuracy: 1.0000
- val_loss: 0.0082 - val_accuracy: 0.9917
Epoch 8/10
32/32 [==============================] - 109s 3s/step - loss: 0.0060 - accuracy: 0.9980
- val_loss: 0.0029 - val_accuracy: 1.0000
Epoch 9/10
32/32 [==============================] - 108s 3s/step - loss: 0.0054 - accuracy: 0.9970
- val_loss: 0.0042 - val_accuracy: 1.0000
Epoch 10/10
32/32 [==============================] - 109s 3s/step - loss: 0.0109 - accuracy: 0.9960
- val_loss: 0.0647 - val_accuracy: 0.9750
<keras.callbacks.History at 0x7fec753079d0>

## Part 4 - Making a single prediction

import numpy as np
from keras.preprocessing import image
import keras.utils as image
test_image = image.load_img('/content/drive/My Drive/g1.jpg', target_size = (64, 64))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)
result = cnn.predict(test_image)
training_set.class_indices
if result[0][0] == 1:
prediction = 'Red Chilli'
else:
prediction = 'Green Chilli'
print(prediction)

Dept. of MCA

You might also like