ML Ex8
ML Ex8
ML Ex8
ipynb - Colaboratory
keyboard_arrow_down Exercise 8: Design and implement a radial basis function neural network to solve function approximation or
regression problem.
import math
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler
import numpy as numpy
Mounted at /content/gdrive
scaler= StandardScaler()
scaler.fit(X_train)
X_train= scaler.transform(X_train)
X_test= scaler.transform(X_test)
K_cent= 8
km= KMeans(n_clusters= K_cent, max_iter= 100)
km.fit(X_train)
cent= km.cluster_centers_
max=0
for i in range(K_cent):
for j in range(K_cent):
d = numpy.linalg.norm(cent[i]-cent[j])
if(d> max):
max= d
d= max
sigma= d/math.sqrt(2*K_cent)
shape= X_train.shape
row= shape[0]
column= K_cent
G= numpy.empty((row,column), dtype= float)
for i in range(row):
for j in range(column):
dist= numpy.linalg.norm(X_train[i]-cent[j])
G[i][j]= math.exp(-math.pow(dist,2)/math.pow(2*sigma,2))
GTG= numpy.dot(G.T,G)
GTG_inv= numpy.linalg.inv(GTG)
fac= numpy.dot(GTG_inv,G.T)
W= numpy.dot(fac,Y_train)
row= X_test.shape[0]
column= K_cent
G_test= numpy.empty((row,column), dtype= float)
for i in range(row):
for j in range(column):
dist= numpy.linalg.norm(X_test[i]-cent[j])
G_test[i][j]= math.exp(-math.pow(dist,2)/math.pow(2*sigma,2))
https://colab.research.google.com/drive/1-ullvnEq413yfUNMv0FE5TR7d0w_JcAN#scrollTo=STOkwGLs41SX 1/2
2/19/24, 11:53 AM Ex 8 - RBF NN.ipynb - Colaboratory
prediction= numpy.dot(G_test,W)
prediction= 0.5*(numpy.sign(prediction-0.5)+1)
score= accuracy_score(prediction,Y_test)
print ('The accuracy of the RBF Neural Network is: ' , "{0:0.2f}".format(score.mean()*100), '%')
https://colab.research.google.com/drive/1-ullvnEq413yfUNMv0FE5TR7d0w_JcAN#scrollTo=STOkwGLs41SX 2/2