def __init__(self, inputs, targets, nRBF, sigma=0, usekmeans=0, normalise=0): self.nin = np.shape(inputs)[1] self.nout = np.shape(targets)[1] self.ndata = np.shape(inputs)[0] self.nRBF = nRBF self.usekmeans = usekmeans self.normalise = normalise if usekmeans: self.kmeansnet = kmeansnet.kmeans(self.nRBF, inputs) self.hidden = np.zeros((self.ndata, self.nRBF + 1)) if sigma == 0: # Set width of Gaussians d = (inputs.max(axis=0) - inputs.min(axis=0)).max() self.sigma = d / np.sqrt(2 * nRBF) else: self.sigma = sigma self.perceptron = pcn.pcn(self.hidden[:, :-1], targets) # Initialise network self.weights1 = np.zeros((self.nin, self.nRBF))
def __init__(self,inputs,targets,nRBF,sigma=0,usekmeans=0,normalise=0): self.nin = np.shape(inputs)[1] self.nout = np.shape(targets)[1] self.ndata = np.shape(inputs)[0] self.nRBF = nRBF self.usekmeans = usekmeans self.normalise = normalise if usekmeans: self.kmeansnet = kmeansnet.kmeans(self.nRBF, inputs) self.hidden = np.zeros((self.ndata, self.nRBF+1)) if sigma==0: # Set width of Gaussians d = (inputs.max(axis=0)-inputs.min(axis=0)).max() self.sigma = d/np.sqrt(2*nRBF) else: self.sigma = sigma self.perceptron = pcn.pcn(self.hidden[:, :-1], targets) # Initialise network self.weights1 = np.zeros((self.nin, self.nRBF))
target = spam[:, 57] order = range(np.shape(spam)[0]) np.random.shuffle(order) spam = spam[order, :] target = target[order] train = spam[0:3449, 0:56] traint = target[0:3449] valid = spam[3450:4029, 0:56] validt = target[3450:4029] test = spam[4030:4600, 0:56] testt = target[4030:4600] net = kmeansnet.kmeans(3, train) net.kmeanstrain(train) cluster = net.kmeansfwd(test) net = som.som(6, 6, train) net.somtrain(train, 400) best = np.zeros(np.shape(train)[0], dtype=int) for i in range(np.shape(train)[0]): best[i], activation = net.somfwd(train[i, :]) pl.plot(net.map[0, :], net.map[1, :], 'k.', ms=15) where = pl.find(traint == 0) pl.plot(net.map[0, best[where]], net.map[1, best[where]], 'rs', ms=30) where = pl.find(traint == 1) pl.plot(net.map[0, best[where]], net.map[1, best[where]], 'gv', ms=30)
random.shuffle(order) data = data[order,:] target = target[order,:] train = data[::2,0:p] traint = target[::2] valid = data[1::4,0:p] validt = target[1::4] test = data[3::4,0:p] testt = target[3::4] #print train.max(axis=0), train.min(axis=0) # Train the network import kmeansnet net = kmeansnet.kmeans(6,train) net.kmeanstrain(train) cluster = net.kmeansfwd(test) kprediction = 1.*cluster actual = data[3::4,p] correct = 0. for i in range(len(actual)): if kprediction[i] == actual[i]: correct += 1. print 'K-means percentage correct =', correct/len(actual) import som net = som.som(7,7,train) net.somtrain(train,400)
np.random.shuffle(order) iris = iris[order, :] target = target[order] train = iris[::2, 0:4] traint = target[::2] valid = iris[1::4, 0:4] validt = target[1::4] test = iris[3::4, 0:4] testt = target[3::4] # print train.max(axis=0), train.min(axis=0) import kmeansnet # import kmeans as kmeansnet net = kmeansnet.kmeans(3, train) net.kmeanstrain(train) cluster = net.kmeansfwd(test) print(1. * cluster) print(iris[3::4, 4]) import som net = som.som(6, 6, train) net.somtrain(train, 400) best = np.zeros(np.shape(train)[0], dtype=int) for i in range(np.shape(train)[0]): best[i], activation = net.somfwd(train[i, :]) pl.plot(net.map[0, :], net.map[1, :], 'k.', ms=15)
random.shuffle(order) data = data[order, :] target = target[order, :] train = data[::2, 0:p] traint = target[::2] valid = data[1::4, 0:p] validt = target[1::4] test = data[3::4, 0:p] testt = target[3::4] #print train.max(axis=0), train.min(axis=0) # Train the network import kmeansnet net = kmeansnet.kmeans(6, train) net.kmeanstrain(train) cluster = net.kmeansfwd(test) kprediction = 1. * cluster actual = data[3::4, p] correct = 0. for i in range(len(actual)): if kprediction[i] == actual[i]: correct += 1. print 'K-means percentage correct =', correct / len(actual) import som net = som.som(7, 7, train) net.somtrain(train, 400)