0% found this document useful (0 votes)
12 views7 pages

Practical SC

This document contains code for implementing several unsupervised machine learning algorithms, including self-organizing maps (SOM), radial basis function networks (RBF), and adaptive resonance theory (ART1) neural networks. It defines classes and functions to train and test SOM, RBF, and ART1 models on sample input data and visualize the results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views7 pages

Practical SC

This document contains code for implementing several unsupervised machine learning algorithms, including self-organizing maps (SOM), radial basis function networks (RBF), and adaptive resonance theory (ART1) neural networks. It defines classes and functions to train and test SOM, RBF, and ART1 models on sample input data and visualize the results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

PRACTICAL 5A

import datetime as dt
import numpy as np
now = dt.datetime.now()
print("Executed by Anu\nRoll No. : 7")
print("Current Date and Time : "+ now.strftime("%d-%m-%Y %H:%M:%S"))
x = [[1,1,1,-1]]
weight = []
print("Inputs are : ",x)
print("Obtain weight matrix : ")
x1 = np.transpose(x)
weight = np.multiply(x, x1)
print(weight)
print("Weight matrix with no self connection.")
for i in range(0, 4):
for j in range(0, 4):
if(i == j):
weight[i][j] = 0
print(weight)
print("Converting to binary")
x_bin = []
for i in range(0,4):
if(x[0][i] <= 1):
x_bin.append(1)
elif(x[0][i] < 1):
x_bin.apeend(0)
y_bin = []
y_bin = x_bin
print(y_bin)
print("Setting missing values")
x1_bin = x_bin
x1_bin[0] = 0
x1_bin[1] = 0
y1_bin = x1_bin
for i in range(0,4):
out = []
choice = int(input("Choose the y output : "))
var = 0
if(choice == 1):
for j in range(0,4):
var = var+y1_bin[j]*weight[j][0]
op = x1_bin[0]+var
if(op > 0):
op = 1
elif(op <= 0):
op = 0
x1_bin[0] = op
print(x1_bin)
elif(choice == 2):
for j in range(0,4):
var = var+y1_bin[j]*weight[j][1]
op = x1_bin[0]+var
if(op > 0):
op = 1
elif(op <= 0):
op = 0
x1_bin[0] = op
print(x1_bin)
elif(choice == 3):
for j in range(0,4):
var = var+y1_bin[j]*weight[j][2]
op = x1_bin[0]+var
if(op > 0):
op = 1
elif(op <= 0):
op = 0
x1_bin[0] = op
print(x1_bin)
elif(choice == 2):
for j in range(0,4):
var = var+y1_bin[j]*weight[j][3]
op = x1_bin[0]+var
if(op > 0):
op = 1
elif(op <= 0):
op = 0
x1_bin[0] = op
print(x1_bin)

if(x1_bin == y_bin):
print("No more iterations required.")
else:
print("More iterations needed.")
PRACTICAL 5B
import datetime as dt
import numpy as np
from scipy import *
from scipy.linalg import norm,pinv
from matplotlib import pyplot as plt
now = dt.datetime.now()
print("Executed by Anu\nRoll No. : 7")
print("Current Date and Time : "+ now.strftime("%d-%m-%Y %H:%M:%S"))
print()
class RBF:
def __init__(self, indim, numCenters, outdim):
self.indim = indim
self.numCenters = numCenters
self.outdim = outdim
self.centers = [random.uniform(-1, 1, indim) for i in range(numCenters)]
self.beta = 8
self.W = random.random((self.numCenters, self.outdim))

def _basisfunc(self, c, d):


assert len(d) == self.indim
return exp(-self.beta * norm(c-d)**2)

def _calcActivation(self, X):


G = zeros((X.shape[0], self.numCenters), float)
for ci, c in enumerate(self.centers):
for xi, x in enumerate(X):
G[xi, ci] = self._basisfunc(c, x)
return G

def train(self, X, Y):


random_idx = random.permutation(X.shape[0])[:self.numCenters]
self.centers = [X[i,:] for i in random_idx]
print("Center : ",self.centers)
G = self._calcActivation(X)
print("Activations of RBC's : ", G)
self.W = dot(pinv(G), Y)

def test(self, X):


G = self._calcActivation(X)
Y = dot(G, self.W)
return Y

if __name__ == '__main__':
n = 100
x = mgrid[-1:1:complex(0,n)].reshape(n,1)
y = sin(3*(x+0.5)**3-1)
rbf = RBF(1,10,1)
rbf.train(x,y)
z = rbf.test(x)
plt.figure(figsize = (12,8))
plt.plot(x, y, 'k-')
plt.plot(rbf.centers, zeros(rbf.numCenters), 'gs')

for c in rbf.centers:
cx = arange(c-0.7, c+0.7, 0.01)
cy = [rbf._basisfunc(array([cx_]), array([c])) for cx_ in cx]
plt.plot(cx, cy, '-', color='gray', linewidth=0.2)
plt.xlim(-1.2, 1.2)
plt.show()

