Skip to content

Instantly share code, notes, and snippets.

@Yeaseen
Created November 18, 2018 18:24
Show Gist options
  • Save Yeaseen/1e29ab6a77da6fc774b955695701680a to your computer and use it in GitHub Desktop.
Save Yeaseen/1e29ab6a77da6fc774b955695701680a to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Nov 18 23:42:40 2018
@author: yeaseen
"""
import numpy as np
def split(s, delim=[" ", '\n']):
words = []
word = []
for c in s:
if c not in delim:
word.append(c)
else:
if word:
words.append(''.join(word))
word = []
if word:
words.append(''.join(word))
return words
def loadfile(filename,checkTrain):
file = open(filename, "r")
first = checkTrain
rows = list()
for line in file:
if(first) == True:
dims = split(line)
first = False
else:
vals = split(line, [' ' ,'\t', '\n'])
#print(vals)
rows.append(vals)
if(checkTrain):
return dims, rows
else:
return rows
dims, rows = loadfile('Train1.txt',True)
test = loadfile('Test1.txt',False)
dims=np.array(dims)
dims = dims.astype(np.float)
rows=np.array(rows)
mat = rows.astype(np.float)
test=np.array(test)
test= test.astype(np.float)
att=int(dims[0])
#print(att)
clss=int(dims[1])
#print(clss)
#wactive =np.random.random_sample(((att+1),))
wactive=[0,0,1]
#print(w)
matrix=np.array(mat)
Y=matrix[:,-1].copy()
Y=np.array(Y)
Y=Y.astype(np.int)
Y=np.array(Y).tolist()
#print(Y)
matrix[:,-1]=np.ones((matrix.shape[0]))
targetOutput=test[:,-1].copy()
targetOutput=np.array(targetOutput)
targetOutput=targetOutput.astype(np.int)
targetOutput=np.array(targetOutput).tolist()
test[:,-1] = np.ones((test.shape[0]))
counter=0
maxCounter=matrix.shape[0]
print(maxCounter)
discriminant= True
while(discriminant):
for i in matrix:
product=np.dot(i,wactive)
print(product)
counter+=1
if(product < 0):
classed=1
else:
classed=2
if(classed != Y[counter]):
if(classed == 1):
#print('sub')
wactive=np.add(wactive,i)
counter=0
else:
#print('add')
wactive=np.subtract(wactive,i)
counter=0
if(counter == maxCounter):
discriminant = False
print(wactive)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment