0% found this document useful (0 votes)
13 views83 pages

2nd Year

The document outlines various algorithms and implementations for different search and machine learning techniques, including BFS, DFS, A*, and Naïve Bayes models. It provides code snippets and outputs for each algorithm, demonstrating their functionality with example datasets. Additionally, it covers regression models such as linear regression, multiple linear regression, and logistic regression, along with decision trees.

Uploaded by

Neona josh
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)
13 views83 pages

2nd Year

The document outlines various algorithms and implementations for different search and machine learning techniques, including BFS, DFS, A*, and Naïve Bayes models. It provides code snippets and outputs for each algorithm, demonstrating their functionality with example datasets. Additionally, it covers regression models such as linear regression, multiple linear regression, and logistic regression, along with decision trees.

Uploaded by

Neona josh
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/ 83

EX NO: UNINFORMED SEARCH ALGORITHMS (BFS)

DATE:

AIM:

ALGORITHM:
PROGRAM AND OUTPUT:

GRAPH={'A':['B','C'],'B':['D','E'],'C':['F'],'D':[],'E':['F'],'F':[]}

visited=[]

queue=[]

def BFS(visited,graph,node):

visited.append(node)

queue.append(node)

print("BFS:\t")

while queue:

p=queue.pop(0)

print(p,end=" ")

for child in GRAPH[p]:

if child not in visited:

visited.append(child)

queue.append(child)

BFS(visited,GRAPH,'A')

BFS:

ABCDEF

RESULT:
EX NO: UNINFORMED SEARCH ALGORITHMS (DFS)

DATE:

AIM:

ALGORITHM:
PROGRAM AND OUTPUT:

GRAPH={'0':set(['1','2']),'1':set(['3','4']),'2':set(['0']),'3':set(['2','3']),'4':set([])}

def DFS(GRAPH, node, visited=None):

if visited is None:

visited = set()

visited.add(node)

print(node, end=" ")

if node not in visited:

visited.add(node)

for next in GRAPH[node] - visited:

DFS(GRAPH, next, visited)

return visited

DFS(GRAPH,'0')

02134

{'0', '1', '2', '3', '4'}

RESULT:
EX NO: INFORMED SEARCH ALGORITHMS (A*)

DATE:

AIM:

ALGORITHM:
PROGRAM AND OUTPUT:

def astaralgo(start_node,stop_node):

open_set = set(start_node)

closed_set= set()

g={}

parents = {}

g[start_node] = 0

parents[start_node] = start_node

while len(open_set) > 0:

n=None

for v in open_set:

if n==None or g[v] + h(v) < g[n] + h(n):

n=v

if n==stop_node or Graph_nodes[n]==None:

pass

else:

for(m,weight) in get_neighbors(n):

if m not in open_set and m not in closed_set:

open_set.add(m)

parents[m]=n

g[m] = g[n] + weight

else:

if g[m] > g[n] + weight:

g[m] = g[n] + weight

parents[m]=n

if m in closed_set:

closed_set.remove(m)

open_set.add(m)

if n==None:
print("path does not exist!")

return None

if n==stop_node:

path=[]

while parents[n]!=n:

path.append(n)

n=parents[n]

path.append(start_node)

path.reverse()

print("Path found: {}".format(path))

return path

open_set.remove(n)

closed_set.add(n)

print("Path does not exist!")

return None

def get_neighbors(v):

if v in Graph_nodes:

return Graph_nodes[v]

else:

return None

def h(n):

H_dist = {

'A' : 10,

'B' : 8,

'C' : 5,

'D' : 7,

'E' : 3,

'F' : 6,

'G' : 5,

'H' : 3,
'I' : 1,

'J' : 0

return H_dist[n]

Graph_nodes = {

'A' : [('B',6),('F',3)],

'B' : [('C',3),('D',2)],

'C' : [('E',5),('D',1)],

'E' : [('D',6)],

'D' : [('E',8)],

'F' : [('G',1),('H',7)],

'G' : [('I',3)],

'H' : [('I',2)],

'I' : [('J',3)]

astaralgo('A','J')

Path found: ['A', 'F', 'G', 'I', 'J']

['A', 'F', 'G', 'I', 'J']

RESULT:
EX NO: INFORMED SEARCH ALGORITHMS (MEMORY-BOUNDED A*)

DATE:

AIM:

ALGORITHM:
PROGRAM AND OUTPUT:

import math

graph = {

'S': [('A', 1), ('B', 3)], 'A': [('B', 1), ('C', 3)], 'B': [('C', 1), ('D', 2)], 'C': [('D', 1), ('G', 2)], 'D': [('E',

3)],

'E': [('G', 2)], 'G': []

heuristic = { 'S': 7, 'A': 6,'B': 2,'C': 4,'D': 2,'E': 1,'G': 0}

def ma_star(graph, heuristic,start, goal, max_memory):

stack = [(0, start, [start])]

min_fcost = {start: 0}

while stack:

(f, node, path) = stack.pop()

if node == goal:

return path

if math.isnan(f) or f> max_memory:

continue

for adjacent, cost in graph[node]:

g = cost + min_fcost[node]

if adjacent in min_fcost and g >= min_fcost[adjacent]:

continue

f= g + heuristic[adjacent]

stack.append((f, adjacent, path + [adjacent]))

min_fcost[adjacent] = g

return None

start = 'S'

goal = 'G'

max_memory = 8
path = ma_star(graph, heuristic, start, goal, max_memory)

print('Shortest path:', path)

Shortest path: ['S', 'B', 'C', 'G']

RESULT:
EX NO: NAÏVE BAYES MODELS

DATE:

AIM:

ALGORITHM:
PROGRAM AND OUTPUT:

import pandas as pd

df = pd.read_csv("/content/loan_data.csv")

df.head()

df.info()

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

RangeIndex: 9578 entries, 0 to 9577

Data columns (total 14 columns):

# Column Non-Null Count Dtype

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

0 credit.policy 9578 non-null int64

1 purpose 9578 non-null object

2 int.rate 9578 non-null float64

3 installment 9578 non-null float64

4 log.annual.inc 9578 non-null float64

5 dti 9578 non-null float64

6 fico 9578 non-null int64

7 days.with.cr.line 9578 non-null float64

8 revol.bal 9578 non-null int64

9 revol.util 9578 non-null float64

10 inq.last.6mths 9578 non-null int64

11 delinq.2yrs 9578 non-null int64

12 pub.rec 9578 non-null int64

13 not.fully.paid 9578 non-null int64

dtypes: float64(6), int64(7), object(1)


memory usage: 1.0+ MB

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

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)

accuracy = accuracy_score(y_pred, y_test)

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

print("Accuracy:", accuracy)

print("F1 Score:", f1)

Accuracy: 0.8206263840556786

F1 Score: 0.8686606980013266

labels = ["Fully Paid", "Not fully Paid"]

cm = confusion_matrix(y_test, y_pred)

disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=labels)

disp.plot();
RESULT:
EX NO: BAYESIAN NETWORKS

DATE:

AIM:

ALGORITHM:
PROGRAM AND OUTPUT:

pip install pgmpy

import numpy as np

import pandas as pd

from pgmpy.estimators import MaximumLikelihoodEstimator

from pgmpy.models import BayesianModel

from pgmpy.inference import VariableElimination

heartDisease = pd.read_csv("C:/Users/Student/Desktop/Heartdisease.csv")

heartDisease = heartDisease.replace('?', np.nan)

print('Sample instances from the dataset are given below')

print(heartDisease.head())

Sample instances from the dataset are given below

age gender cp trestbps chol fbs restecg thalach exang oldpeak

0 63 1 1 145 233 1 2 150 0 2.3

1 67 1 4 160 286 0 2 108 1 1.5

2 67 1 4 120 229 0 2 129 1 2.6

3 37 1 3 130 250 0 0 187 0 3.5

4 41 0 2 130 204 0 2 172 0 1.4

slope ca thal heartdisease

03060

12332

22271

33030

41030

print('\n Attributes and datatypes')

print(heartDisease.dtypes)

Attributes and datatypes


age int64

gender int64

cp int64

trestbps int64

chol int64

fbs int64

restecg int64

thalach int64

exang int64

oldpeak float64

slope int64

ca object

thal object

heartdisease int64

dtype: object

model = BayesianModel([('age', 'heartdisease'), ('exang', 'heartdisease'), ('cp', 'heartdisease'),

('heartdisease', 'restecg'), ('heartdisease', 'chol')])

print('\nLearning CPD using Maximum likelihood estimators')

model.fit(heartDisease, estimator=MaximumLikelihoodEstimator)

Learning CPD using Maximum likelihood estimators

print('\n Inferencing with Bayesian Network:')

HeartDisease_test_infer = VariableElimination(model)

Inferencing with Bayesian Network:

print('\n 1. Probability of HeartDisease given evidence= restecg')

q1 = HeartDisease_test_infer.query(variables=['heartdisease'], evidence={'restecg': 1})

print(q1)

1. Probability of HeartDisease given evidence= restecg

+-----------------+---------------------+

| heartdisease | phi(heartdisease) |
+=================+=====================+

| heartdisease(0) | 0.1386 |

+-----------------+---------------------+

| heartdisease(1) | 0.0000 |

+-----------------+---------------------+

| heartdisease(2) | 0.2403 |

+-----------------+---------------------+

| heartdisease(3) | 0.2174 |

+-----------------+---------------------+

| heartdisease(4) | 0.4036 |

+-----------------+---------------------+

print('\n 2. Probability of HeartDisease given evidence= cp ')

q2 = HeartDisease_test_infer.query(variables=['heartdisease'], evidence={'cp': 2})

print(q2)

2. Probability of HeartDisease given evidence= cp

+-----------------+---------------------+

| heartdisease | phi(heartdisease) |

+=================+=====================+

| heartdisease(0) | 0.4433 |

+-----------------+---------------------+

| heartdisease(1) | 0.1888 |

+-----------------+---------------------+

| heartdisease(2) | 0.1189 |

+-----------------+---------------------+

| heartdisease(3) | 0.1377 |

+-----------------+---------------------+

| heartdisease(4) | 0.1114 |

+-----------------+---------------------+
RESULT:
EX NO: BUILD REGRESSION MODELS : LINEAR REGRESSION

DATE:

AIM:

ALGORITHM:
PROGRAM AND OUTPUT:

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

data = pd.read_csv('Salary_Data.csv')

X = data[['YearsExperience']].values

y = data['Salary'].values

print(data.head())

YearsExperience Salary

0 1.1 39343.0

1 1.3 46205.0

2 1.5 37731.0

3 2.0 43525.0

4 2.2 39891.0

# Split the Dataset

from sklearn.model_selection import train_test_split

X_train,X_test,Y_train,Y_test=train_test_split(X,y,test_size=10,random_state=0)

from sklearn.linear_model import LinearRegression

regressor=LinearRegression()

regressor.fit(X_train,Y_train)

y_pre=regressor.predict(X_test)

# Visualizing the Training set results

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()
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()
RESULT:
EX NO: BUILD REGRESSION MODELS: MULTIPLE LINEAR REGRESSION

DATE:

AIM:

ALGORITHM:
PROGRAM AND OUTPUT:

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

from sklearn.linear_model import LinearRegression

from sklearn.model_selection import train_test_split

import matplotlib.pyplot as plt

data = pd.read_csv('Student_Performance.csv')

data.head()

X = data[['Hours Studied','Previous Scores','Sleep Hours','Sample Question Papers

Practiced']].values

y = data['Performance Index'].values

#Split the Dataset

X_train,X_test,Y_train,Y_test=train_test_split(X,y,test_size=10,random_state=0)

regressor=LinearRegression()

regressor.fit(X_train,Y_train)

y_pred=regressor.predict(X_test)

regressor.score(X_test,Y_test)

0.984498422313455

# Visualizing the results

plt.scatter(Y_test, y_pred, color='blue', label='Actual vs. Predicted')

plt.plot([Y_test.min(), Y_test.max()], [Y_test.min(), Y_test.max()], color='red', linestyle='--',

label='Ideal')

plt.title('Actual vs. Predicted')

plt.xlabel('Actual Performance Index')

plt.ylabel('Predicted Performance Index')

plt.legend()

plt.show()
RESULT:
EX NO: BUILD REGRESSION MODELS: LOGISTIC REGRESSION

DATE:

AIM:

ALGORITHM:
PROGRAM AND OUTPUT:

from sklearn.linear_model import LogisticRegression

from sklearn.model_selection import train_test_split

import pandas as pd

df=pd.read_csv("insurance_data.csv")

df.head()

X=df[["age"]]

y=df.bought_insurance

X_train, X_test, y_train, y_test=train_test_split(X,y,test_size=0.1)

model=LogisticRegression()

model.fit(X_train.values.reshape(-1,1),y_train)

print("Prediction for age 40: ", model.predict([[40]]))

print("Prediction for age 30: ", model.predict([[30]]))

print("Prediction for age 70: ", model.predict([[70]]))

Prediction for age 40: [1]

Prediction for age 30: [0]

Prediction for age 70: [1]

import seaborn as sns

sns.regplot(x=X, y=y, data=df, logistic=True, ci=None)


RESULT:
EX NO: DECISION TRESS

DATE:

AIM:

ALGORITHM:
PROGRAM AND OUTPUT:

import pandas as pd

from sklearn.tree import DecisionTreeClassifier

from sklearn.model_selection import train_test_split

from sklearn import metrics

path='/content/drive/MyDrive/diabetes.csv'

df=pd.read_csv(path)

df.head()

df.shape

(768, 9)

feature_col=['Pregnancies','Glucose','BloodPressure','SkinThickness','Insulin','BMI','DiabetesPedigr
eeFunction','Age','Outcome']

X = df[feature_col]

Y = df.Outcome

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.3, random_state=1)

model = DecisionTreeClassifier()

model = model.fit(X_train, Y_train)

Y_pred = model.predict(X_test)

print("Accuracy: ",metrics.accuracy_score(Y_test,Y_pred))

Accuracy: 1.0

from sklearn.tree import export_graphviz

from six import StringIO

from IPython.display import Image

import pydotplus
dot_data = StringIO()

export_graphviz(model, out_file=dot_data,

filled=True, rounded=True,

special_characters=True,feature_names = feature_col,class_names=['0','1'])

graph = pydotplus.graph_from_dot_data(dot_data.getvalue())

graph.write_png('diabetes.png')

Image(graph.create_png())

path='/content/drive/MyDrive/AIML LAB/DATASET - AIML/IRIS.csv'

df=pd.read_csv(path)

df.head()

df.shape

(150, 5)
feature_col=['sepal_length', 'sepal_width', 'petal_length', 'petal_width']

X=df[feature_col]

Y=df.species

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.3, random_state=1)

model = DecisionTreeClassifier()

model = model.fit(X_train, Y_train)

Y_pred = model.predict(X_test)

print("Accuracy: ",metrics.accuracy_score(Y_test,Y_pred))

Accuracy: 0.9555555555555556 from sklearn.tree import export_graphviz

from six import StringIO

from IPython.display import Image

import pydotplus

dot_data = StringIO()

export_graphviz(model, out_file=dot_data,

filled=True, rounded=True,

special_characters=True,feature_names = feature_col,class_names=['setosa',
'versicolor', 'virginica'])

graph = pydotplus.graph_from_dot_data(dot_data.getvalue())

graph.write_png('diabetes.png')

Image(graph.create_png())
RESULT:
EX NO: RANDOM FORESTS

DATE:

AIM:

ALGORITHM:
PROGRAM AND OUTPUT:

from sklearn.datasets import make_classification, make_regression

from sklearn.model_selection import train_test_split

from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor

from sklearn.tree import DecisionTreeClassifier

from sklearn.metrics import accuracy_score, mean_squared_error, r2_score

X_class, y_class = make_classification(n_samples=1000, n_features=20, n_classes=2, random_state=42)

X_train_class, X_test_class, y_train_class, y_test_class = train_test_split(X_class, y_class, test_size=0.1,


random_state=23)

rf_class = RandomForestClassifier()

rf_class.fit(X_train_class, y_train_class)

y_pred_class = rf_class.predict(X_test_class)

print("Classification Accuracy Score:", accuracy_score(y_test_class, y_pred_class))

X_reg, y_reg = make_regression(n_samples=1000, n_features=20, noise=0.1, random_state=42)

X_train_reg, X_test_reg, y_train_reg, y_test_reg = train_test_split(X_reg, y_reg, test_size=0.1,


random_state=23)

rf_reg = RandomForestRegressor()

rf_reg.fit(X_train_reg, y_train_reg)

y_pred_reg = rf_reg.predict(X_test_reg)

print("Regression R² Score:", r2_score(y_test_reg, y_pred_reg))

print("Regression Mean Squared Error:", mean_squared_error(y_test_reg, y_pred_reg))

Classification Accuracy Score: 0.89

Regression R² Score: 0.8208256157278803

Regression Mean Squared Error: 5195.092545779625


RESULT:
EX NO: SVM MODELS

DATE:

AIM:

ALGORITHM:
PROGRAM AND OUTPUT:

import pandas as pd

path = '/content/drive/MyDrive/AIML LAB/DATASET - AIML/winequality-red.csv'

df = pd.read_csv(path)

df.head(5)

from sklearn.model_selection import train_test_split

X=df[['fixed acidity', 'volatile acidity', 'citric acid', 'residual sugar', 'chlorides', 'free sulfur dioxide',
'total sulfur dioxide', 'density', 'pH', 'sulphates', 'alcohol']] # Features

y=df['quality']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20)

from sklearn.svm import SVC

model=SVC()

model.fit(X_train, y_train)

y_pred=model.predict(X_test)

from sklearn.metrics import classification_report, confusion_matrix

print(confusion_matrix(y_test,y_pred))
[[ 0 0 1 3 0 0]

[ 0 0 3 8 0 0]

[ 0 0 67 76 0 0]

[ 0 0 19 103 0 0]

[ 0 0 2 35 0 0]

[ 0 0 1 2 0 0]]

print(classification_report(y_test, y_pred))

precision recall f1-score support

3 0.00 0.00 0.00 4

4 0.00 0.00 0.00 11

5 0.72 0.47 0.57 143

6 0.45 0.84 0.59 122

7 0.00 0.00 0.00 37

8 0.00 0.00 0.00 3

accuracy 0.53 320

macro avg 0.20 0.22 0.19 320

weighted avg 0.49 0.53 0.48 320

data_p=pd.DataFrame({'Actual':y_test, 'Predicted':y_pred})

data_p
from sklearn import metrics

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

Accuracy: 0.53125

import pandas as pd

from sklearn.model_selection import train_test_split

from sklearn.svm import SVC

from sklearn.metrics import classification_report, confusion_matrix

from sklearn import metrics

import numpy as np

import matplotlib.pyplot as plt

from sklearn.datasets import make_blobs

X, y = make_blobs(n_samples=100, centers=2, random_state=0, cluster_std=1.0)

model_viz = SVC(kernel='linear', C=1)

model_viz.fit(X, y)
plt.figure(figsize=(8, 6))

plt.scatter(X[:, 0], X[:, 1], c=y, s=30, cmap='viridis')

ax = plt.gca()

xlim = ax.get_xlim()

ylim = ax.get_ylim()

xx = np.linspace(xlim[0], xlim[1], 100)

yy = np.linspace(ylim[0], ylim[1], 100)

YY, XX = np.meshgrid(yy, xx)

xy = np.vstack([XX.ravel(), YY.ravel()]).T

Z = model_viz.decision_function(xy).reshape(XX.shape)

ax.contour(XX, YY, Z, colors='k', levels=[-1, 0, 1], alpha=0.5, linestyles=['--', '-', '--'])

ax.scatter(model_viz.support_vectors_[:, 0], model_viz.support_vectors_[:, 1], s=100,


linewidth=1, facecolors='none', edgecolors='k')

plt.title('SVM Decision Boundary')

plt.xlabel('Feature 1')

plt.ylabel('Feature 2')

plt.show()
RESULT:
EX NO: ENSEMBLING TECHNIQUES: CLASSIFIER

DATE:

AIM:

ALGORITHM:
PROGRAM AND OUTPUT:

import pandas as pd
import numpy as np
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
import statistics as st
import warnings
warnings.filterwarnings('ignore')

path = '/content/drive/MyDrive/AIML/DATASET - AIML/PlayTennis.csv'


df = pd.read_csv(path)
df.head()

Outlook Temperature Humidity Wind PlayTennis

0 Sunny Hot High Weak No

1 Sunny Hot High Strong No

2 Overcast Hot High Weak Yes

3 Rain Mild High Weak Yes

4 Rain Cool NormalWeak Yes

X = df.drop('PlayTennis', axis = 1)
y = df['PlayTennis']

categorical_features = ['Outlook', 'Temperature', 'Humidity', 'Wind']


X = pd.get_dummies(df.drop('PlayTennis', axis=1), columns=categorical_features)
y = df['PlayTennis']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20)

from sklearn.ensemble import VotingClassifier


from sklearn import tree
model1 = LogisticRegression(random_state=1)
model2 = tree.DecisionTreeClassifier(random_state=1)
model = VotingClassifier(estimators=[('lr', model1), ('dt', model2)], voting='hard')
model.fit(X_train,y_train)
print("Accuracy of the model")
model.score(X_test,y_test)

Accuracy of the model

0.6666666666666666
RESULT:
EX NO: ENSEMBLING TECHNIQUES: REGRESSION

DATE:

AIM:

ALGORITHM:
PROGRAM AND OUTPUT:

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error

from sklearn.ensemble import RandomForestRegressor


import xgboost as xgb
from sklearn.linear_model import LinearRegression

path = '/content/drive/MyDrive/AIML/DATASET - AIML/car_price.csv'


df = pd.read_csv(path)
df.head(3)

df.shape

(100, 13)

#print(df['Brand'].drop_duplicates())
Brand_c = df['Brand'].unique() # c:categorical
print(Brand_c)

['Toyota' 'Honda' 'Ford' 'Maruti' 'Hyundai' 'Tata' 'Mahindra' 'Volkswagen'


'Audi' 'BMW' 'Mercedes']

# Label Encoding
from sklearn import preprocessing
label_encoder = preprocessing.LabelEncoder()
df['Brand']= label_encoder.fit_transform(df['Brand'])
Brand_l = df['Brand'].unique() # Brand_l: after label coding
print(Brand_l)

[ 9 3 2 6 4 8 5 10 0 1 7]

Model_c = df['Model'].unique() # c:categorical


print(Model_c)

['Corolla' 'Civic' 'Mustang' 'Swift' 'Sonata' 'Nexon' 'Scorpio' 'Polo'


'A4' 'X1' 'C-Class' 'Endeavour' 'Creta' 'Harrier' 'Ertiga' 'City'
'Tiguan' 'Q3' '5 Series' 'GLC' 'Innova' 'Figo' 'Verna' 'Altroz' 'Thar'
'Passat' 'A6' 'X3' 'E-Class' 'Fortuner' 'Aspire' 'Elantra' 'Safari'
'Vitara' 'WR-V' 'Ameo' 'A3' '7 Series' 'GLE' 'Yaris' 'Ranger' 'Santro'
'Tigor' 'S-Cross' 'BR-V' 'T-Roc' 'Q7' 'X5' 'GLA' 'Camry' 'Venue' 'Tiago'
'XUV300' 'Vento' 'A5' '3 Series' 'Innova Crysta' 'EcoSport']

df['Model']= label_encoder.fit_transform(df['Model'])
Model_l = df['Model'].unique()
print(pd.DataFrame({'Model_c':Model_c, 'Model_l':Model_l}))
Model_c Model_l
0 Corolla 15
1 Civic 14
2 Mustang 30
3 Swift 42
4 Sonata 41
5 Nexon 31
6 Scorpio 40
7 Polo 33
8 A4 4
9 X1 53
10 C-Class 11
11 Endeavour 20
12 Creta 16
13 Harrier 27
14 Ertiga 21
15 City 13
16 Tiguan 47
17 Q3 34
18 5 Series 1
19 GLC 25
20 Innova 28
21 Figo 22
22 Verna 50
23 Altroz 7
24 Thar 44
25 Passat 32
26 A6 6
27 X3 54
28 E-Class 17
29 Fortuner 23
30 Aspire 9
31 Elantra 19
32 Safari 38
33 Vitara 51
34 WR-V 52
35 Ameo 8
36 A3 3
37 7 Series 2
38 GLE 26
39 Yaris 57
40 Ranger 36
41 Santro 39
42 Tigor 46
43 S-Cross 37
44 BR-V 10
45 T-Roc 43
46 Q7 35
47 X5 55
48 GLA 24
49 Camry 12
50 Venue 49

FuelType_c = df['Fuel_Type'].unique() # c:categorical


print(FuelType_c)

['Petrol' 'Diesel']

df['Fuel_Type']= label_encoder.fit_transform(df['Fuel_Type'])
FuelType_l = df['Fuel_Type'].unique()
print(pd.DataFrame({'FuelType_c':FuelType_c, 'FuelType_l':FuelType_l}))

FuelType_c FuelType_l
0 Petrol 1
1 Diesel 0

Transmission_c = df['Transmission'].unique() # c:categorical


print(Transmission_c)

['Manual' 'Automatic']

df['Transmission']= label_encoder.fit_transform(df['Transmission'])
Transmission_l = df['Transmission'].unique()
print(pd.DataFrame({'Transmission_c':Transmission_c, 'Transmission_l':Transmission_l}))

Transmission_c Transmission_l
0 Manual 1
1 Automatic 0

OwnerType_c = df['Owner_Type'].unique() # c:categorical


print(OwnerType_c)

['First' 'Second' 'Third']

df['Owner_Type']= label_encoder.fit_transform(df['Owner_Type'])
OwnerType_l = df['Owner_Type'].unique()
print(pd.DataFrame({'OwnerType_c':OwnerType_c, 'OwnerType_l':OwnerType_l}))

OwnerType_c OwnerType_l
0 First 0
1 Second 1
2 Third 2

# All numeric now


data = pd.DataFrame({'Car_ID':df['Car_ID'], 'Brand_l':df['Brand'], 'Model_l':df['Model'],
'Year':df['Year'], 'FuelType_l':df['Fuel_Type'], 'Kilometers_Driven':df['Kilometers_Driven'],
'Transmission_l':df['Transmission'], 'OwnerType_l':df['Owner_Type'], 'Mileage':df['Mileage'],
'Engine':df['Engine'], 'Power':df['Power'], 'Seats':df['Seats'], 'Price':df['Price']})
print(data.head())

Car_ID Brand_l Model_l Year FuelType_l Kilometers_Driven \


0 1 9 15 2018 1 50000
1 2 3 14 2019 1 40000
2 3 2 30 2017 1 20000
3 4 6 42 2020 0 30000
4 5 4 41 2016 0 60000

Transmission_l OwnerType_l Mileage Engine Power Seats Price


0 1 0 15 1498 108 5 800000
1 0 1 17 1597 140 5 1000000
2 0 0 10 4951 395 4 2500000
3 1 2 23 1248 74 5 600000
4 0 1 18 1999 194 5 850000

# Class Label
y = data["Price"]

# Feature set
X = data[['Car_ID', 'Brand_l', 'Model_l', 'Year', 'Kilometers_Driven', 'FuelType_l', 'Transmission_l',
'OwnerType_l', 'Mileage', 'Engine', 'Power', 'Seats']]

# Splitting between train data into training and validation dataset


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20)

# Ensemble Technique 1: Averaging


# initializing all the model objects with default parameters
model_1 = LinearRegression()
model_2 = xgb.XGBRegressor()
model_3 = RandomForestRegressor()

# training all the model on the training dataset


model_1.fit(X_train, y_train)
model_2.fit(X_train, y_train)
model_3.fit(X_train, y_train)

# predicting the output on the validation dataset


pred_1 = model_1.predict(X_test)
pred_2 = model_2.predict(X_test)
pred_3 = model_3.predict(X_test)

# final prediction after averaging on the prediction of all 3 models


pred_Avg = (pred_1 + pred_2 + pred_3) / 3.0
print(pred_Avg)

# printing the mean squared error between real value and predicted value
print(mean_squared_error(y_test, pred_Avg))

[1057065.72186251 693253.88756693 1487553.87957339 3014477.22995055


1658753.14066076 1522064.84764398 3374138.91205966 992573.8221779
1590376.48699941 1645948.27482176 2370640.46202375 709934.90710531
1818576.3477759 444763.03108568 476314.36258506 1759354.70699398
1051660.64769018 340922.76534071 936840.32552158 2338542.73394866]
52091135211.45682

# Ensemble Technique 2: Bagging


from sklearn.ensemble import BaggingRegressor
from sklearn import tree
model = BaggingRegressor(tree.DecisionTreeRegressor(random_state=1))
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
print(pd.DataFrame({'y_test':y_test,'y_pred':y_pred}))
print(mean_squared_error(y_test, y_pred))
model.score(X_test, y_test)

y_test y_pred
62 1200000 805000.0
5 750000 725000.0
32 1300000 1450000.0
56 2800000 2960000.0
87 1800000 1990000.0
96 1400000 1400000.0
50 2700000 2690000.0
82 850000 820000.0
17 1900000 1605000.0
49 1800000 1990000.0
67 2500000 2050000.0
34 750000 760000.0
63 1600000 1900000.0
89 550000 540000.0
90 500000 505000.0
74 2000000 1960000.0
24 1200000 805000.0
41 450000 490000.0
22 850000 780000.0
9 2700000 2555000.0
42142500000.0

0.9258511480601742
# Ensemble Technique 3: Boosting
# ADABOOST

from sklearn.ensemble import AdaBoostRegressor


model = AdaBoostRegressor()
model.fit(X_train, y_train)
print(pd.DataFrame({'y_test':y_test,'y_pred':y_pred}))
print(mean_squared_error(y_test, y_pred))
model.score(X_test,y_test)

y_test y_pred
62 1200000 805000.0
5 750000 725000.0
32 1300000 1450000.0
56 2800000 2960000.0
87 1800000 1990000.0
96 1400000 1400000.0
50 2700000 2690000.0
82 850000 820000.0
17 1900000 1605000.0
49 1800000 1990000.0
67 2500000 2050000.0
34 750000 760000.0
63 1600000 1900000.0
89 550000 540000.0
90 500000 505000.0
74 2000000 1960000.0

42142500000.0

0.9189869247528335

RESULT:
EX NO: CLUSTERING ALGORITHM: KNN

DATE:

AIM:

ALGORITHM:
PROGRAM AND OUTPUT:

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

df = pd.read_csv(“/content/drive/MyDrive/AIML/DATASET-AIML/Social_Network_Ads.csv”)

print(df)
print(df.shape)

User ID Gender Age EstimatedSalary Purchased


0 15624510 Male 19 19000 0
1 15810944 Male 35 20000 0
2 15668575 Female 26 43000 0
3 15603246 Female 27 57000 0
4 15804002 Male 19 76000 0
.. ... ... ... ... ...
395 15691863 Female 46 41000 1
396 15706071 Male 51 23000 1
397 15654296 Female 50 20000 1
398 15755018 Male 36 33000 0
399 15594041 Female 49 36000 1

[400 rows x 5 columns]


(400, 5)

X = df.iloc[:, [1, 2, 3]].values


y = df.iloc[:, -1].values
print(len(X))

400

from sklearn.preprocessing import LabelEncoder


le = LabelEncoder()
X[:,0] = le.fit_transform(X[:,0])

from sklearn.model_selection import train_test_split


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.20, random_state = 0)

from sklearn.preprocessing import StandardScaler


sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
from sklearn.neighbors import KNeighborsClassifier
classifier = KNeighborsClassifier(n_neighbors = 5, metric = 'minkowski', p = 2)
classifier.fit(X_train, y_train)

KNeighborsClassifier()

y_pred = classifier.predict(X_test)

print(y_test,y_pred)

[0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 1 0 1 0 0 0 0 0 1 1 0 0 0 0
0010000100101100011001001010100001001
0 0 0 0 1 1] [0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 0 1 0 0 1 0 1 0 1 0 0 0 0 0 0 1 0 0 0 0
0010000100101100111001001010100001001
0 0 0 0 1 1]

from sklearn.metrics import confusion_matrix,accuracy_score


cm = confusion_matrix(y_test, y_pred)
ac = accuracy_score(y_test,y_pred)
print(cm)
print(ac)

[[55 3]
[ 1 21]]

RESULT:
EX NO: CLUSTERING ALGORITHM: K MEANS

DATE:

AIM:

ALGORITHM:
PROGRAM AND OUTPUT:

import matplotlib.pyplot as plt

x = [4, 5, 10, 4, 3, 11, 14 , 6, 10, 12]


y = [21, 19, 24, 17, 16, 25, 24, 22, 21, 21]

plt.scatter(x, y)
plt.show()

data = list(zip(x, y))


print(data)

[(4, 21), (5, 19), (10, 24), (4, 17), (3, 16), (11, 25), (14, 24), (6, 22), (10, 21), (12, 21)]

from sklearn.cluster import KMeans

inertias = []

for i in range(1,11):
kmeans = KMeans(n_clusters=i)
kmeans.fit(data)
inertias.append(kmeans.inertia_)

plt.plot(range(1,11), inertias, marker='o')


plt.title('Elbow method')
plt.xlabel('Number of clusters')
plt.ylabel('Inertia')
plt.show()
kmeans = KMeans(n_clusters=2)
kmeans.fit(data)

plt.scatter(x, y, c=kmeans.labels_)
plt.show()

RESULT:
EX NO: EM FOR BAYESIAN NETWORKS

DATE:

AIM:

ALGORITHM:
PROGRAM AND OUTPUT:

import numpy as np

import matplotlib.pyplot as plt

from scipy.stats import norm

from scipy.stats import gaussian_kde

import seaborn as sns

# Generate a dataset with two Gaussian components

mu1, sigma1 = 2, 1

mu2, sigma2 = -1, 0.8

X1 = np.random.normal(mu1, sigma1, size=200)

X2 = np.random.normal(mu2, sigma2, size=600)

X = np.concatenate([X1, X2])

# Plot the density estimation using seaborn

sns.kdeplot(X)

plt.xlabel('X')

plt.ylabel('Density')

plt.title('Density Estimation of X')

plt.show()
import numpy as np

import matplotlib.pyplot as plt

from scipy.stats import norm

# Initialize parameters

mu1_hat, sigma1_hat = np.mean(X1), np.std(X1)

mu2_hat, sigma2_hat = np.mean(X2), np.std(X2)

pi1_hat, pi2_hat = len(X1) / len(X), len(X2) / len(X)

# Perform EM algorithm for 20 epochs

num_epochs = 20

log_likelihoods = []

for epoch in range(num_epochs):

# E-step: Compute responsibilities

gamma1 = pi1_hat * norm.pdf(X, mu1_hat, sigma1_hat)

gamma2 = pi2_hat * norm.pdf(X, mu2_hat, sigma2_hat)

total = gamma1 + gamma2

gamma1 /= total

gamma2 /= total

# M-step: Update parameters

mu1_hat = np.sum(gamma1 * X) / np.sum(gamma1)

mu2_hat = np.sum(gamma2 * X) / np.sum(gamma2)

sigma1_hat = np.sqrt(np.sum(gamma1 * (X - mu1_hat)**2) / np.sum(gamma1))

sigma2_hat = np.sqrt(np.sum(gamma2 * (X - mu2_hat)**2) / np.sum(gamma2))

pi1_hat = np.mean(gamma1)

pi2_hat = np.mean(gamma2)

# Compute log-likelihood

log_likelihood = np.sum(np.log(pi1_hat * norm.pdf(X, mu1_hat, sigma1_hat) + pi2_hat *

norm.pdf(X, mu2_hat, sigma2_hat)))

log_likelihoods.append(log_likelihood)

# Plot log-likelihood values over epochs


plt.plot(range(1, num_epochs+1), log_likelihoods)

plt.xlabel('Epoch')

plt.ylabel('Log-Likelihood')

plt.title('Log-Likelihood vs. Epoch')

plt.show()

from scipy.stats import gaussian_kde

# Plot the final estimated density

X_sorted = np.sort(X)

density_estimation = pi1_hat * norm.pdf(X_sorted, mu1_hat, sigma1_hat) + pi2_hat *

norm.pdf(X_sorted, mu2_hat, sigma2_hat)

plt.plot(X_sorted, gaussian_kde(X)(X_sorted), color='green', linewidth=2)

plt.plot(X_sorted, density_estimation, color='red', linewidth=2)

plt.xlabel('X')

plt.ylabel('Density')

plt.title('Density Estimation of X')

plt.legend(['Kernel Density Estimation', 'Mixture Density'])

plt.show()
RESULT:
EX NO: SIMPLE NN MODELS

DATE:

AIM:

ALGORITHM:
PROGRAM AND OUTPUT:

import numpy as np
import pandas as pd
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense
tf.__version__

{"type":"string"}

path =' /content/drive/MyDrive/AIML/DATASET - AIML/Churn_Modelling.csv '


df = pd.read_csv(path)
df.head(3)

RowNumber CustomerId Surname CreditScore Geography Gender Age


Tenure Balance NumOfProducts HasCrCard IsActiveMember
EstimatedSalary Exited

0 1 15634602 Hargrave 619 France Female 42 2 0.00 1


1 1 101348.88 1

1 2 15647311 Hill 608 Spain Female 41 1 83807.86 1


0 1 112542.58 0

2 3 15619304 Onio 502 France Female 42 8 159660.80 3


1 0 113931.57 1

df.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 10000 entries, 0 to 9999
Data columns (total 14 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 RowNumber 10000 non-null int64
1 CustomerId 10000 non-null int64
2 Surname 10000 non-null object
3 CreditScore 10000 non-null int64
4 Geography 10000 non-null object
5 Gender 10000 non-null object
6 Age 10000 non-null int64
7 Tenure 10000 non-null int64
8 Balance 10000 non-null float64
9 NumOfProducts 10000 non-null int64
10 HasCrCard 10000 non-null int64
11 IsActiveMember 10000 non-null int64
12 EstimatedSalary 10000 non-null float64
13 Exited 10000 non-null int64
dtypes: float64(2), int64(9), object(3)
memory usage: 1.1+ MB

X=df.iloc[:, 3:-1].values
y=df.iloc[:, -1].values
print(X)
print(y)

[[619 'France' 'Female' ... 1 1 101348.88]


[608 'Spain' 'Female' ... 0 1 112542.58]
[502 'France' 'Female' ... 1 0 113931.57]
...
[709 'France' 'Female' ... 0 1 42085.58]
[772 'Germany' 'Male' ... 1 0 92888.52]
[792 'France' 'Female' ... 1 0 38190.78]]
[1 0 1 ... 1 1 0]

#Encoding categorical data


#Label Encoding the "Gender" column
from sklearn.preprocessing import LabelEncoder
le=LabelEncoder()
X[:, 2]=le.fit_transform(X[:,2])
print(X)

[[619 'France' 0 ... 1 1 101348.88]


[608 'Spain' 0 ... 0 1 112542.58]
[502 'France' 0 ... 1 0 113931.57]
...
[709 'France' 0 ... 0 1 42085.58]
[772 'Germany' 1 ... 1 0 92888.52]
[792 'France' 0 ... 1 0 38190.78]]

#One Hot Encoding the "Geography" column


from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
ct = ColumnTransformer(transformers=[('encoder', OneHotEncoder(), [1])],
remainder='passthrough')
X = np.array(ct.fit_transform(X))
print(X)
# encoded_data will be an array with the one-hot encoded representation of the categorical
variable
[[1.0 0.0 0.0 ... 1 1 101348.88]
[0.0 0.0 1.0 ... 0 1 112542.58]
[1.0 0.0 0.0 ... 1 0 113931.57]
...
[1.0 0.0 0.0 ... 0 1 42085.58]
[0.0 1.0 0.0 ... 1 0 92888.52]
[1.0 0.0 0.0 ... 1 0 38190.78]]

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)

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