PRACTICAL 6A
import datetime as dt
from minisom import MiniSom
from matplotlib import pyplot as plt
now = dt.datetime.now()
print("Executed by Anu\nRoll No. : 7")
print("Current Date and Time : "+ now.strftime("%d-%m-%Y %H:%M:%S"))
print()
#find out the input features
data = [[0.80, 0.55, 0.22, 0.03, 0.21], [0.82, 0.50, 0.23, 0.03, 0.21],
[0.80, 0.54, 0.22, 0.03, 0.21], [0.80, 0.53, 0.26, 0.03, 0.21],
[0.79, 0.56, 0.22, 0.03, 0.21], [0.75, 0.60, 0.25, 0.03, 0.21],
[0.77, 0.59, 0.22, 0.03, 0.21]]
som = MiniSom(6, 6, 5, sigma=0.3, learning_rate=0.2)#Initialization 6x6 SOM 5-input
features
som.train_random(data, 100) #Trains the SOM with 100 iterations
plt.imshow(som.distance_map())
PRACTICAL 6B
import datetime as dt
import math
import sys
now = dt.datetime.now()
print("Executed by Anu\nRoll No. : 7")
print("Current Date and Time : "+ now.strftime("%d-%m-%Y %H:%M:%S"))
print()
N = 4
M = 5
VIGILANCE = 0.4
PATTERN_ARRAY = [[1, 1, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 1, 1],
[0, 1, 0, 0], [0, 0, 1, 0], [1, 0, 1, 0]]
class ART1:
def __init__(self, inputSize, numClusters, vigilance):
self.mInputSize = inputSize
self.mNumClusters = numClusters
self.mvigilance = vigilance

self.bottom_up_weights = []
self.top_down_weights = []

self.inputLayer = []
self.interface_layer = []
self.f2 = []

#Initialize bottom-up weight matrix


for i in range(self.mNumClusters):
self.bottom_up_weights.append([0.0] * self.mInputSize)
for j in range(self.mInputSize):
self.bottom_up_weights[i][j] = 1.0 / (1.0 + self.mInputSize)

#Initialize top-down weight matrix


for i in range(self.mNumClusters):
self.top_down_weights.append([0.0] * self.mInputSize)
for j in range(self.mInputSize):
self.top_down_weights[i][j] = 1.0

self.inputLayer = [0.0] * self.mInputSize


self.interface_layer = [0.0] * self.mInputSize
self.f2 = [0.0] * self.mNumClusters

def get_vector_sum(self, nodeArray):


total = 0
for i in range(len(nodeArray)):
total += nodeArray[i]
return total
def get_maximum(self, nodeArray):
maximum = -1
maxValue = -0
unique = True
for i in range(len(nodeArray)):
if nodeArray[i] > maxValue:
maximum = i
maxValue = nodeArray[i]
return maximum

def test_for_reset(self, activationSum, inputSum, f2max):


if(inputSum == 0): return False
elif(float(activationSum) / float(inputSum) >= self.mvigilance):
return False
else:
self.f2[f2max] = -1.0
return True

def update_weights(self, activationSum, f2max):


for i in range(self.mInputSize):
self.bottom_up_weights[f2max][i] = (2.0 * float(self.interface_layer[i])) / (1.0
+ float(activationSum))

for i in range(self.mInputSize):
self.top_down_weights[f2max][i] = self.interface_layer[i]

def ART1(self, trainingPattern, isTraining):


inputSum = 0
activationSum = 0
f2max = 0
reset = True

for i in range(self.mNumClusters):
self.f2[i] = 0.0

for i in range(self.mInputSize):
self.inputLayer[i] = float(trainingPattern[i])

inputSum = self.get_vector_sum(self.inputLayer)

for i in range(self.mInputSize):
self.interface_layer[i] = self.inputLayer[i]

for i in range(self.mNumClusters):
for j in range(self.mInputSize):
self.f2[i] += self.bottom_up_weights[i][j] * float(self.inputLayer[j])

reset = True
while reset:
f2Max = self.get_maximum(self.f2)
if f2Max == -1:
f2Max = self.mNumClusters
self.f2.append(0.0)
self.top_down_weights.append([1.0] * self.mInputSize)
self.bottom_up_weights.append([1.0 / (1.0 + self.mInputSize)] *
self.mInputSize)
self.mNumClusters += 1

for i in range(self.mInputSize):
self.interface_layer[i] = self.inputLayer[i] *
math.floor(self.top_down_weights[f2Max][i])

activationSum = self.get_vector_sum(self.interface_layer)
reset = self.test_for_reset(activationSum, inputSum, f2Max)
if isTraining:
self.update_weights(activationSum, f2Max)
return f2Max
def print_results(self):
sys.stdout.write("Clusters found : "+str(self.mNumClusters)+"\n")
sys.stdout.write("Rho : "+str(self.mvigilance)+"\n")
sys.stdout.write("Final weight values : \n")

for i in range(self.mNumClusters):
for j in range(self.mInputSize):
sys.stdout.write(str(self.bottom_up_weights[i][j])+",")
sys.stdout.write("\n")
sys.stdout.write("\n")

for i in range(self.mNumClusters):
for j in range(self.mInputSize):
sys.stdout.write(str(self.top_down_weights[i][j])+",")
sys.stdout.write("\n")
sys.stdout.write("\n")
return
if __name__ == '__main__':
net = ART1(N,M,VIGILANCE)
for line in PATTERN_ARRAY:
net.ART1(line, True)
for line in PATTERN_ARRAY:
print(str(line)+','+str(net.ART1(line, True)))

You might also like