Commit c6d699cc authored by Sushant Mahajan's avatar Sushant Mahajan

created weight matrices

parent 64bb590d
Pipeline #275 skipped
......@@ -2,27 +2,60 @@
import sys
import os
import csv
from random import random
from pprint import pprint as pp
params = {"layers":[57, 57*2, 1], "test":"TestX.csv", "train":"Train.csv"}
params = {"layers":[57, int(57/2), 1], "test":"TestX.csv", "train":"Train.csv"}
def getData(srcF, isTrain=True):
def getData(srcF, isTrain=True, addBias=True):
X,y=[],[]
with open(srcF) as src:
reader = csv.reader(src, delimiter=',')
for i,row in enumerate(reader):
temp = []
if addBias:
temp.append(1)
end = -1 if isTrain else len(row)
X.append(row[:end])
temp.extend(row[:end])
#correct data type
X[i] = [x for x in map(float, X[i][:-2])]
X[i].extend([int(X[i][-2]),int(X[i][-1])])
X.append([x for x in map(float, temp)])
if isTrain:
y.append(int(row[-1]))
return (X,y)
def getMatrix(r, c, vec):
mat = []
for i in range(r):
temp = []
for j in range(c):
idx = i*(c)+j
temp.append(vec[j])
mat.append(temp)
return mat
def cost(li, lh, lo, weights, X, y):
w1t = weights[:(li+1)*lh] #28x58
w2t = weights[(li+1)*lh:] #1x29
w1,w2 = getMatrix(lh, li+1, w1t), getMatrix(lo, lh+1, w2t)
if __name__ == "__main__":
X,y = getData(params["train"])
tX,ty = getData(params["test"], isTrain=False)
# tX,ty = getData(params["test"], isTrain=False)
print(len(X), len(X[0]), len(y), X[0])
li,lh,lo = tuple(params["layers"])
weights = [random() for _ in range(lh*(li+1)+lo*(lh+1))]
w1,w2 = cost(li, lh, lo, weights, X, y)
#print(len(w1), len(w1[0]), len(w2), len(w2[0]))
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment