-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogistic_regression_multiclass.py
More file actions
176 lines (143 loc) · 4.21 KB
/
logistic_regression_multiclass.py
File metadata and controls
176 lines (143 loc) · 4.21 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
175
176
# THE ONE THAT WORKS PERFECTLY
from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
def sigmoid(Z):
return 1/(1+np.exp(-Z))
def hypothesis(theta, X):
# X is a mx(n+1) matrix
# theta is a (n+1)x1 matrix
# Both are 2 dimensional arrays and not
# np.matrix
X_np = np.matrix(X)
theta_np = np.matrix(theta)
return np.array(sigmoid(X_np * theta_np))[0][0]
def cost(theta, X, y):
cost = 0
m = len(y)
for i in range(m):
cost = cost+(y[i][0] * np.log(hypothesis(theta, X[i])) + (1-y[i][0])*np.log(1-hypothesis(theta, X[i])))
return -cost/m
def gradient(X, y, alpha=0.01):
m = len(X)
theta = [[0] for i in range(len(X[0]))]
iter_count = 1000
for i2 in range(iter_count):
for i in range(m):
for j in range(len(X[0])):
theta[j] = theta[j] - alpha * (hypothesis(theta, X[i]) - y[i]) * X[i][j] / m
# print(type(theta))
# print(theta)
return theta
def predict(y):
# new_y=[0] * len(y)
# for i in range(len(y)):
# if y[i]>=0.5:
# new_y[i] = 1
# return new_y
if y>=0.5:
return 1
else:
return 0
def multiclass(X, y, new_x, all=[0, 1, 2]):
p=3
max_hypothesis_val=-1
max_hypothesis=-1
combined_arr_x, combined_arr_y = get_combined_arr(X, y, p)
for i in range(p):
rest = get_except(all, i)
final_temp=[]
final_temp_y = []
for j in range(len(combined_arr_y)):
if combined_arr_y[j][0][0] in rest:
final_temp=combine(final_temp, combined_arr_x[j])
final_temp_y=combine(final_temp_y, combined_arr_y[j])
#final temp set to the value to use as X in logistic regression
#y value to be used is combined_arr_y[i]
print(final_temp)
final_temp_y=combine(final_temp_y, combined_arr_y[i])
final_temp=combine(final_temp, combined_arr_x[i])
for j in range(len(final_temp_y)):
if final_temp_y[j][0] != i:
final_temp_y[j] = [0]
else:
final_temp_y[j] = [1]
theta_temp = gradient(final_temp, final_temp_y)
print(theta_temp)
hypothesis_temp = hypothesis(theta_temp, new_x)
if hypothesis_temp>max_hypothesis:
max_hypothesis = hypothesis_temp
max_hypothesis_val = i
print(max_hypothesis_val)
# plot(combined_arr_x)
# plot_line(theta_temp, -10, 10)
return max_hypothesis_val
def get_except(old_arr, exception):
new_arr=[0 for i in range(len(old_arr) - 1)]
j=0
for i in range(len(old_arr)):
if old_arr[i] != exception:
new_arr[j] = old_arr[i]
j += 1
return new_arr
def get_combined_arr(X, y, p):
combined_arr_x = []
combined_arr_y = []
for i in range(p):
temp_combined_arr_x = []
temp_combined_arr_y = []
for j in range(len(y)):
if y[j][0] == i:
temp_combined_arr_x.append(X[j])
temp_combined_arr_y.append(y[j])
combined_arr_x.append(temp_combined_arr_x)
combined_arr_y.append(temp_combined_arr_y)
return combined_arr_x, combined_arr_y
X = [
[1, 1, 10],
[1, 2, 12],
[1, 3, 9],
[1, 3, 13],
[1, 11, 1],
[1, 22, 2],
[1, 13, 4],
[1, 15, 3],
[1, 1, 0],
[1, 2, 1],
[1, 3, 4],
[1, 3, 1]
]
y = [
[0],
[0],
[0],
[0],
[1],
[1],
[1],
[1],
[2],
[2],
[2],
[2]
]
def combine(s, t):
for i in range(len(t)):
s.append(t[i])
return s
def plot(X):
for i in range(len(X[0])):
plt.plot(X[0][i][1], X[0][i][2], 'o')
for i in range(len(X[1])):
plt.plot(X[0][i][1], X[0][i][2], 'r')
for i in range(len(X[2])):
plt.plot(X[0][i][1], X[0][i][2], 'x')
def plot_line(theta, start, end):
y=[]
x=[]
for i in range(start, int((end-start)/2), end):
x.append(i)
y.append((theta[0]+theta[1]*i)/theta[2])
plt.plot(x, y)
print(multiclass(X, y, [1, 10, 1]))
#plt.show()