Practical SC
Practical SC
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))
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 = []
for i in range(self.mInputSize):
self.top_down_weights[f2max][i] = self.interface_layer[i]
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)))