Last active
August 29, 2015 14:24
-
-
Save sksullivan/cae75891a3045fc5baab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
import random | |
import json | |
import pandas as pd | |
class NN: | |
def __init__(self,numNeurons): | |
self.knowledgeTable = [{} for i in range(numNeurons)] | |
def compareInputs(self,a,b): | |
totalMatches = 0 | |
for x in range(len(a)): | |
if a[x] == b[x]: | |
totalMatches += 1 | |
return totalMatches | |
def feedInput(self, image, accepted): | |
for y in range(len(image)): | |
if not image[y] in self.knowledgeTable[y]: | |
self.knowledgeTable[y][image[y]] = accepted | |
def mostSimilarInput(self, neuron, imageSlice): | |
bestInputs = [] | |
bestDifference = 0 | |
for inputSlice in neuron.keys(): | |
diff = self.compareInputs(imageSlice, inputSlice) | |
print diff | |
if diff > bestDifference: | |
bestDifference = diff | |
bestInputs = [inputSlice] | |
elif diff == bestDifference: | |
bestInputs.append(inputSlice) | |
return bestInputs | |
def classify(self, image): | |
hypothesis = 0 | |
for y in range(len(image)): | |
bestCandidateInputs = self.mostSimilarInput(self.knowledgeTable[y], image[y]) | |
print bestCandidateInputs | |
bestInput = bestCandidateInputs[0] | |
if len(bestCandidateInputs) > 1: | |
bestInput = bestCandidateInputs[random.randint(0,len(bestCandidateInputs)-1)] | |
hypothesis += self.knowledgeTable[y][bestInput] | |
return hypothesis / float(len(image)) | |
#end | |
class DataAdapter: | |
def __init__(self,fileNames): | |
self.data = [] | |
for fileName in fileNames: | |
self.data.append((tuple(tuple(x) for x in pd.read_csv(fileName, sep=',',header=None).values), int(fileName[0]))) | |
#end | |
n = NN(9) #L.csv #S.csv | |
trainingData = DataAdapter(['1doc.csv','0doc.csv']).data | |
print trainingData | |
for document in trainingData: | |
n.feedInput(document[0], document[1]) | |
#Sort-of-S.csv | |
print n.classify(DataAdapter(['9doc.csv']).data[0][0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment