-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulticlass_logistic_regression.py
More file actions
133 lines (116 loc) · 2.78 KB
/
multiclass_logistic_regression.py
File metadata and controls
133 lines (116 loc) · 2.78 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
#multiclass logisitic regression
import numpy as np
import matplotlib.pyplot as plt
all_cost=[]
def logistic(z):
return 1/(1+np.exp(-z))
def hypothesis(theta, X):
return logistic(np.array(np.matrix(X)*np.transpose(np.matrix(theta))))[0][0]
# return getY(theta, X)
def cost(theta, X, y, y_val):
m=len(y)
total=0
for i in range(m):
temp_y=y[i]
if(y[i]==y_val):
temp_y=1
else:
temp_y=0
total+=(temp_y*np.log(hypothesis(theta, X[i])) + (1-temp_y)*np.log(1-hypothesis(theta, X[i])))
return -total/m
#y_val denotes the current class being tested
def gradient_descent(X, y, alpha, y_val):
theta=[0]*len(X[0])
tempCost=1000
while(tempCost>0.01):
for j in range(len(theta)):
pd=0
for i in range(len(y)):
temp_y=y[i]
if(y[i]==y_val):
temp_y=1
else:
temp_y=0
pd+=(hypothesis(theta, X[i])-temp_y)*X[i][j]
theta[j]=theta[j]-alpha*pd
all_cost.append(tempCost)
if(tempCost-cost(theta, X, y, y_val)<1e-5):
break
tempCost=cost(theta, X, y, y_val)
# print(tempCost)
# print(theta)
# temp_x = np.linspace(0, len(all_cost), len(all_cost) + 1)
# for i in range(len(all_cost)):
# plt.plot(temp_x[i], all_cost[i], 'ro')
# plt.show()
return theta
#X is an (n+1) row vector
def getY(theta, X, no_of_classes):
max_=0
max_hypothesis=-1
for i in range(no_of_classes):
temp_hypothesis=np.array(np.matrix(X)*np.transpose(np.matrix(theta[i])))[0][0]
if(temp_hypothesis>max_hypothesis):
hypothesis=i
return hypothesis
# class count starts from 0
def multiclass(X, y, alpha, classes):
all_thetas = []
for i in range(classes):
all_thetas.append(gradient_descent(X, y, alpha, i))
return all_thetas
#dataset 1
X=[
[1, 0, 2],
[1, 1, 4],
[1, 2, 6],
[1, 3, 8],
[1, 4, 10],
[1, 5, 12]
]
y=[
0,
0,
1,
1,
2,
2
]
# dataset 2
X = [
[1, 3, 3],
[1, 3, 4],
[1, 3, 5],
[1, 4, 3],
[1, 4, 4],
[1, 4, 5],
[1, 5, 3],
[1, 5, 4],
[1, 5, 5],
[1, 3, 3],
[1, 3, 2],
[1, 3, 1],
[1, 2, 3],
[1, 2, 2],
[1, 2, 1],
[1, 1, 3],
[1, 1, 2],
[1, 1, 1],
[1, 4, 0],
[1, 4, 1],
[1, 4, 2],
[1, 5, 0],
[1, 5, 1],
[1, 5, 2],
[1, 6, 0],
[1, 6, 1],
[1, 6, 2]
]
y = [
0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2, 2
]
theta=multiclass(X, y, 0.01, 3)
print(theta)
getY(theta, [1,3.5,3.5], 3)