-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogisticRegression.py
More file actions
174 lines (141 loc) · 4.39 KB
/
LogisticRegression.py
File metadata and controls
174 lines (141 loc) · 4.39 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
'''
Created on 19-Sep-2016
@author: ankit
'''
import random
import math
import numpy as np
import matplotlib.pyplot as plt
def LogisticRegression(z):
count=0
nita=0.0001
f=open('breast-cancer-wisconsin10.txt', 'r')
list1=list(f)
#print(training_list)
break1= (len(list1)*(2/3))
break1= int(round(break1,0))
#print(break1)
r_percent=int(round(z*break1))
starting_index=random.randrange(0,break1-r_percent+1)
training_list=list1[starting_index:starting_index+r_percent] #will stop 1 element before stop value
test_list=list1[break1:]
#print(test_list)
predict=[None]*(len(test_list))
prob=[0.5]*len(list1)
w_list=[]
convergence=0
y=[None]*(len(list1))
x=[[1 for j in range(0,10)] for i in range(0,len(list1))]
#print(x)
i=0
cost=10000000000
flag=0
#print("**************")
#print(training_list)
#print("*****************")
while(not convergence):
i=0
count+=1
#print(count)
for aaa in training_list:
#print(aaa)
if '?' in aaa:
continue
train_row=[int(x) for x in aaa.split(',')]
#print(len(y))
#print(len(training_list))
#print(test_row)
#print(i)
y[i]=train_row[10]
for index in range(1,10):
x[i][index]=train_row[index]
if not w_list:
w_list=[0]*10
#print("Kya 0 se hamaara chutiya kat raha hai dost?")
else:
#print("Kabhi gaye ho idhar")
theta=w_list[0]
for j in range(1,10):
theta+=w_list[j]*train_row[j]
prob[i]=float((math.e**theta)/(1+(math.e**theta)))
#print(prob[i])
#print("ending loop")
i=i+1
#print("****************** Main yahan bhi aaya")
'''
if count==100:
convergence=1
continue
'''
len_train_list=i
for index in range(0,9):
summant=0
for j in range(0,len_train_list):
summant+=x[j][index]*(y[j]-prob[j])
#print(summant)
w_list[index]+=(nita*summant)
#print("divider")
#print(w_list)
#Calculating the cost function
#*******Consistent
curr_cost=0
new_index=-1
for str1 in training_list:
if '?' in str1:
continue
new_index+=1
temp_train_row=[int(x) for x in str1.split(',')]
if temp_train_row[10]==1:
curr_cost+=float(math.log(prob[new_index]))
elif temp_train_row[10]==0:
curr_cost+=float(math.log(1-prob[new_index]))
curr_cost=float(curr_cost*float(-1/new_index))
if curr_cost<cost:
cost=curr_cost
#print(cost )
else:
print("Cost is increasing")
if cost<0.2:
convergence=1
continue
#*************consistent end
t=-1
for str2 in test_list:
if '?' in str2:
continue
t+=1
test_row=[int(x) for x in str2.split(',')]
theta2=w_list[0]
for j in range(1,10):
theta2+=w_list[j]*test_row[j]
prob[t]=float(math.e**(theta2)/(1+math.e**(theta2)))
antiprob=1-prob[t]
if prob[t]>antiprob:
predict[t]=1
elif prob[t]<=antiprob:
predict[t]=0
i=0
for num1 in range(0,t+1):
if predict[num1]==y[num1]:
#print(type(predict[num1]))
#print("Dusra")
#print(type(y[num1]))
i+=1
accuracy=float((i/(t+1))*100)
#print(accuracy)
return accuracy
accuracy=[0]*6
list1=[.01,.02 ,.03, .125, .625 ,1]
for i in range(0,5):
for j in range(0,6):
accuracy[j]+=(LogisticRegression(list1[j]))
#print(accuracy)
accuracy1=[float(x/5) for x in accuracy]
#print(accuracy1)
plt.plot(list1,accuracy1)
#plt.ylabel('some numbers')
plt.show()
'''
accuracy=LogisticRegression(1)
print(accuracy)
'''