Machine

Download as pdf or txt
Download as pdf or txt
You are on page 1of 45

EXPERIMENT 1: Write a machine learning program to implement Linear Regression.

SOURCE CODE:
import numpy as nm
import matplotlib.pyplot as mtp
import pandas as pd

data_set = pd.read_csv('data_for_lr.csv')
data_set = data_set.dropna()

x = data_set.iloc[:, :-1].values
y = data_set.iloc[:, 1].values

# Splitting the dataset into training 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)

# Fitting the Simple Linear Regression model to the training dataset


from sklearn.linear_model import LinearRegression

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

# Prediction of Test and Training set result


y_pred = regressor.predict(x_test)
X_pred = regressor.predict(x_train)

mtp.scatter(x_train, y_train, color="green")


mtp.plot(x_train, X_pred, color="red")
mtp.title("X vs Y (Training Dataset)")
mtp.xlabel("X")
mtp.ylabel("Y")
mtp.show()

# visualizing the Test set results


mtp.scatter(x_test, y_test, color="blue")
mtp.plot(x_train, X_pred, color="red")
mtp.title("X vs Y (Test Dataset)")
mtp.xlabel("X")
mtp.ylabel("Y")
mtp.show()

Program No: 01
OUTPUT:

Program No: 01
EXPRIMENT 2: Write a machine learning program to implement Logistic Regression and Locally
Weighted Regression Algorithm.

SOURCE CODE:

Logistic Regression
# importing libraries
import numpy as nm
import matplotlib.pyplot as mtp
import pandas as pd

data_set = pd.read_csv('logistic regression dataset-Social_Network_Ads.csv')


data_set = data_set.dropna()

# Extracting Independent and dependent Variable


x = data_set.iloc[:, [2, 3]].values
y = data_set.iloc[:, 4].values

# Splitting the dataset into training 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)

# feature Scaling
from sklearn.preprocessing import StandardScaler

st_x = StandardScaler()
x_train = st_x.fit_transform(x_train)
x_test = st_x.transform(x_test)

# Fitting Logistic Regression to the training set


from sklearn.linear_model import LogisticRegression

classifier = LogisticRegression(random_state=0)
classifier.fit(x_train, y_train)

LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,


intercept_scaling=1, l1_ratio=None, max_iter=100,
multi_class='warn', n_jobs=None, penalty='l2',
random_state=0, solver='warn', tol=0.0001, verbose=0,
warm_start=False)

# Predicting the test set result


y_pred = classifier.predict(x_test)

# Visualizing the training set result


from matplotlib.colors import ListedColormap

Program No: 02
x_set, y_set = x_train, y_train
x1, x2 = nm.meshgrid(nm.arange(start=x_set[:, 0].min() - 1, stop=x_set[:,
0].max() + 1, step=0.01),
nm.arange(start=x_set[:, 1].min() - 1, stop=x_set[:,
1].max() + 1, step=0.01))
mtp.contourf(x1, x2, classifier.predict(nm.array([x1.ravel(),
x2.ravel()]).T).reshape(x1.shape),
alpha=0.75, cmap=ListedColormap(('purple', 'green')))
mtp.xlim(x1.min(), x1.max())
mtp.ylim(x2.min(), x2.max())
for i, j in enumerate(nm.unique(y_set)):
mtp.scatter(x_set[y_set == j, 0], x_set[y_set == j, 1],
c=ListedColormap(('purple', 'green'))(i), label=j)
mtp.title('Logistic Regression (Training set)')
mtp.xlabel('Age')
mtp.ylabel('Estimated Salary')
mtp.legend()
mtp.show()

# Visulaizing the test set result


from matplotlib.colors import ListedColormap

x_set, y_set = x_test, y_test


x1, x2 = nm.meshgrid(nm.arange(start=x_set[:, 0].min() - 1, stop=x_set[:,
0].max() + 1, step=0.01),
nm.arange(start=x_set[:, 1].min() - 1, stop=x_set[:,
1].max() + 1, step=0.01))
mtp.contourf(x1, x2, classifier.predict(nm.array([x1.ravel(),
x2.ravel()]).T).reshape(x1.shape),
alpha=0.75, cmap=ListedColormap(('purple', 'green')))
mtp.xlim(x1.min(), x1.max())
mtp.ylim(x2.min(), x2.max())
for i, j in enumerate(nm.unique(y_set)):
mtp.scatter(x_set[y_set == j, 0], x_set[y_set == j, 1],
c=ListedColormap(('purple', 'green'))(i), label=j)
mtp.title('Logistic Regression (Test set)')
mtp.xlabel('Age')
mtp.ylabel('Estimated Salary')
mtp.legend()
mtp.show()

Program No: 02
OUTPUT:

Program No: 02
Locally Weighted Regression

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib
inline
df = pd.read_csv('tips.csv')
features = np.array(df.total_bill)
labels = np.array(df.tip)

def kernel(data, point, xmat, k):


m, n = np.shape(xmat)
ws = np.mat(np.eye((m)))
for j in range(m):
diff = point - data[j]
ws[j, j] = np.exp(diff * diff.T / (-2.0 * k ** 2))
return ws

def local_weight(data, point, xmat, ymat, k):


wei = kernel(data, point, xmat, k)
return (data.T * (wei * data)).I * (data.T * (wei * ymat.T))

def local_weight_regression(xmat, ymat, k):


m, n = np.shape(xmat)
ypred = np.zeros(m)
for i in range(m):
ypred[i] = xmat[i] * local_weight(xmat, xmat[i], xmat, ymat, k)
return ypred

m = features.shape[0]
mtip = np.mat(labels)
data = np.hstack((np.ones((m, 1)), np.mat(features).T))

ypred = local_weight_regression(data, mtip, 0.5)


indices = data[:, 1].argsort(0)
xsort = data[indices][:, 0]

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.scatter(features, labels, color='blue')
ax.plot(xsort[:, 1], ypred[indices], color='red', linewidth=3)
plt.xlabel('Total bill')

Program No: 02
plt.ylabel('Tip')
plt.show()

OUTPUT:

Program No: 02
EXPERIMENT 3: Write a machine learning program to implement Naïve Bayes Classifier.

SOURCE CODE:
# Naive Bayes Classifier in Python

import pandas as pd

df = pd.read_csv('loan_data.csv')
df.head()

df.info()

import seaborn as sns


import matplotlib.pyplot as plt

sns.countplot(data=df, x='purpose', hue='not.fully.paid')


plt.xticks(rotation=45, ha='right');

pre_df = pd.get_dummies(df, columns=['purpose'], drop_first=True)


pre_df.head()

from sklearn.model_selection import train_test_split

X = pre_df.drop('not.fully.paid', axis=1)
y = pre_df['not.fully.paid']

X_train, X_test, y_train, y_test = train_test_split(


X, y, test_size=0.33, random_state=125
)

from sklearn.naive_bayes import GaussianNB

model = GaussianNB()

model.fit(X_train, y_train);

from sklearn.metrics import (


accuracy_score,
confusion_matrix,
ConfusionMatrixDisplay,
f1_score,
classification_report,
)

y_pred = model.predict(X_test)

accuray = accuracy_score(y_pred, y_test)


f1 = f1_score(y_pred, y_test, average="weighted")

print("Accuracy:", accuray)
print("F1 Score:", f1)
labels = ["Fully Paid", "Not fully Paid"]
cm = confusion_matrix(y_test, y_pred)
disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=labels)
disp.plot();

Program No: 03
OUTPUT:

Program No: 03
Program No: 03
EXPERIMENT 4: Write a machine learning program to implement SVM Classifier for
Classification.

SOURCE CODE:
# Import scikit-learn dataset library
from sklearn import datasets

# Load dataset
cancer = datasets.load_breast_cancer()
# print the names of the 13 features
print("Features: ", cancer.feature_names)

# print the label type of cancer('malignant' 'benign')


print("Labels: ", cancer.target_names)
Features: ['mean radius' 'mean texture' 'mean perimeter' 'mean area'
'mean smoothness' 'mean compactness' 'mean concavity'
'mean concave points' 'mean symmetry' 'mean fractal dimension'
'radius error' 'texture error' 'perimeter error' 'area error'
'smoothness error' 'compactness error' 'concavity error'
'concave points error' 'symmetry error' 'fractal dimension error'
'worst radius' 'worst texture' 'worst perimeter' 'worst area'
'worst smoothness' 'worst compactness' 'worst concavity'
'worst concave points' 'worst symmetry' 'worst fractal dimension']
Labels: ['malignant' 'benign']
# print data(feature)shape
cancer.data.shape

# print the cancer data features (top 5 records)


print(cancer.data[0:5])

# print the cancer labels (0:malignant, 1:benign)


print(cancer.target)

# Import train_test_split function


from sklearn.model_selection import train_test_split

# Split dataset into training set and test set


X_train, X_test, y_train, y_test = train_test_split(cancer.data,
cancer.target, test_size=0.3,
random_state=109) # 70%
training and 30% test

# Import svm model


from sklearn import svm

# Create a svm Classifier


clf = svm.SVC(kernel='linear') # Linear Kernel

# Train the model using the training sets


clf.fit(X_train, y_train)

Program No: 04
# Predict the response for test dataset
y_pred = clf.predict(X_test)

# Import scikit-learn metrics module for accuracy calculation


from sklearn import metrics

# Model Accuracy: how often is the classifier correct?


print("Accuracy:", metrics.accuracy_score(y_test, y_pred))

# Model Precision: what percentage of positive tuples are labeled as such?


print("Precision:", metrics.precision_score(y_test, y_pred))

# Model Recall: what percentage of positive tuples are labelled as such?


print("Recall:", metrics.recall_score(y_test, y_pred))

Program No: 04
OUTPUT:

Program No: 04
EXPERIMENT 5: Write a machine learning program to implement Decision tree for Classification
as well a for Regression.

SOURCE CODE:

DecisionTreeClassifier
# Import the necessary libraries
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from sklearn.tree import export_graphviz
from graphviz import Source

# Load the dataset


iris = load_iris()
X = iris.data[:, 2:] # petal length and width
y = iris.target

# DecisionTreeClassifier
tree_clf = DecisionTreeClassifier(criterion='entropy',
max_depth=2)
tree_clf.fit(X, y)

# Plot the decision tree graph


export_graphviz(
tree_clf,
out_file="iris_tree.dot",
feature_names=iris.feature_names[2:],
class_names=iris.target_names,
rounded=True,
filled=True
)

with open("iris_tree.dot") as f:
dot_graph = f.read()

Source(dot_graph)

Program No: 05
OUTPUT:

Program No: 05
DecisionTreeRegressor

# Import the necessary libraries


from sklearn.datasets import load_diabetes
from sklearn.tree import DecisionTreeRegressor
from sklearn.tree import export_graphviz
from graphviz import Source

# Load the dataset


diabetes = load_diabetes()
X = diabetes.data
y = diabetes.target

# DecisionTreeRegressor
tree_reg = DecisionTreeRegressor(criterion='squared_error',
max_depth=2)

tree_reg.fit(X, y)

# Plot the decision tree graph


export_graphviz(
tree_reg,
out_file="diabetes_tree.dot",
feature_names=diabetes.feature_names,
class_names=diabetes.target,
rounded=True,
filled=True
)

with open("diabetes_tree.dot") as f:
dot_graph = f.read()

Source(dot_graph)

Program No: 05
OUTPUT:

Program No: 05
EXPERIMENT 6: Write a machine learning program to implement K-Nearest Neighbors Algorithm.

SOURCE CODE:
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np

from sklearn.preprocessing import StandardScaler

df = pd.read_csv("prostate.csv")
scaler = StandardScaler()

scaler.fit(df.drop('Target', axis=1))
scaled_features = scaler.transform(df.drop('Target',
axis=1))

df_feat = pd.DataFrame(scaled_features,
columns=df.columns[:-1])
df_feat.head()

from sklearn.metrics import classification_report, \


confusion_matrix
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split

X_train, X_test, \
y_train, y_test = train_test_split(scaled_features,
df['Target'],
test_size=0.30)

# Remember that we are trying to come up


# with a model to predict whether
# someone will Target or not.
# We'll start with k = 1.

knn = KNeighborsClassifier(n_neighbors=1)
knn.fit(X_train, y_train)
pred = knn.predict(X_test)

# Predictions and Evaluations


# Let's evaluate our KNN model !
print(confusion_matrix(y_test, pred))
print(classification_report(y_test, pred))

error_rate = []

# Will take some time


for i in range(1, 40):
knn = KNeighborsClassifier(n_neighbors=i)
Program No: 06
knn.fit(X_train, y_train)
pred_i = knn.predict(X_test)
error_rate.append(np.mean(pred_i != y_test))

plt.figure(figsize=(10, 6))
plt.plot(range(1, 40), error_rate, color='blue',
linestyle='dashed', marker='o',
markerfacecolor='red', markersize=10)

plt.title('Error Rate vs. K Value')


plt.xlabel('K')
plt.ylabel('Error Rate')
plt.show()

OUTPUT:

Program No: 06
Program No: 06
EXPERIMENT 7: Write a machine learning program to multi-layer Preceptor for Classification.

SOURCE CODE:
# importing modules
import tensorflow as tf
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Activation
import matplotlib.pyplot as plt

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()

# Cast the records into float values


x_train = x_train.astype('float32')
x_test = x_test.astype('float32')

# normalize image pixel values by dividing


# by 255
gray_scale = 255
x_train /= gray_scale
x_test /= gray_scale

print("Feature matrix:", x_train.shape)


print("Target matrix:", x_test.shape)
print("Feature matrix:", y_train.shape)
print("Target matrix:", y_test.shape)

fig, ax = plt.subplots(10, 10)


k = 0
for i in range(10):
for j in range(10):
ax[i][j].imshow(x_train[k].reshape(28, 28),
aspect='auto')
k += 1
plt.show()

model = Sequential([

# reshape 28 row * 28 column data to 28*28 rows


Flatten(input_shape=(28, 28)),

# dense layer 1
Dense(256, activation='sigmoid'),

# dense layer 2
Dense(128, activation='sigmoid'),

# output layer
Program No: 07
Dense(10, activation='sigmoid'),
])

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

model.fit(x_train, y_train, epochs=10,


batch_size=2000,
validation_split=0.2)

results = model.evaluate(x_test, y_test, verbose=0)


print('test loss, test acc:', results)

OUTPUT:

Program No: 07
Program No: 07
EXPERIMENT 8: Write a machine learning program to implement CNN for the case study of
Diabetic Retinopathy.

SOURCE CODE:

Pre-Processing
from scipy import misc
from PIL import Image
from skimage import exposure
from sklearn import svm

import scipy
from math import sqrt,pi
from numpy import exp
from matplotlib import pyplot as plt
import numpy as np
import glob
import matplotlib.pyplot as pltss
import cv2
from matplotlib import cm
import pandas as pd
from math import pi, sqrt
import pywt

#img_rows=img_cols=200
immatrix=[]
im_unpre = []
#image_path =
Image.open('C:\Users\Rohan\Desktop\Diabetic_Retinopathy\diaretdb1_v_1_1\diaret
db1_v_1_1\resources\images\ddb1_fundusimages\image0')
#image = misc.imread(image_path)

for i in range(1,90):
img_pt =
r'C:\Users\Rohan\Desktop\Diabetic_Retinopathy\diaretdb1_v_1_1\diaretdb1_v_1_1\
resources\images\ddb1_fundusimages\image'
if i < 10:
img_pt = img_pt + "00" + str(i) + ".png"
else:
img_pt = img_pt + "0" + str(i)+ ".png"

img = cv2.imread(img_pt)
#im_unpre.append(np.array(img).flatten())
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
equ = cv2.equalizeHist(img_gray)
immatrix.append(np.array(equ).flatten())
#res = np.hstack((img_gray,equ))

Program No: 08
np.shape(np.array(equ).flatten())

np.shape(immatrix)
np.shape(equ)
plt.imshow(immatrix[78].reshape((1152,1500)),cmap='gray')
plt.show()

imm_dwt = []
for equ in immatrix:
equ = equ.reshape((1152,1500))
coeffs = pywt.dwt2(equ, 'haar')
equ2 = pywt.idwt2(coeffs, 'haar')
imm_dwt.append(np.array(equ2).flatten())

np.shape(imm_dwt)
np.shape(equ2)
plt.imshow(imm_dwt[78].reshape((1152,1500)),cmap='gray')
plt.show()

def _filter_kernel_mf_fdog(L, sigma, t=3, mf=True):


dim_y = int(L)
dim_x = 2 * int(t * sigma)
arr = np.zeros((dim_y, dim_x), 'f')

ctr_x = dim_x / 2
ctr_y = int(dim_y / 2.)

# an un-natural way to set elements of the array


# to their x coordinate.
# x's are actually columns, so the first dimension of the iterator is used
it = np.nditer(arr, flags=['multi_index'])
while not it.finished:
arr[it.multi_index] = it.multi_index[1] - ctr_x
it.iternext()

two_sigma_sq = 2 * sigma * sigma


sqrt_w_pi_sigma = 1. / (sqrt(2 * pi) * sigma)
if not mf:
sqrt_w_pi_sigma = sqrt_w_pi_sigma / sigma ** 2

# @vectorize(['float32(float32)'], target='cpu')
def k_fun(x):
return sqrt_w_pi_sigma * exp(-x * x / two_sigma_sq)

# @vectorize(['float32(float32)'], target='cpu')
def k_fun_derivative(x):

Program No: 08
return -x * sqrt_w_pi_sigma * exp(-x * x / two_sigma_sq)

if mf:
kernel = k_fun(arr)
kernel = kernel - kernel.mean()
else:
kernel = k_fun_derivative(arr)

# return the "convolution" kernel for filter2D


return cv2.flip(kernel, -1)

def show_images(images, titles=None, scale=1.3):


"""Display a list of images"""
n_ims = len(images)
if titles is None: titles = ['(%d)' % i for i in range(1, n_ims + 1)]
fig = plt.figure()
n = 1
for image, title in zip(images, titles):
a = fig.add_subplot(1, n_ims, n) # Make subplot
if image.ndim == 2: # Is image grayscale?
plt.imshow(image, cmap=cm.Greys_r)
else:
plt.imshow(cv2.cvtColor(image, cv2.COLOR_RGB2BGR))
a.set_title(title)
plt.axis("off")
n += 1
fig.set_size_inches(np.array(fig.get_size_inches(), dtype=np.float) *
n_ims / scale)
plt.show()

def gaussian_matched_filter_kernel(L, sigma, t=3):


'''
K = 1/(sqrt(2 * pi) * sigma ) * exp(-x^2/2sigma^2), |y| <= L/2, |x| < s *
t
'''
return _filter_kernel_mf_fdog(L, sigma, t, True)

# Creating a matched filter bank using the kernel generated from the above
functions
def createMatchedFilterBank(K, n=12):
rotate = 180 / n
center = (K.shape[1] / 2, K.shape[0] / 2)
cur_rot = 0
kernels = [K]

for i in range(1, n):

Program No: 08
cur_rot += rotate
r_mat = cv2.getRotationMatrix2D(center, cur_rot, 1)
k = cv2.warpAffine(K, r_mat, (K.shape[1], K.shape[0]))
kernels.append(k)

return kernels

# Given a filter bank, apply them and record maximum response

def applyFilters(im, kernels):


images = np.array([cv2.filter2D(im, -1, k) for k in kernels])
return np.max(images, 0)

gf = gaussian_matched_filter_kernel(20, 5)
bank_gf = createMatchedFilterBank(gf, 4)

imm_gauss = []
for equ2 in imm_dwt:
equ2 = equ2.reshape((1152, 1500))
equ3 = applyFilters(equ2, bank_gf)
imm_gauss.append(np.array(equ3).flatten())

# the array ranges from 0 - 89


np.shape(imm_gauss)
plt.imshow(imm_gauss[78].reshape((1152,1500)),cmap='gray')
plt.show()

def createMatchedFilterBank():
filters = []
ksize = 31
for theta in np.arange(0, np.pi, np.pi / 16):
kern = cv2.getGaborKernel((ksize, ksize), 6, theta,12, 0.37, 0,
ktype=cv2.CV_32F)
kern /= 1.5*kern.sum()
filters.append(kern)
return filters

def applyFilters(im, kernels):


images = np.array([cv2.filter2D(im, -1, k) for k in kernels])
return np.max(images, 0)

bank_gf = createMatchedFilterBank()
#equx=equ3
#equ3 = applyFilters(equ2,bank_gf)
imm_gauss2 = []
for equ2 in imm_dwt:

Program No: 08
equ2 = equ2.reshape((1152,1500))
equ3 = applyFilters(equ2,bank_gf)
imm_gauss2.append(np.array(equ3).flatten())

# the array ranges from 0 - 89


np.shape(imm_gauss2)
plt.imshow(imm_gauss2[20].reshape((1152,1500)),cmap='gray')
plt.show()

# the array ranges from 0 - 89


np.shape(imm_gauss2)
plt.imshow(imm_gauss2[1].reshape((1152,1500)),cmap='gray')
plt.show()

e_ = equ3
np.shape(e_)
e_=e_.reshape((-1,3))
np.shape(e_)

img = equ3
Z = img.reshape((-1,3))

# convert to np.float32
Z = np.float32(Z)

k=cv2.KMEANS_PP_CENTERS

# define criteria, number of clusters(K) and apply kmeans()


criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
K = 2
ret,label,center=cv2.kmeans(Z,K,None,criteria,10,k)

# Now convert back into uint8, and make original image


center = np.uint8(center)
res = center[label.flatten()]
res2 = res.reshape((img.shape))

imm_kmean = []
for equ3 in imm_gauss2:
img = equ3.reshape((1152,1500))
Z = img.reshape((-1,3))

# convert to np.float32
Z = np.float32(Z)

Program No: 08
k=cv2.KMEANS_PP_CENTERS

# define criteria, number of clusters(K) and apply kmeans()


criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
K = 2
ret,label,center=cv2.kmeans(Z,K,None,criteria,10,k)

# Now convert back into uint8, and make original image


center = np.uint8(center)
res = center[label.flatten()]
res2 = res.reshape((img.shape))
imm_kmean.append(np.array(res2).flatten())

# the array ranges from 0 - 89


np.shape(imm_kmean)
plt.imshow(imm_kmean[78].reshape((1152,1500)),cmap="gray")
plt.show()

Model Training

from sklearn.svm import SVC


clf = SVC()

Y = np.ones(89)

Y[1]=Y[5]=Y[7]=Y[17]=Y[6]=0]

clf.fit(imm_kmean, Y)]

y_pred = clf.predict(imm_kmean)

k =
[1,3,4,9,10,11,13,14,20,22,24,25,26,27,28,29,35,36,38,42,53,55,57,64,70,79,84,
86]
k = k-np.ones(len(k))
k

Program No: 08
imm_train = []
y_train = []
k.append(5)
k.append(7)
for i in k:
imm_train.append(imm_kmean[i])
y_train.append(Y[i])
y_train

clf.fit(imm_train, y_train)

y_pred = clf.predict(imm_kmean)
accuracy_score(Y,y_pred)

OUTPUT:

Program No: 08
Program No: 08
Program No: 08
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape=None, degree=3, gamma='auto', kernel='rbf',
max_iter=-1, probability=False, random_state=None, shrinking=True,
tol=0.001, verbose=False)

[0, 2, 3, 8, 9, 10, 12, 13, 19, 21, 23, 24, 25, 26, 27, 28, 34, 35, 37,
41, 52, 54, 56, 63, 69, 78, 83,85]

[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1
.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0
, 0.0]

SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,


decision_function_shape=None, degree=3, gamma='auto', kernel='rbf',
max_iter=-1, probability=False, random_state=None, shrinking=True,
tol=0.001, verbose=False)

0.9662921348314607

Program No: 08
EXPERIMENT 9: Write a machine learning program to implement Genetic Algorithm on dataset
with multiple features.

SOURCE CODE:
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from random import randint
%matplotlib
inline
import warnings

warnings.filterwarnings("ignore")

from sklearn.model_selection import train_test_split

def split(df, label):


X_tr, X_te, Y_tr, Y_te = train_test_split(df, label, test_size=0.25,
random_state=42)
return X_tr, X_te, Y_tr, Y_te

from sklearn import svm


from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn import metrics
from sklearn.metrics import accuracy_score
from sklearn.model_selection import KFold, cross_val_score

classifiers = ['LinearSVM', 'RadialSVM',


'Logistic', 'RandomForest',
'AdaBoost', 'DecisionTree',
'KNeighbors', 'GradientBoosting']

models = [svm.SVC(kernel='linear'),
svm.SVC(kernel='rbf'),
LogisticRegression(max_iter=1000),
RandomForestClassifier(n_estimators=200, random_state=0),
AdaBoostClassifier(random_state=0),
DecisionTreeClassifier(random_state=0),
KNeighborsClassifier(),
GradientBoostingClassifier(random_state=0)]

def acc_score(df, label):


Score = pd.DataFrame({"Classifier": classifiers})

Program No: 09
j = 0
acc = []
X_train, X_test, Y_train, Y_test = split(df, label)
for i in models:
model = i
model.fit(X_train, Y_train)
predictions = model.predict(X_test)
acc.append(accuracy_score(Y_test, predictions))
j = j + 1
Score["Accuracy"] = acc
Score.sort_values(by="Accuracy", ascending=False, inplace=True)
Score.reset_index(drop=True, inplace=True)
return Score

def plot(score, x, y, c="b"):


gen = [1, 2, 3, 4, 5]
plt.figure(figsize=(6, 4))
ax = sns.pointplot(x=gen, y=score, color=c)
ax.set(xlabel="Generation", ylabel="Accuracy")
ax.set(ylim=(x, y))

def initilization_of_population(size, n_feat):


population = []
for i in range(size):
chromosome = np.ones(n_feat, dtype=np.bool)
chromosome[:int(0.3 * n_feat)] = False
np.random.shuffle(chromosome)
population.append(chromosome)
return population

def fitness_score(population):
scores = []
for chromosome in population:
logmodel.fit(X_train.iloc[:, chromosome], Y_train)
predictions = logmodel.predict(X_test.iloc[:, chromosome])
scores.append(accuracy_score(Y_test, predictions))
scores, population = np.array(scores), np.array(population)
inds = np.argsort(scores)
return list(scores[inds][::-1]), list(population[inds, :][::-1])

def selection(pop_after_fit, n_parents):


population_nextgen = []
for i in range(n_parents):
population_nextgen.append(pop_after_fit[i])
return population_nextgen

Program No: 09
def crossover(pop_after_sel):
pop_nextgen = pop_after_sel
for i in range(0, len(pop_after_sel), 2):
new_par = []
child_1, child_2 = pop_nextgen[i], pop_nextgen[i + 1]
new_par = np.concatenate((child_1[:len(child_1) // 2],
child_2[len(child_1) // 2:]))
pop_nextgen.append(new_par)
return pop_nextgen

def mutation(pop_after_cross, mutation_rate, n_feat):


mutation_range = int(mutation_rate * n_feat)
pop_next_gen = []
for n in range(0, len(pop_after_cross)):
chromo = pop_after_cross[n]
rand_posi = []
for i in range(0, mutation_range):
pos = randint(0, n_feat - 1)
rand_posi.append(pos)
for j in rand_posi:
chromo[j] = not chromo[j]
pop_next_gen.append(chromo)
return pop_next_gen

def generations(df, label, size, n_feat, n_parents, mutation_rate, n_gen,


X_train,
X_test, Y_train, Y_test):
best_chromo = []
best_score = []
population_nextgen = initilization_of_population(size, n_feat)
for i in range(n_gen):
scores, pop_after_fit = fitness_score(population_nextgen)
print('Best score in generation', i + 1, ':', scores[:1]) # 2
pop_after_sel = selection(pop_after_fit, n_parents)
pop_after_cross = crossover(pop_after_sel)
population_nextgen = mutation(pop_after_cross, mutation_rate, n_feat)
best_chromo.append(pop_after_fit[0])
best_score.append(scores[0])
return best_chromo, best_score

Program No: 09
data_bc = pd.read_csv("data.csv")
label_bc = data_bc["diagnosis"]
label_bc = np.where(label_bc == 'M', 1, 0)
data_bc.drop(["id", "diagnosis", "Unnamed: 32"], axis=1, inplace=True)

print("Breast Cancer dataset:\n", data_bc.shape[0], "Records\n",


data_bc.shape[1], "Features")

score1 = acc_score(data_bc, label_bc)


score1

logmodel = RandomForestClassifier(n_estimators=200, random_state=0)


X_train, X_test, Y_train, Y_test = split(data_bc, label_bc)
chromo_df_bc, score_bc = generations(data_bc, label_bc, size=80,
n_feat=data_bc.shape[1], n_parents=64,
mutation_rate=0.20, n_gen=5,
X_train=X_train, X_test=X_test,
Y_train=Y_train, Y_test=Y_test)

plot(score_bc, 0.9, 1.0, c="gold")

OUTPUT:

Program No: 09
Program No: 09
EXPERIMENT 10: Write a machine learning program to implement Reinforcement Learning
(Q- Learning Technique) in game playing.

SOURCE CODE:
'''
#import libraries
'''

import numpy as np
print('numpy: %s' % np.__version__) # print version

# Note need to 'pip install gym', and 'pip install gym[toy_text]'


# or 'pip install gym\[toy_text\]' if zsh
does nor recongize the first command
import gym # for simulated environments
print('gym: %s' % gym.__version__) # print version

import matplotlib
import matplotlib.pyplot as plt # for displaying environment states
print('matplotlib: %s' % matplotlib.__version__) # print version

from IPython import display # for displaying environment states


import time # for slowing down rendering of states by adding small time delays
#%matplotlib inline

'''
#Setup the Environment
'''

env = gym.make(id='FrozenLake-v1', # choose one of the existing environments


desc=None, # Used to specify custom map for frozen lake. E.g.,
desc=["SFFF", "FHFH", "FFFH", "HFFG"].
map_name='4x4', # ID to use any of the preloaded maps. E.g.,
'4x4', '8x8'
is_slippery=False, # True/False. If True will move in intended
direction with probability of 1/3 else will move in either perpendicular
direction with equal probability of 1/3 in both directions.
max_episode_steps=None, # default=None, Maximum length of an
episode (TimeLimit wrapper).
autoreset=False, # default=None, Whether to automatically reset
the environment after each episode (AutoResetWrapper).
disable_env_checker=None, # default=None, If to run the env
checker
render_mode = 'rgb_array' # The set of supported modes varies
per environment. (And some third-party environments may not support rendering
at all.)
)

Program No: 10
# Show environment description (map) as an array
env.desc

# Observation and action space


state_obs_space = env.observation_space # Returns sate(observation) space of
the environment.
action_space = env.action_space # Returns action space of the environment.
print("State(Observation) space:", state_obs_space)
print("Action space:", action_space)

'''
# Interacting with the Environment
'''

# Specify seed for reproducability (optional)


#env.action_space.seed(42)

# Reset environment to initial state


#state, info = env.reset(seed=42)
state, info = env.reset()

# Print the state(observstion) of the environment


print("The initial state is {}".format(state))
#print("The initial information {}".format(info))

# Cycle through 20 random steps redering and displaying the agent inside the
environment each time
for _ in range(20):

# Render and display current state of the environment


plt.imshow(env.render()) # render current state and pass to pyplot
plt.axis('off')
display.display(plt.gcf()) # get current figure and display
display.clear_output(wait=True) # clear output before showing the next
frame

# Sample a random action from the entire action space


random_action = env.action_space.sample()

# Pass the random action into the step function


state, reward, done, _, info = env.step(random_action)

Program No: 10
# Wait a little bit before the next frame
time.sleep(0.2)

# Reset environment when done=True, i.e., when the agent falls into a Hole
(H) or reaches the Goal (G)
if done:
# Render and display current state of the environment
plt.imshow(env.render()) # render current state and pass to pyplot
plt.axis('off')
display.display(plt.gcf()) # get current figure and display
display.clear_output(wait=True) # clear output before showing the
next frame
# Reset environment
state, info = env.reset()

# Close environment
env.close()

'''
Train a model, i.e., find optimal Policy (π)
'''

# Q-function parameters
alpha = 0.7 # learning rate
gamma = 0.95 # discount factor

# Training parameters
n_episodes = 10000 # number of episodes to use for training
n_max_steps = 100 # maximum number of steps per episode

# Exploration / Exploitation parameters


start_epsilon = 1.0 # start training by selecting purely random actions
min_epsilon = 0.05 # the lowest epsilon allowed to decay to
decay_rate = 0.001 # epsilon will gradually decay so we do less exploring
and more exploiting as Q-function improves

# Initial Q-table
# Our Q-table is a matrix of state(observation) space x action space, i.e., 16
x 4
Qtable = np.zeros((env.observation_space.n, env.action_space.n))

# Show
Qtable

# This is our acting policy (epsilon-greedy), for the agent to do exploration


and exploitation during training
def epsilon_greedy(Qtable, state, epsilon):

Program No: 10
# Generate a random number and compare to epsilon, if lower then explore,
itherwuse exploit
randnum = np.random.uniform(0, 1)
if randnum < epsilon:
action = env.action_space.sample() # explore
else:
action = np.argmax(Qtable[state, :]) # exploit
return action

# This is our updating policy (greedy)


# i.e., always select the action with the highest value for that state:
np.max(Qtable[next_state])
def update_Q(Qtable, state, action, reward, next_state):
# Q(S_t,A_t) = Q(S_t,A_t) + alpha [R_t+1 + gamma * max Q(S_t+1,a) -
Q(S_t,A_t)]
Qtable[state][action] = Qtable[state][action] + alpha * (reward + gamma *
np.max(Qtable[next_state]) - Qtable[state][action])
return Qtable

# Always go for the highest value (state,action) pair


def eval_greedy(Qtable, state):
action = np.argmax(Qtable[state, :])
return action

def train(n_episodes, n_max_steps, start_epsilon, min_epsilon, decay_rate,


Qtable):
for episode in range(n_episodes):

# Reset the environment at the start of each episode


state, info = env.reset()
t = 0
done = False

# Calculate epsilon value based on decay rate


epsilon = max(min_epsilon, (start_epsilon - min_epsilon) * np.exp(-
decay_rate * episode))

for t in range(n_max_steps):
# Choose an action using previously defined epsilon greedy policy
action = epsilon_greedy(Qtable, state, epsilon)

# Perform the action in the environment, get reward and next state
next_state, reward, done, _, info = env.step(action)

# Update Q-table
Qtable = update_Q(Qtable, state, action, reward, next_state)

# Update current state


Program No: 10
state = next_state

# Finish the episode when done=True, i.e., reached the goal or


fallen into a hole
if done:
break

# Return final Q-table


return Qtable

# Train
Qtable = train(n_episodes, n_max_steps, start_epsilon, min_epsilon,
decay_rate, Qtable)

# Show Q-table
Qtable

'''
Evaluate the Q-function to see if we managed to find the best policy
'''

def evaluate_agent(n_max_steps, n_eval_episodes, Qtable):


# Initialize an empty list to store rewards for each episode
episode_rewards = []

# Evaluate for each episode


for episode in range(n_eval_episodes):

# Reset the environment at the start of each episode


state, info = env.reset()
t = 0
done = False
tot_episode_reward = 0

for t in range(n_max_steps):

# Use greedy policy to evaluate


action = eval_greedy(Qtable, state)

# Pass action into step function


next_state, reward, done, _, info = env.step(action)

# Sum episode rewards


tot_episode_reward += reward

# Update current state


state = next_state

Program No: 10
# Finish the episode when done=True, i.e., reached the goal or
fallen into a hole
if done:
break

episode_rewards.append(tot_episode_reward)

mean_reward = np.mean(episode_rewards)
std_reward = np.std(episode_rewards)

return mean_reward, std_reward

n_eval_episodes=100
mean_reward, std_reward = evaluate_agent(n_max_steps, n_eval_episodes, Qtable)
print(f"Mean Reward = {mean_reward:.2f} +/- {std_reward:.2f}")

# Cycle through 19 steps redering and displaying environment state each time
state, info = env.reset()
for _ in range(19):

# Render and display current state of the environment


plt.imshow(env.render()) # render current state and pass to pyplot
plt.axis('off')
display.display(plt.gcf()) # get current figure and display
display.clear_output(wait=True) # clear output before showing the next
frame

# Use greedy policy to evaluate


action = eval_greedy(Qtable, state)

# Pass action into step function


state, reward, done, _, info = env.step(action)

# Wait a little bit before the next frame


time.sleep(0.2)

# Reset environment when done=True, i.e. when the agent falls into a Hole
(H) or reaches the Goal (G)
if done:
# Render and display final state of the environment
plt.imshow(env.render()) # render current state and pass to pyplot
plt.axis('off')
display.display(plt.gcf()) # get current figure and display
display.clear_output(wait=True) # clear output before showing the
next frame
state, info = env.reset()

env.close()

Program No: 10
OUTPUT:

array([[b'S', b'F', b'F', b'F'],


[b'F', b'H', b'F', b'H'],
[b'F', b'F', b'F', b'H'],
[b'H', b'F', b'F', b'G']], dtype='|S1')

array([[0., 0., 0., 0.],


[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])

array([[0.73509189, 0.77378094, 0.77378094, 0.73509189],


[0.73509189, 0. , 0.81450625, 0.77378094],
[0.77378094, 0.857375 , 0.77378094, 0.81450625],
[0.81450625, 0. , 0.77376731, 0.76585808],
[0.77378094, 0.81450625, 0. , 0.73509189],
[0. , 0. , 0. , 0. ],
[0. , 0.9025 , 0. , 0.81450625],
[0. , 0. , 0. , 0. ],
[0.81450625, 0. , 0.857375 , 0.77378094],
[0.81450625, 0.9025 , 0.9025 , 0. ],
[0.857375 , 0.95 , 0. , 0.857375 ],
[0. , 0. , 0. , 0. ],
[0. , 0. , 0. , 0. ],
[0. , 0.9025 , 0.95 , 0.857375 ],
[0.9025 , 0.95 , 1. , 0.9025 ],
[0. , 0. , 0. , 0. ]])

Program No: 10

You might also like