#Building the ANN


#Initializing the ANN
ann = tf.keras.models.Sequential()

#Adding the input layer and the first hidden layer


ann.add(tf.keras.layers.Dense(units=6, activation='relu'))

#Adding the second hidden layer


ann.add(tf.keras.layers.Dense(units=6, activation='relu'))

#Adding the output layer


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

#Compiling the ANN


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

#Training the ANN on the Training set


ann.fit(X_train, y_train, batch_size = 32, epochs = 100)

Epoch 1/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 4s 4ms/step - accuracy: 0.7984 - loss:
0.5569
Epoch 2/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.7957 - loss:
0.4642
Epoch 3/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.7962 - loss:
0.4495
Epoch 4/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.7941 - loss:
0.4365
Epoch 5/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.7948 - loss:
0.4449
Epoch 6/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - accuracy: 0.8011 - loss:
0.4261
Epoch 7/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.7969 - loss:
0.4341
Epoch 8/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8093 - loss:
0.4222
Epoch 9/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8112 - loss:
0.4314
Epoch 10/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 3ms/step - accuracy: 0.8187 - loss:
0.4205
Epoch 11/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 3ms/step - accuracy: 0.8206 - loss:
0.4079
Epoch 12/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 3ms/step - accuracy: 0.8228 - loss:
0.4037
Epoch 13/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8153 - loss:
0.4113
Epoch 14/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8185 - loss:
0.4078
Epoch 15/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8205 - loss:
0.4091
Epoch 16/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8256 - loss:
0.4032
Epoch 17/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8281 - loss:
0.3939
Epoch 18/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8375 - loss:
0.3902
Epoch 19/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8285 - loss:
0.3988
Epoch 20/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8415 - loss:
0.3791
Epoch 21/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8419 - loss:
0.3844
Epoch 22/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8456 - loss:
0.3760
Epoch 23/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8471 - loss:
0.3657
Epoch 24/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8568 - loss:
0.3605
Epoch 25/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8570 - loss:
0.3438
Epoch 26/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8556 - loss:
0.3488
Epoch 27/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8465 - loss:
0.3560
Epoch 28/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8561 - loss:
0.3550
Epoch 29/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8602 - loss:
0.3424
Epoch 30/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.8563 - loss:
0.3561
Epoch 31/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 3ms/step - accuracy: 0.8606 - loss:
0.3404
Epoch 32/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.8612 - loss:
0.3388
Epoch 33/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8676 - loss:
0.3251
Epoch 34/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8592 - loss:
0.3437
Epoch 35/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8603 - loss:
0.3438
Epoch 36/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8645 - loss:
0.3438
Epoch 37/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8681 - loss:
0.3344
Epoch 38/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8619 - loss:
0.3438
Epoch 39/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8614 - loss:
0.3416
Epoch 40/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8651 - loss:
0.3367
Epoch 41/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8631 - loss:
0.3348
Epoch 42/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8571 - loss:
0.3488
Epoch 43/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8591 - loss:
0.3417
Epoch 44/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - accuracy: 0.8581 - loss:
0.3417
Epoch 45/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8632 - loss:
0.3343
Epoch 46/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8608 - loss:
0.3418
Epoch 47/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8608 - loss:
0.3418
Epoch 48/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8657 - loss:
0.3341
Epoch 49/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8647 - loss:
0.3335
Epoch 50/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 3ms/step - accuracy: 0.8591 - loss:
0.3367
Epoch 51/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 3ms/step - accuracy: 0.8610 - loss:
0.3492
Epoch 52/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 3ms/step - accuracy: 0.8626 - loss:
0.3373
Epoch 53/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8647 - loss:
0.3338
Epoch 54/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8632 - loss:
0.3304
Epoch 55/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8608 - loss:
0.3381
Epoch 56/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8540 - loss:
0.3467
Epoch 57/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8581 - loss:
0.3466
Epoch 58/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - accuracy: 0.8565 - loss:
0.3452
Epoch 59/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8664 - loss:
0.3379
Epoch 60/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - accuracy: 0.8612 - loss:
0.3429
Epoch 61/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - accuracy: 0.8690 - loss:
0.3258
Epoch 62/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - accuracy: 0.8603 - loss:
0.3418
Epoch 63/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8645 - loss:
0.3390
Epoch 64/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8574 - loss:
0.3419
Epoch 65/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - accuracy: 0.8597 - loss:
0.3426
Epoch 66/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8700 - loss:
0.3288
Epoch 67/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8624 - loss:
0.3388
Epoch 68/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8657 - loss:
0.3301
Epoch 69/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8609 - loss:
0.3402
Epoch 70/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 3ms/step - accuracy: 0.8734 - loss:
0.3198
Epoch 71/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 3ms/step - accuracy: 0.8664 - loss:
0.3340
Epoch 72/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 3ms/step - accuracy: 0.8657 - loss:
0.3323
Epoch 73/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8647 - loss:
0.3363
Epoch 74/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8671 - loss:
0.3336
Epoch 75/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8651 - loss:
0.3367
Epoch 76/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8602 - loss:
0.3431
Epoch 77/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8637 - loss:
0.3347
Epoch 78/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8652 - loss:
0.3326
Epoch 79/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8639 - loss:
0.3244
Epoch 80/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8654 - loss:
0.3303
Epoch 81/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8598 - loss:
0.3403
Epoch 82/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8694 - loss:
0.3307
Epoch 83/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8588 - loss:
0.3399
Epoch 84/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8629 - loss:
0.3320
Epoch 85/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8683 - loss:
0.3248
Epoch 86/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8691 - loss:
0.3348
Epoch 87/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8625 - loss:
0.3360
Epoch 88/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8671 - loss:
0.3346
Epoch 89/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 3ms/step - accuracy: 0.8631 - loss:
0.3424
Epoch 90/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 3ms/step - accuracy: 0.8672 - loss:
0.3263
Epoch 91/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 3ms/step - accuracy: 0.8629 - loss:
0.3347
Epoch 92/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8616 - loss:
0.3391
Epoch 93/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8658 - loss:
0.3354
Epoch 94/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - accuracy: 0.8686 - loss:
0.3255
Epoch 95/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8617 - loss:
0.3353
Epoch 96/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8607 - loss:
0.3451
Epoch 97/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8618 - loss:
0.3402
Epoch 98/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8652 - loss:
0.3317
Epoch 99/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8641 - loss:
0.3331
Epoch 100/100
250/250 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8722 - loss:
0.3193

