Final Aiml PDF
Final Aiml PDF
VASUDEVAN COLLEGE OF
ENGINEERING AND TECHNOLOGY
PONMAR,CHENNAI-600 048
DEPARTMENT OF CSE (CYBER SECURITY)
RECORD
NAME :
REGISTER NO.:
SEMESTER :
BRANCH :
SNO DATE TITLE SIGNATURE
7
Implementation of Ensembling Methods
AIM:
To implement the uninformed Search Algorithms like Breadth First Search using Python.
ALGORITHM
PROGRAM:
pq = PriorityQueue()
pq.put((0, actual_Src))
visited[actual_Src] = True
while pq.empty() == False:
u = pq.get()[1]
Reg.no:411622149007
for v, c in graph[u]:
if visited[v] == False:
visited[v] = True
pq.put((c, v))
print()
graph[y].append((x, cost))
# The nodes shown in above example(by alphabets) are
# implemented using integers addedge(x,y,cost);
addedge(0, 1, 3)
addedge(0, 2, 6)
addedge(0, 3, 5)
addedge(1, 4, 9)
addedge(1, 5, 8)
addedge(2, 6, 12)
addedge(2, 7, 14)
addedge(3, 8, 7)
addedge(8, 9, 5)
addedge(8, 10, 6)
addedge(9, 11, 1)
addedge(9, 12, 10)
addedge(9, 13, 2)
source = 0
target = 9
best_first_search(source, target, v)
Reg.no:411622149007
OUTPUT:
013289
RESULT:
Thus the Program for uninformed search Algorithm like Breadth first search was executed and
output has been verified successfully.
Reg.no:411622149007
AIM:
To implement the uninformed Search Algorithms like Depth First Search using Python.
ALGORITHM:
PROGRAM:
def dfs(node, graph, visited, component):
component.append(node) # Store answer
visited[node] = True # Mark visited
graph = {
0: [2],
1: [2, 3],
2: [0, 1, 4],
3: [1, 4],
4: [2, 3]
Reg.no:411622149007
}
node = 0 # Starting node
visited = [False]*len(graph) # Make all nodes to False initially
component = []
dfs(node, graph, visited, component) # Traverse to each node of a graph
print(f"Following is the Depth-first search: {component}") # Print the answer
Reg.no:411622149007
OUTPUT:
Following is the Depth-first search: [0, 2, 1, 3, 4]
RESULT:
Thus the Program for uninformed search Algorithm like Depth first search was executed and
output has been verified successfully.
Reg.no:411622149007
AIM:
To implement the Informed Search Algorithms like A* Algorithm using Python.
ALGORITHM:
PROGRAM:
class Graph:
self.adjac_lis = adjac_lis
return self.adjac_lis[v]
# This is heuristic function which is having equal values for all nodes
H={
Reg.no:411622149007
'A': 1,
'B': 1,
'C': 1,
'D': 1
return H[n]
# In this open_lst is a lisy of nodes which have been visited, but who's
# neighbours haven't all been always inspected, It starts off with the start
#node
open_lst = set([start])
closed_lst = set([])
poo = {}
poo[start] = 0
par = {}
par[start] = start
n = None
for v in open_lst:
n = v;
if n == None:
return None
if n == stop:
reconst_path = []
while par[n] != n:
reconst_path.append(n)
n = par[n]
reconst_path.append(start)
reconst_path.reverse()
return reconst_path
open_lst.add(m)
par[m] = n
else:
par[m] = n
if m in closed_lst:
closed_lst.remove(m)
open_lst.add(m)
open_lst.remove(n)
closed_lst.add(n)
return None
INPUT:
adjac_lis = {
graph1 = Graph(adjac_lis)
graph1.a_star_algorithm('A', 'D')
Reg.no:411622149007
OUTPUT:
RESULT:
Thus the Program for informed search Algorithm like A* was executed and output has been
verified successfully.
Reg.no:411622149007
AIM:
To implement the Navie Bayes Classifier using Python.
ALGORITHM:
PROGRAM:
# Compute accuracy
accuracy = 100.0 * (y == y_pred).sum() / X.shape[0]
print("Accuracy of Naive Bayes classifier =", round(accuracy, 2), "%")
# Defining the Visualizer
def visualize_classifier(classifier, X, y):
# Define the minimum and maximum values for X and Y
# that will be used in the mesh grid
min_x, max_x = X[:, 0].min() - 1.0, X[:, 0].max() + 1.0
min_y, max_y = X[:, 1].min() - 1.0, X[:, 1].max() + 1.0
#Creating a classifier
classifier_new = GaussianNB()
OUTPUT:
Accuracy: 99.75%
Precision: 99.76%
Recall: 99.75%
F1: 99.75%
RESULT:
Thus the Python Program for Navie Bayes Classifier was executed and output has been verified
successfully.
Reg.no:411622149007
AIM:
To implement the Bayesian Networks using Python.
ALGORITHM:
PROGRAM:
import torch
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 0.5, 1000)
ε = 0.02 * np.random.randn(x.shape[0])
y = x + 0.3 * np.sin(2 * np.pi * (x + ε)) + 0.3 * np.sin(4 * np.pi * (x
+ ε)) + ε
pyro.clear_param_store()
bar = trange(20000)
x_train = torch.from_numpy(x).float()
y_train = torch.from_numpy(y).float()
for epoch in bar:
loss = svi.step(x_train, y_train)
bar.set_postfix(loss=f'{loss / x.shape[0]:.3f}')
predictive = Predictive(model, guide=guide, num_samples=500)
x_test = torch.linspace(-0.5, 1, 3000)
preds = predictive(x_test)
y_pred = preds['obs'].T.detach().numpy().mean(axis=1)
y_std = preds['obs'].T.detach().numpy().std(axis=1)
OUTPUT:
RESULT:
Thus the Python Program for Bayesian Networks was executed and output has been verified
successfully.
Reg.no:411622149007
AIM:
To implement the Linear and Multiple Linear Regression using Python.
ALGORITHM:
PROGRAM:
Linear Regression
import numpy as np
import matplotlib.pyplot as plt
# putting labels
plt.xlabel('x')
plt.ylabel('y')
def main():
# observations / data
x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
y = np.array([1, 3, 2, 5, 7, 8, 8, 9, 10, 12])
# estimating coefficients
b = estimate_coef(x, y)
print("Estimated coefficients:\nb_0 = {} \
\nb_1 = {}".format(b[0], b[1]))
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
dataset = pd.read_csv('/content/50_Startups.csv')
dataset.head()
# data preprocessing
X = dataset.iloc[:,:-1].values
y = dataset.iloc[:,4].values
X = X[:, 1:]
y_test
y_pred
Reg.no:411622149007
OUTPUT:
Linear Regression
Estimated coefficients:
b_0 = 1.2363636363636363
b_1 = 1.1696969696969697
Multiple Regression:
array([103015.20159796, 132582.27760816, 132447.73845175,
71976.09851259, 178537.48221054, 116161.24230163, 67851.69209676,
98791.73374688, 113969.43533012, 167921.0656955 ])
RESULT:
Thus the Python Program for Regression models was executed and output has been verified
successfully.
Reg.no:411622149007
AIM:
To implement the decision tress using Python.
ALGORITHM:
PROGRAM:
# Python program to implement decision tree algorithm and plot the tree
# Plotting heatmap
sns.heatmap(matrix, annot = True, fmt = "g", ax = axis, cmap = "magma")
axis.set_title('Confusion Matrix')
axis.set_xlabel("Predicted Values", fontsize = 10)
axis.set_xticklabels([''] + target_labels)
axis.set_ylabel( "True Labels", fontsize = 10)
axis.set_yticklabels(list(target_labels), rotation = 0)
plt.show()
Reg.no:411622149007
OUTPUT:
Reg.no:411622149007
RESULT:
Thus the Python Program for Decision Tree was executed and output has been verified
successfully.
Reg.no:411622149007
AIM:
To implement the Random Forest Algorithm using Python.
ALGORITHM:
PROGRAM:
iris.shape
(150, 6)
iris.isnull().sum()
iris["Species"].unique()
Reg.no:411622149007
fig = iris[iris.Species=='Iris-
setosa'].plot(kind='scatter',x='SepalLengthCm',y='SepalWidthCm',color='
orange', label='Setosa')
iris[iris.Species=='Iris-
versicolor'].plot(kind='scatter',x='SepalLengthCm',y='SepalWidthCm',col
or='blue', label='versicolor',ax=fig)
iris[iris.Species=='Iris-
virginica'].plot(kind='scatter',x='SepalLengthCm',y='SepalWidthCm',colo
r='green', label='virginica', ax=fig)
fig.set_xlabel("Sepal Length")
fig.set_ylabel("Sepal Width")
fig.set_title("Sepal Length VS Width")
fig=plt.gcf()
fig.set_size_inches(10,6)
plt.show()
fig.savefig("Sepal Length VS Width.png")
Reg.no:411622149007
'''let us see how are the length and width are distributed'''
iris.hist(edgecolor='Yellow', linewidth=1.2)
fig=plt.gcf()
fig.set_size_inches(12,6)
plt.show()
'''Let us see how the length and width vary according to the species'''
plt.figure(figsize=(15,10))
plt.subplot(2,2,1)
sns.violinplot(x='Species',y='PetalLengthCm',data=iris)
plt.subplot(2,2,2)
Reg.no:411622149007
sns.violinplot(x='Species',y='PetalWidthCm',data=iris)
plt.subplot(2,2,3)
sns.violinplot(x='Species',y='SepalLengthCm',data=iris)
plt.subplot(2,2,4)
sns.violinplot(x='Species',y='SepalWidthCm',data=iris)
fig.savefig("variable with species.png")
y=iris.iloc[:, -1].values
y
array(['Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa',
'Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa',
'Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa',
'Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa',
'Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa',
'Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa',
'Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa',
'Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa',
'Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa',
'Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa',
'Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa',
'Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa',
'Iris-setosa', 'Iris-setosa', 'Iris-versicolor', 'Iris-versicolor',
'Iris-versicolor', 'Iris-versicolor', 'Iris-versicolor',
'Iris-versicolor', 'Iris-versicolor', 'Iris-versicolor',
'Iris-versicolor', 'Iris-versicolor', 'Iris-versicolor',
'Iris-versicolor', 'Iris-versicolor', 'Iris-versicolor',
'Iris-versicolor', 'Iris-versicolor', 'Iris-versicolor',
'Iris-versicolor', 'Iris-versicolor', 'Iris-versicolor',
'Iris-versicolor', 'Iris-versicolor', 'Iris-versicolor',
'Iris-versicolor', 'Iris-versicolor', 'Iris-versicolor',
'Iris-versicolor', 'Iris-versicolor', 'Iris-versicolor',
'Iris-versicolor', 'Iris-versicolor', 'Iris-versicolor',
'Iris-versicolor', 'Iris-versicolor', 'Iris-versicolor',
'Iris-versicolor', 'Iris-versicolor', 'Iris-versicolor',
'Iris-versicolor', 'Iris-versicolor', 'Iris-versicolor',
'Iris-versicolor', 'Iris-versicolor', 'Iris-versicolor',
'Iris-versicolor', 'Iris-versicolor', 'Iris-versicolor',
'Iris-versicolor', 'Iris-versicolor', 'Iris-versicolor',
'Iris-virginica', 'Iris-virginica', 'Iris-virginica',
'Iris-virginica', 'Iris-virginica', 'Iris-virginica',
'Iris-virginica', 'Iris-virginica', 'Iris-virginica',
'Iris-virginica', 'Iris-virginica', 'Iris-virginica',
'Iris-virginica', 'Iris-virginica', 'Iris-virginica',
'Iris-virginica', 'Iris-virginica', 'Iris-virginica',
'Iris-virginica', 'Iris-virginica', 'Iris-virginica',
'Iris-virginica', 'Iris-virginica', 'Iris-virginica',
'Iris-virginica', 'Iris-virginica', 'Iris-virginica',
'Iris-virginica', 'Iris-virginica', 'Iris-virginica',
'Iris-virginica', 'Iris-virginica', 'Iris-virginica',
'Iris-virginica', 'Iris-virginica', 'Iris-virginica',
'Iris-virginica', 'Iris-virginica', 'Iris-virginica',
'Iris-virginica', 'Iris-virginica', 'Iris-virginica',
'Iris-virginica', 'Iris-virginica', 'Iris-virginica',
Reg.no:411622149007
OUTPUT:
The accuracy of the Random forest is: 0.9
RESULT:
Thus the Random Forest Algorithm using Python was executed and output has been
verified successfully.
Reg.no:411622149007
AIM:
To implement the Support Vector Machine Models using Python.
ALGORITHM:
Step 1: Start the Program.
Step 2: Import the Libraries- import numpy as np import matplotlib.pyplot as plt import pandas
as pd.
Setp 3: Load the Dataset.
Step 4: Split Dataset into X and Y.
Step 5: Split the X and Y Dataset into the Training set and Test set.
Step 6: Perform Feature Scaling.
Step 7: Fit SVM to the Training set.
Step 8: Predict the Test Set Results.
Step 9: Make the Confusion Matrix
Step 10. Stop
PROGRAM:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
dataset = pd.read_csv('/content/Social_Network_Ads.csv')
X = dataset.iloc[:, [2, 3]].values
y = dataset.iloc[:, 4].values
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)
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
from sklearn.svm import SVC
classifier = SVC(kernel = 'rbf', random_state = 0)
classifier.fit(X_train, y_train)
y_pred = classifier.predict(X_test)
from sklearn.metrics import confusion_matrix, accuracy_score
cm = confusion_matrix(y_test, y_pred)
print(cm)
accuracy_score(y_test,y_pred)
from matplotlib.colors import ListedColormap
X_set, y_set = X_test, y_test
Reg.no:411622149007
OUTPUT:
Confusion Matrix
[[64 4]
[ 3 29]]
0.93
RESULT:
Thus the Support Vector Machine models using Python was executed and output has
been verified successfully.
Reg.no:411622149007
AIM:
To implement the Ensembling Methods like Bagging, Boosting and Voting Classifier
Algorithms using Python.
ALGORITHM:
Step 1: Start
Step 2: Split the train dataset into n parts
Step 3: The base model is then fitted on the whole train dataset.
Step 4: The Steps 2 to 4 are repeated for another base model which results in another set of
predictions for the train and test dataset.
Step 5: The predictions on train data set are used as a feature to build the new model.
Step 6: Stop
PROGRAM:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
sns.set_style("whitegrid")
plt.style.use("fivethirtyeight")
df = pd.read_csv("/content/diabetes.csv")
df.head()
df.info()
df.isnull().sum()
pd.set_option('display.float_format', '{:.2f}'.format)
df.describe()
categorical_val = []
continous_val = []
for column in df.columns:
# print('==============================')
# print(f"{column} : {df[column].unique()}")
if len(df[column].unique()) <= 10:
categorical_val.append(column)
else:
continous_val.append(column)
df.columns
# How many missing zeros are mising in each feature
feature_columns = [
Reg.no:411622149007
X = df[feature_columns]
y = df.Outcome
tree = DecisionTreeClassifier()
bagging_clf = BaggingClassifier(base_estimator=tree, n_estimators=1500,
random_state=42)
bagging_clf.fit(X_train, y_train)
ada_boost_clf = AdaBoostClassifier(n_estimators=30)
ada_boost_clf.fit(X_train, y_train)
evaluate(ada_boost_clf, X_train, X_test, y_train, y_test)
Reg.no:411622149007
scores['AdaBoost'] = {
'Train': accuracy_score(y_train, ada_boost_clf.predict(X_train)
),
'Test': accuracy_score(y_test, ada_boost_clf.predict(X_test)),
}
Gradient Boosting Classifier
estimators = []
log_reg = LogisticRegression(solver='liblinear')
estimators.append(('Logistic', log_reg))
tree = DecisionTreeClassifier()
estimators.append(('Tree', tree))
svm_clf = SVC(gamma='scale')
estimators.append(('SVM', svm_clf))
voting = VotingClassifier(estimators=estimators)
voting.fit(X_train, y_train)
scores_df = pd.DataFrame(scores)
OUTPUT:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 768 entries, 0 to 767
Data columns (total 9 columns):
# Column Non-Null Count Dtype
Bagging:
TRAINIG RESULTS:
===============================
CONFUSION MATRIX:
[[349 0]
[ 0 188]]
ACCURACY SCORE:
1.0000
CLASSIFICATION REPORT:
0 1 accuracy macro avg weighted avg
precision 1.00 1.00 1.00 1.00 1.00
recall 1.00 1.00 1.00 1.00 1.00
f1-score 1.00 1.00 1.00 1.00 1.00
support 349.00 188.00 1.00 537.00 537.00
TESTING RESULTS:
===============================
CONFUSION MATRIX:
[[119 32]
ACCURACY SCORE:
0.7576
Reg.no:411622149007
CLASSIFICATION REPORT:
0 1 accuracy macro avg weighted avg
precision 0.83 0.64 0.76 0.73 0.76
recall 0.79 0.70 0.76 0.74 0.76
f1-score 0.81 0.67 0.76 0.74 0.76
support 151.00 80.00 0.76 231.00 231.00
ACCURACY SCORE:
0.7749
CLASSIFICATION REPORT:
0 1 accuracy macro avg weighted avg
precision 0.83 0.67 0.77 0.75 0.78
recall 0.82 0.69 0.77 0.75 0.77
f1-score 0.83 0.68 0.77 0.75 0.78
support 151.00 80.00 0.77 231.00 231.00
AdaBoost Classifier
TRAINIG RESULTS:
===============================
CONFUSION MATRIX:
[[310 39]
[ 51 137]]
ACCURACY SCORE:
0.8324
CLASSIFICATION REPORT:
0 1 accuracy macro avg weighted avg
precision 0.86 0.78 0.83 0.82 0.83
recall 0.89 0.73 0.83 0.81 0.83
f1-score 0.87 0.75 0.83 0.81 0.83
support 349.00 188.00 0.83 537.00 537.00
TESTING RESULTS:
===============================
CONFUSION MATRIX:
[[123 28]
[ 27 53]]
ACCURACY SCORE:
0.7619
CLASSIFICATION REPORT:
0 1 accuracy macro avg weighted avg
precision 0.82 0.65 0.76 0.74 0.76
recall 0.81 0.66 0.76 0.74 0.76
f1-score 0.82 0.66 0.76 0.74 0.76
support 151.00 80.00 0.76 231.00 231.00
TRAINIG RESULTS:
===============================
CONFUSION MATRIX:
[[342 7]
[ 19 169]]
ACCURACY SCORE:
0.9516
CLASSIFICATION REPORT:
0 1 accuracy macro avg weighted avg
precision 0.95 0.96 0.95 0.95 0.95
recall 0.98 0.90 0.95 0.94 0.95
f1-score 0.96 0.93 0.95 0.95 0.95
support 349.00 188.00 0.95 537.00 537.00
TESTING RESULTS:
===============================
CONFUSION MATRIX:
[[116 35]
[ 26 54]]
Reg.no:411622149007
ACCURACY SCORE:
0.7359
CLASSIFICATION REPORT:
0 1 accuracy macro avg weighted avg
precision 0.82 0.61 0.74 0.71 0.74
recall 0.77 0.68 0.74 0.72 0.74
f1-score 0.79 0.64 0.74 0.72 0.74
support 151.00 80.00 0.74 231.00 231.00
Voting Classifier
TRAINIG RESULTS:
===============================
CONFUSION MATRIX:
[[327 22]
[ 82 106]]
ACCURACY SCORE:
0.8063
CLASSIFICATION REPORT:
0 1 accuracy macro avg weighted avg
precision 0.80 0.83 0.81 0.81 0.81
recall 0.94 0.56 0.81 0.75 0.81
f1-score 0.86 0.67 0.81 0.77 0.80
support 349.00 188.00 0.81 537.00 537.00
TESTING RESULTS:
===============================
CONFUSION MATRIX:
[[131 20]
[ 37 43]]
ACCURACY SCORE:
0.7532
CLASSIFICATION REPORT:
0 1 accuracy macro avg weighted avg
precision 0.78 0.68 0.75 0.73 0.75
recall 0.87 0.54 0.75 0.70 0.75
f1-score 0.82 0.60 0.75 0.71 0.75
support 151.00 80.00 0.75 231.00 231.00
Comparison:
Reg.no:411622149007
Result:
Thus, the Ensemble Methods like Bagging, Boosting and Voting Classifiers using Python
was executed and output has been verified successfully.
Reg.no:411622149007
AIM:
To implement the clustering algorithms using Python.
ALGORITHM:
Step 1: Start the Program.
Step 2: Import the Libraries- import numpy as np import matplotlib.pyplot as plt import pandas
as pd.
Step 3: Select K data objects for each cluster.
Step 4: Calculate dissimilarities D(X,Q) and allocate each data object to nearest cluster.
Step 5: Calculate the new modes for all clusters.
Step 6: Repeat step 4 and 5 until the cluster will become stable.
Step 7. Stop
PROGRAM:
import os
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
df = pd.read_csv("/content/CC GENERAL.csv")
mask = np.triu(np.ones_like(df.corr()))
heatmap = sns.heatmap(df.corr(), mask=mask, vmin=-
1, vmax=1, annot=True, cmap='Greens')
heatmap.set_title('Correlation Heatmap', fontdict={'fontsize':21}, pad=
16);
#make a copy for the original dataset
df_copy=df.copy()
df_copy.set_index('CUST_ID', inplace=True)
#solution
df_copy["CREDIT_LIMIT"].fillna(df_copy["CREDIT_LIMIT"].mean(), inplace=
True)
df_copy["MINIMUM_PAYMENTS"].fillna( df_copy["MINIMUM_PAYMENTS"].mean(),
inplace=True)
#test
round(df_copy.isnull().sum(axis=0)*100/df_copy.shape[0],2)
df_scaled = df_copy.copy()
col_names =df_scaled.columns
features = df_scaled[col_names]
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
df_Standard_Scaler = df_copy.copy()
df_Standard_Scaler[col_names] = scaler.fit_transform(features.values)
df_Standard_Scaler.describe().T
Reg.no:411622149007
df_RobustScaler = df_copy.copy()
df_Standard_Scaler
df_RobustScaler[col_names] = scaler.fit_transform(features.values)
df_RobustScaler.describe()
from sklearn.preprocessing import MinMaxScaler
df_MinMaxScaler = df_copy.copy()
scaler = MinMaxScaler()
df_MinMaxScaler[col_names] = scaler.fit_transform(features.values)
df_MinMaxScaler.describe()
pca = PCA()
#Transform the data
df_pca = pca.fit_transform(df_Standard_Scaler)
plt.plot(df_pca[0], df_pca[1]) # Plot the chart
plt.show()
kernel_pca = KernelPCA(n_components=2,kernel='linear')
kernel_pca_df = kernel_pca.fit_transform(df_Standard_Scaler)
plt.plot(kernel_pca_df[0], kernel_pca_df[1]) # Plot the chart
plt.show()
# 3. Use elbow method
inertia_list=[]
def cluster_Hierarchical(df):
cluster_Hierarchical = AgglomerativeClustering(n_clusters=4, affini
ty='euclidean')
Reg.no:411622149007
cluster_Hierarchical.fit_predict(df)
labels_cluster_Hierarchical=cluster_Hierarchical.labels_
return labels_cluster_Hierarchical
"""
#linkage='complete'
plt.figure(figsize=(10, 7))
plt.title("Counters Dendograms")
dend = shc.dendrogram(shc.linkage(y=df_pca , method='complete',metric='
euclidean')) """
from sklearn.cluster import DBSCAN
def dbscan(df):
cluster_dbscan = DBSCAN(eps=8, min_samples=4).fit(df)
n=cluster_dbscan.labels_
return cluster_dbscan.labels_
n=dbscan(df_RobustScaler)
n.shape
unique, counts = np.unique(n, return_counts=True)
dict(zip(unique, counts))
print('Silhoutte score of dbscan is ' , silhouette_score(df_RobustScale
r,n))
from sklearn.cluster import DBSCAN
def dbscan(df):
cluster_dbscan = DBSCAN(eps=8, min_samples=4).fit(df)
return cluster_dbscan.labels_
n=dbscan(df_RobustScaler)
n.shape
unique, counts = np.unique(n, return_counts=True)
print('Silhoutte score of dbscan is ' , silhouette_score(df_RobustScale
r,n))
dict(zip(unique, counts))
from sklearn.ensemble import IsolationForest
def IForest(df):
lforest = IsolationForest().fit(df)
lforest_labels = lforest.predict(df)
return lforest_labels
from sklearn import mixture
def gmm(df):
gmm = mixture.GaussianMixture(n_components=3,covariance_type="full"
,max_iter = 100,init_params="random")
gmm.fit(df)
gmm_labels = gmm.predict(df)
return(gmm_labels)
#1-MinMaxScaler with diff algo
df_pca = pca.fit_transform(df_MinMaxScaler)
labels_kmean=kmean(df_pca)
labels_cluster_Hierarchical=cluster_Hierarchical(df_pca)
Reg.no:411622149007
gmm_labels=gmm(df_pca)
cluster_dbscan_labels=dbscan(df_pca)
lforest_labels=IForest(df_pca)
print("scores of pca with MinMaxScaler")
print('Silhoutte score of kmean is ' , silhouette_score(df_pca, labels_
kmean))
print('Silhoutte score of Hierarchical is ' , silhouette_score(df_pca,
labels_cluster_Hierarchical))
print('Silhoutte score of EM is ' , silhouette_score(df_pca,gmm_labels)
)
print('Silhoutte score of IsolationForest is ' , silhouette_score(df_pc
a,lforest_labels))
for c in df_RobustScaler:
grid= sns.FacetGrid(df_RobustScaler, col='cluster')
grid.map(plt.hist, c)
cl = []
for row in df_RobustScaler['cluster']:
if row == "0" :cl.append("People mostly doesn't pay by Cash in Adva
nce mostly Purchase in installment and Purchase Frequently")
elif row =="1":cl.append("People with high Purchase Frequently and
use all types of payments ")
elif row =="2":cl.append("People mostly pay by Cash in Advance with
high palance and less Purchase Frequently")
elif row =="3":cl.append("People with less Purchases and mostly doe
sn't Purchase in installment")
#sns.scatterplot(tsne[:,0], tsne[:,1],s=100)
cluster_dbscan_labels=dbscan(tsne)
plt.scatter(tsne[:,0], tsne[:,1] , c = cluster_dbscan_labels,s=100)
#sns.scatterplot(tsne[:,0], tsne[:,1] , c = cluster_dbscan_labels,s=10,
palette="Set2")
tsne_ss = TSNE(n_components=2).fit_transform(df_Standard_Scaler)
cluster_dbscan_labels=dbscan(tsne_ss)
plt.scatter(tsne_ss[:,0], tsne_ss[:,1] , c = cluster_dbscan_labels,s=10
)
Reg.no:411622149007
OUTPUT:
Read the data Set
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 8950 entries, 0 to 8949
Data columns (total 18 columns):
# Column Non-Null Count Dtype
PRC_FULL_PAYMENT 0.00
TENURE 0.00
dtype: float64
Reg.no:411622149007
Data Preprocessing
BALANCE 0.0
BALANCE_FREQUENCY 0.0
PURCHASES 0.0
ONEOFF_PURCHASES 0.0
INSTALLMENTS_PURCHASES 0.0
CASH_ADVANCE 0.0
PURCHASES_FREQUENCY 0.0
ONEOFF_PURCHASES_FREQUENCY 0.0
PURCHASES_INSTALLMENTS_FREQUENCY 0.0
CASH_ADVANCE_FREQUENCY 0.0
CASH_ADVANCE_TRX 0.0
PURCHASES_TRX 0.0
CREDIT_LIMIT 0.0
PAYMENTS 0.0
MINIMUM_PAYMENTS 0.0
PRC_FULL_PAYMENT 0.0
TENURE 0.0
dtype: float64
Features Transformation:
Reg.no:411622149007
PCA:
Reg.no:411622149007
k-Means
Comparison:
Hierarchical Clustering:
Reg.no:411622149007
Dbscan:
Result:
Thus, the Clustering Algorithms using Python was executed and output has been
verified successfully.
Reg.no:411622149007
AIM:
To Build simple NN models using Python.
ALGORITHM:
Step 1: Start the Program.
Step 2: Given a set of incomplete data, consider a set of starting parameters.
Step 3: Expectation step (E – step): Using the observed available data of the dataset, estimate
(guess) the values of the missing data.
Step 4: Maximization step (M – step): Complete data generated after the expectation (E) step is
used in order to update the parameters.
Step 5: Repeat step 3 and step 4 until convergence.
Step 6: Stop
PROGRAM:
import numpy as np
import pandas as pd
# matplotlib
plt.rc('font', size=15)
plt.rc('axes', titlesize=18)
plt.rc('xtick', labelsize=10)
plt.rc('ytick', labelsize=10)
# seaborn
sns.set(font_scale = 1.2)
sns.set_style('whitegrid')
# Configuration
RANDOM_SEED = 1
N_SAMPLE_SIZE = 1500
N_CLUSTER = 3
def plot_clusters(X, y=None, ax=None, **kwargs):
"""Displays the given data as a scatter plot."""
if ax is None:
ax = plt.gca()
sns.scatterplot(
x=X[:, 0],
Reg.no:411622149007
y=X[:, 1],
palette='Set2',
hue=y,
ax=ax)
return ax
from matplotlib.patches import Ellipse
e = Ellipse(
x,
width,
height,
angle=angle,
facecolor='gray',
**kwargs)
e.set_alpha(a*alpha)
ax.add_artist(e)
return ax
def plot_center(x, ax=None, **kwargs):
"""Displays of a center point for the specified coordinate."""
if ax is None:
ax = plt.gca()
sns.scatterplot(
x=[x[0]],
y=[x[1]],
color='red',
s=80,
alpha=0.8,
ax=ax)
return ax
X, y = make_blobs(
n_samples=N_SAMPLE_SIZE,
centers=N_CLUSTER,
Reg.no:411622149007
n_features=2,
random_state=RANDOM_SEED
)
plt.subplots(1, 1, figsize=(8, 8))
plot_clusters(X)
plt.show()
class GaussianMixture(NamedTuple):
"""Gaussian mixture parameter set."""
# (K, dim) array -
each row corresponds to a gaussian component mean
mu: np.ndarray
Parameters
Returns
Parameters
Returns
for k in range(K):
rv = multivariate_normal(gm.mu[k], gm.var[k])
post[:, k] = gm.p[k] * rv.pdf(X)
# normalize
c = np.sum(post, axis=1)[:, np.newaxis]
post /= c
return post
M-Step
def m_step(X:np.ndarray, gm:GaussianMixture, post:np.ndarray) -
> GaussianMixture:
"""Performing M-Step.
Parameters
Returns
"""
N, _ = X.shape
K, dim = gm.mu.shape # number of clusters (K)
# update weights
w = np.sum(post, axis=0)
p = w / N
# update mixture component means (μ's)
c = np.sum(post, axis=1)[:, np.newaxis]
mu = np.dot(post.T, X) / w[:, np.newaxis]
y = get_labels(post)
plot_clusters(X, y=y, ax=ax)
for k in range(N_CLUSTER):
μ = gm.mu[k]
Σ = gm.var[k]
plot_center(μ, ax=ax)
plot_ellipse(μ, Σ, alpha=0.3, ax=ax)
return ax
Reg.no:411622149007
Visualization:
nrows = 2
ncols = 4
ax.set_title(f'Step {step+1}')
plt.legend()
plt.show()
Reg.no:411622149007
OUTPUT:
Result:
Thus, the EM Algorithm for Bayesian Network was built using Python was executed and
output has been verified successfully.
Reg.no:411622149007
AIM:
To Build simple NN models using Python.
ALGORITHM:
Step 1: Start the Program.
Step 2: Import the libraries. For example: import numpy as np
Step 3: Define/create input data. For example, use numpy to create a dataset and an array of
data values.
Step 4: Add weights and bias (if applicable) to input features. These are learnable parameters,
meaning that they can be adjusted during training.
Weights = input parameters that influences output
Bias = an extra threshold value added to the output
Step 5: Train the network against known, good data in order to find the correct values for the
weights and biases.
Step 6: Test the Network against a set of test data to see how it performs.
Step 7: Fit the model with hyperparameters (parameters whose values are used to control the
learning process), calculate accuracy, and make a prediction.
Step 8: Stop
PROGRAM:
# A
a =[0, 0, 1, 1, 0, 0,
0, 1, 0, 0, 1, 0,
1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 1]
# B
b =[0, 1, 1, 1, 1, 0,
0, 1, 0, 0, 1, 0,
0, 1, 1, 1, 1, 0,
0, 1, 0, 0, 1, 0,
0, 1, 1, 1, 1, 0]
# C
c =[0, 1, 1, 1, 1, 0,
0, 1, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0,
Reg.no:411622149007
0, 1, 0, 0, 0, 0,
0, 1, 1, 1, 1, 0]
# Creating labels
y =[[1, 0, 0],
[0, 1, 0],
[0, 0, 1]]
import numpy as np
import matplotlib.pyplot as plt
# visualizing the data, plotting A.
plt.imshow(np.array(a).reshape(5, 6))
plt.show()
# converting data and labels into numpy array
"""
Convert the matrix of 0 and 1 into one hot vector
so that we can directly feed it to the neural network,
these vectors are then stored in a list x.
"""
def sigmoid(x):
return(1/(1 + np.exp(-x)))
# Output layer
z2 = a1.dot(w2)# input of out layer
a2 = sigmoid(z2)# output of out layer
return(a2)
# initializing the weights randomly
def generate_wt(x, y):
l =[]
for i in range(x * y):
Reg.no:411622149007
l.append(np.random.randn())
return(np.array(l).reshape(x, y))
# for loss we will be using mean square error(MSE)
def loss(out, Y):
s =(np.square(out-Y))
s = np.sum(s)/len(y)
return(s)
# Back propagation of error
def back_prop(x, y, w1, w2, alpha):
# hidden layer
z1 = x.dot(w1)# input from layer 1
a1 = sigmoid(z1)# output of layer 2
# Output layer
z2 = a1.dot(w2)# input of out layer
a2 = sigmoid(z2)# output of out layer
# error in output layer
d2 =(a2-y)
d1 = np.multiply((w2.dot((d2.transpose()))).transpose(),
(np.multiply(a1, 1-a1)))
# Updating parameters
w1 = w1-(alpha*(w1_adj))
w2 = w2-(alpha*(w2_adj))
return(w1, w2)
def train(x, Y, w1, w2, alpha = 0.01, epoch = 10):
acc =[]
losss =[]
for j in range(epoch):
l =[]
for i in range(len(x)):
out = f_forward(x[i], w1, w2)
l.append((loss(out, Y[i])))
w1, w2 = back_prop(x[i], y[i], w1, w2, alpha)
print("epochs:", j + 1, "======== acc:", (1-(sum(l)/len(x)))*100)
acc.append((1-(sum(l)/len(x)))*100)
losss.append(sum(l)/len(x))
return(acc, losss, w1, w2)
k = 0
for i in range(len(Out[0])):
if(maxm<Out[0][i]):
maxm = Out[0][i]
k = i
if(k == 0):
print("Image is of letter A.")
elif(k == 1):
print("Image is of letter B.")
else:
print("Image is of letter C.")
plt.imshow(x.reshape(5, 6))
plt.show()
w1 = generate_wt(30, 5)
w2 = generate_wt(5, 3)
print(w1, "\n\n", w2)
# plotting accuracy
plt1.plot(acc)
plt1.ylabel('Accuracy')
plt1.xlabel("Epochs:")
plt1.show()
# plotting Loss
plt1.plot(losss)
plt1.ylabel('Loss')
plt1.xlabel("Epochs:")
plt1.show()
"""
The predict function will take the following arguments:
1) image matrix
2) w1 trained weights
3) w2 trained weights
Reg.no:411622149007
"""
predict(x[1], w1, w2)
OUTPUT
[array([[0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0,
0,
0, 1, 1, 0, 0, 0, 0, 1]]), array([[0, 1, 1, 1, 1, 0, 0, 1, 0,
0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0,
1, 0, 0, 1, 1, 1, 1, 0]]), array([[0, 1, 1, 1, 1, 0, 0, 1, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0,
0, 0, 0, 1, 1, 1, 1, 0]])]
[[1 0 0]
[0 1 0]
[0 0 1]]
Activation Function:
[[-1.19606517 0.35701803 0.71317902 0.50827198 1.98620667]
[-1.59077486 0.41880816 0.57304877 -0.37205614 -1.04269024]
[-0.1941151 0.50861565 -1.56259308 -0.08872702 0.33786455]
[-0.20572917 3.39354129 -1.06391825 0.28943676 -1.52478848]
[-1.54570587 -0.17357302 -0.42439848 -2.06332659 1.74626094]
[-0.75078944 2.00375702 -0.02322824 -1.07577908 1.1380853 ]
[ 0.86302856 -0.64502336 -0.23851911 1.01381374 1.30492746]
[ 1.10468106 1.40883514 -1.89216469 0.27791238 -0.01079454]
[ 0.0486144 0.35403961 -0.24370201 -2.51911717 -0.52835615]
[ 1.10427439 -1.20811277 -0.51972944 -0.29924303 -2.39862386]
[ 0.18311864 -0.353889 1.02854945 -0.5052859 -0.30519449]
[-0.75303406 2.71893476 -0.28173665 0.04685361 -0.60211221]
Reg.no:411622149007
Trained weights:
[[-1.19606517 0.35701803 0.71317902 0.50827198 1.98620667]
[-1.69198646 -0.26861884 0.56666918 -0.50344874 -1.27665875]
[-0.21125776 -0.17732944 -1.5692321 -0.22550232 0.32036221]
[-0.22287183 2.7075962 -1.07055728 0.15266146 -1.54229081]
[-1.64691748 -0.86100003 -0.43077808 -2.1947192 1.51229243]
[-0.75078944 2.00375702 -0.02322824 -1.07577908 1.1380853 ]
[ 0.86302856 -0.64502336 -0.23851911 1.01381374 1.30492746]
[ 1.0875384 0.72289005 -1.89880372 0.14113708 -0.02829687]
[ 0.0486144 0.35403961 -0.24370201 -2.51911717 -0.52835615]
[ 1.10427439 -1.20811277 -0.51972944 -0.29924303 -2.39862386]
[ 0.15004908 -0.42548163 1.01105131 -0.87578889 -0.31919913]
[-0.75303406 2.71893476 -0.28173665 0.04685361 -0.60211221]
[ 1.36674619 0.32791067 -1.55455751 -0.24942951 1.27866359]
[-0.96551924 -0.73473536 -0.52959574 0.82960585 -0.48920106]
[ 0.70894706 -0.19840764 -0.7373286 -0.63435794 -0.47628657]
[ 0.20755697 0.83920957 -1.23721569 -0.15983252 0.48734214]
[-0.48197649 -1.02127759 -0.79385556 0.5778004 0.37562074]
[ 0.48869817 1.33250917 -0.46949942 -0.71654212 -0.88629644]
[ 0.5541016 1.42298874 1.59844073 -1.08382493 1.02495951]
[-0.63453655 -2.45110645 -0.26357239 0.70669276 -0.30921974]
[-0.42082677 -0.09650442 0.8275041 -0.17783027 0.27453112]
[-0.75790551 0.60631097 0.31681654 -0.20157639 0.79697155]
[ 0.07799772 -0.24986059 1.7174474 -0.0203369 2.10136325]
[ 0.55452318 0.48222955 -0.0340718 -0.77963666 0.49603798]
[ 0.23348858 2.03799535 -0.32911282 -0.81109665 -0.83252439]
[-1.1368171 -2.24202867 -0.09336897 0.16987714 -0.87537594]
[-1.91415448 0.09431746 0.94347695 -0.40117661 -1.41535867]
[-0.10819795 -0.2078957 -1.85932425 -1.29832966 -0.3979213 ]
[ 1.94012198 -1.74752022 -0.46695343 -0.92049742 -2.38908138]
[-0.27701335 -0.0620355 0.47985888 -0.4579591 0.83121089]]
[[ 0.78098127 -1.89716614 -0.77318318]
Reg.no:411622149007
Predicted Output:
Result:
Thus, the simple NN models was built using Python was executed and output has been
verified successfully.
Reg.no:411622149007
AIM:
To Build simple NN models using Python.
ALGORITHM:
Step 1: Start the Program.
Step 2: Import the libraries. For example: import numpy as np
Step 3: Define/create input data. For example, use numpy to create a dataset and an array of
data values.
Step 4: Add weights and bias (if applicable) to input features. These are learnable parameters,
meaning that they can be adjusted during training.
Weights = input parameters that influences output
Bias = an extra threshold value added to the output
Step 5: Train the network against known, good data in order to find the correct values for the
weights and biases.
Step 6: Test the Network against a set of test data to see how it performs.
Step 7: Fit the model with hyperparameters (parameters whose values are used to control the
learning process), calculate accuracy, and make a prediction.
Step 8: Stop
PROGRAM:
import numpy as np
import matplotlib.pyplot as plt
import random
#
import tensorflow as tf
import keras
from tensorflow import keras
import keras_tuner as kt
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnis
t.load_data()
train_images = train_images / 255.0
test_images = test_images / 255.0
plt.figure(figsize=(10,10))
for i in range(16):
plt.subplot(4,4,i+1)
plt.xticks([])
Reg.no:411622149007
plt.yticks([])
plt.grid(False)
plt.imshow(train_images[i], cmap=plt.cm.binary)
plt.title("Image label is: {}".format(train_labels[i]))
plt.show()
x_train = train_images.reshape(-1,28,28,1)
x_test = test_images.reshape(-1,28,28,1)
early_stop = keras.callbacks.EarlyStopping(monitor='val_loss', patience
=3)
def build_model(hp):
model = keras.Sequential([
# First conv_block
keras.layers.Conv2D(
filters = hp.Choice('conv_1_filter', values=[16, 32, 64, 128]),
kernel_size=hp.Choice('conv_1_kernel', values = [3,4]),
activation='relu',
input_shape=(28,28,1)),
keras.layers.MaxPooling2D((2,2)),
# Second conv_block
keras.layers.Conv2D(
filters = hp.Choice('conv_2_filter', values=[16, 32, 64, 128]),
kernel_size=hp.Choice('conv_2_kernel', values = [3,4]),
activation='relu'),
keras.layers.MaxPooling2D((2,2)),
#
keras.layers.Flatten(),
keras.layers.Dense(units = hp.Choice('units', values=[16, 32, 64, 1
28, 256]),
activation='relu'),
keras.layers.Dropout(hp.Float('dropout', 0, 0.5, step=0.1, default=
0.5)),
#
keras.layers.Dense(10)
])
model.compile(optimizer=keras.optimizers.Adam(hp.Choice('learning_r
ate',
values=[1e-
1, 1e-2, 1e-3, 1e-4])),
loss=keras.losses.SparseCategoricalCrossentropy(from_logi
ts=True),
metrics=['accuracy'])
return model
tuner = kt.Hyperband(build_model,
objective="val_accuracy",
Reg.no:411622149007
max_epochs=5,
factor=3,
hyperband_iterations=3)
tuner.search_space_summary()
tuner.search(x_train,train_labels, epochs=3, validation_split=0.2)
print(f"""conv_1_filter is {best_hps.get('conv_1_filter')}""")
print(f"""conv_1_kernel is {best_hps.get('conv_1_kernel')}""")
print(f"""conv_2_filter is {best_hps.get('conv_2_filter')}""")
print(f"""conv_2_kernel is {best_hps.get('conv_2_kernel')}""")
print(" ")
print(f"""units is {best_hps.get('units')}""")
print(f"""learning_rate is {best_hps.get('learning_rate')}""")
print(f"""dropout is {best_hps.get('dropout')}""")
model = tuner.hypermodel.build(best_hps)
history = model.fit(x_train, train_labels,
epochs=50, validation_split=0.2)
val_acc_per_epoch = history.history['val_accuracy']
best_epoch = val_acc_per_epoch.index(max(val_acc_per_epoch)) + 1
print('Best epoch: %d' % (best_epoch,))
hypermodel = tuner.hypermodel.build(best_hps)
x = train_images[index]
x = x.reshape((1,) + x.shape)
x /= 255
successive_feature_maps = visualization_model.predict(x)
layer_names = [layer.name for layer in hypermodel.layers[1:]]
x -= x.mean()
x /= x.std()
x *= 64
x += 128
x = np.clip(x, 0, 255).astype('uint8')
display_grid[:, i * size : (i + 1) * size] = x
scale = 20. / n_features
plt.figure(figsize=(scale * n_features, scale))
plt.title(layer_name)
plt.grid(False)
plt.imshow(display_grid, aspect='auto', cmap='viridis')
epochs = range(len(acc))
plt.figure()
OUTPUT:
Result:
Thus, the deep NN models was built using Python was executed and output has been
verified successfully.