-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogReg.py
58 lines (48 loc) · 1.66 KB
/
LogReg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import math
import matplotlib.pyplot as plt
from sys import stdin
import numpy as np
def dot(xArr,yArr):
return sum([x*y for x,y in zip(xArr,yArr)])
def p(wArr,xArr):
temp = math.exp(dot(wArr[:-1:],xArr) + wArr[-1])
return temp/(1+temp)
# Prepare the input
inputArray = list(stdin.readlines())
inputArray = [line.strip().split() for line in inputArray]
inputArray = [[float(data) for data in line] for line in inputArray]
inputArray = np.array(inputArray)
# We set up the weights
k = len(inputArray[0])
W = [0 for j in range(k)]
# This portion is just the gradient descent with a scaling constant of 1
for index in range(100):
pArr = [p(W,x[:-1]) for x in inputArray]
for step in range(k-1):
W[step] += sum([x*(y-p) for x,y,p in zip(inputArray[:,step],inputArray[:,-1],pArr)])
W[k-1] += sum([y - p for y,p in zip(inputArray[:,-1],pArr)])
# The folloing items can be used to separate the blues from the reds to aid in visual representation
blue = []
red = []
for line in inputArray:
if line[-1] == 0:
blue.append(line[:-1])
else:
red.append(line[:-1])
blueLen = len(blue)
redLen = len(red)
print("\nThe Blues\t\tThe Reds")
for index in range(max(blueLen,redLen)):
if index < blueLen and index < redLen:
print(str(blue[index])+"\t\t"+str(red[index]))
elif index < blueLen:
print(str(blue[index])+"\t\t---")
elif index < redLen:
print("---\t\t"+str(red[index]))
# Display the separating plane
print("\nThe separating plane is given by\n")
print("0 = ",end="")
for index in range(len(W)-1):
print("("+str(W[index])+") X"+str(index+1),end=" + ")
print("("+str(W[-1])+")"+"\n\n")
exit()