<keras.src.callbacks.history.History at 0x7f8d06a5a750>

print(ann.predict(sc.transform([[1, 0, 0, 600, 1, 40, 3, 60000, 2, 1, 1, 50000]])) > 0.5)

1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 110ms/step


[[False]]

Therefore, our ANN model predicts that this customer stays in the bank!

#Predicting the Test set results


y_pred = ann.predict(X_test)
y_pred = (y_pred > 0.5)
print(np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1))

63/63 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step


[[0 0]
[0 1]
[0 0]
...
[0 0]
[0 0]
[0 0]]

#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)

[[1503 92]
[ 188 217]]

0.86

RESULT:
EX NO: DEEP LEARNING NN MODELS

DATE:

AIM:

ALGORITHM:
PROGRAM AND OUTPUT:

import numpy as np
import keras
from keras import layers

# Model / data parameters


num_classes = 10
input_shape = (28, 28, 1)

# Load the data and split it between train and test sets
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()

# Scale images to the [0, 1] range


x_train = x_train.astype("float32") / 255
x_test = x_test.astype("float32") / 255
# Make sure images have shape (28, 28, 1)
x_train = np.expand_dims(x_train, -1)
x_test = np.expand_dims(x_test, -1)
print("x_train shape:", x_train.shape)
print(x_train.shape[0], "train samples")
print(x_test.shape[0], "test samples")

# convert class vectors to binary class matrices


y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

x_train shape: (60000, 28, 28, 1)


60000 train samples
10000 test samples

model = keras.Sequential(
[
keras.Input(shape=input_shape),
layers.Conv2D(32, kernel_size=(3, 3), activation="relu"),
layers.MaxPooling2D(pool_size=(2, 2)),
layers.Conv2D(64, kernel_size=(3, 3), activation="relu"),
layers.MaxPooling2D(pool_size=(2, 2)),
layers.Flatten(),
layers.Dropout(0.5),
layers.Dense(num_classes, activation="softmax"),
]
)

model.summary()
Model: "sequential"

Layer (Type) Output Shape Param #

Conv2D (conv2d) (None, 26, 26, 32) 320

MaxPooling2D (max_pooling2d) (None, 13, 13, 32) 0

Conv2D (conv2d_1) (None, 11, 11, 64) 18,496

MaxPooling2D (max_pooling2d_1) (None, 5, 5, 64) 0

Flatten (flatten) (None, 1600) 0

Dropout (dropout) (None, 1600) 0

Dense (dense) (None, 10) 16,010

Total params: 34,826 (136.04 KB)

Trainable params: 34,826 (136.04 KB)

Non-trainable params: 0 (0.00 B)

# Train the model


batch_size = 128
epochs = 15

model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])

model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=0.1)

Epoch 1/15
422/422 ━━━━━━━━━━━━━━━━━━━━ 48s 109ms/step - accuracy: 0.7681 -
loss: 0.7563 - val_accuracy: 0.9772 - val_loss: 0.0895
Epoch 2/15
422/422 ━━━━━━━━━━━━━━━━━━━━ 78s 102ms/step - accuracy: 0.9606 -
loss: 0.1286 - val_accuracy: 0.9840 - val_loss: 0.0600
Epoch 3/15
422/422 ━━━━━━━━━━━━━━━━━━━━ 88s 116ms/step - accuracy: 0.9731 -
loss: 0.0862 - val_accuracy: 0.9868 - val_loss: 0.0475
Epoch 4/15
422/422 ━━━━━━━━━━━━━━━━━━━━ 76s 102ms/step - accuracy: 0.9773 -
loss: 0.0727 - val_accuracy: 0.9875 - val_loss: 0.0450
Epoch 5/15
422/422 ━━━━━━━━━━━━━━━━━━━━ 80s 98ms/step - accuracy: 0.9809 -
loss: 0.0609 - val_accuracy: 0.9890 - val_loss: 0.0396
Epoch 6/15
422/422 ━━━━━━━━━━━━━━━━━━━━ 83s 100ms/step - accuracy: 0.9830 -
loss: 0.0571 - val_accuracy: 0.9878 - val_loss: 0.0388
Epoch 7/15
422/422 ━━━━━━━━━━━━━━━━━━━━ 81s 99ms/step - accuracy: 0.9823 -
loss: 0.0561 - val_accuracy: 0.9890 - val_loss: 0.0390
Epoch 8/15
422/422 ━━━━━━━━━━━━━━━━━━━━ 82s 100ms/step - accuracy: 0.9853 -
loss: 0.0474 - val_accuracy: 0.9910 - val_loss: 0.0334
Epoch 9/15
422/422 ━━━━━━━━━━━━━━━━━━━━ 83s 102ms/step - accuracy: 0.9856 -
loss: 0.0455 - val_accuracy: 0.9907 - val_loss: 0.0341
Epoch 10/15
422/422 ━━━━━━━━━━━━━━━━━━━━ 82s 103ms/step - accuracy: 0.9867 -
loss: 0.0421 - val_accuracy: 0.9918 - val_loss: 0.0320
Epoch 11/15
422/422 ━━━━━━━━━━━━━━━━━━━━ 80s 100ms/step - accuracy: 0.9880 -
loss: 0.0386 - val_accuracy: 0.9913 - val_loss: 0.0311
Epoch 12/15
422/422 ━━━━━━━━━━━━━━━━━━━━ 82s 100ms/step - accuracy: 0.9879 -
loss: 0.0378 - val_accuracy: 0.9910 - val_loss: 0.0297
Epoch 13/15
422/422 ━━━━━━━━━━━━━━━━━━━━ 82s 99ms/step - accuracy: 0.9889 -
loss: 0.0351 - val_accuracy: 0.9923 - val_loss: 0.0273
Epoch 14/15
422/422 ━━━━━━━━━━━━━━━━━━━━ 82s 99ms/step - accuracy: 0.9889 -
loss: 0.0336 - val_accuracy: 0.9910 - val_loss: 0.0314
Epoch 15/15
422/422 ━━━━━━━━━━━━━━━━━━━━ 82s 99ms/step - accuracy: 0.9887 -
loss: 0.0337 - val_accuracy: 0.9922 - val_loss: 0.0285

<keras.src.callbacks.history.History at 0x79aaffd14510>

# Evaluate the trained model


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

Test loss: 0.024387754499912262

Test accuracy: 0.9909999966621399


RESULT:

You might also like