-
Notifications
You must be signed in to change notification settings - Fork 0
/
logregmodel.py
181 lines (156 loc) · 4.74 KB
/
logregmodel.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
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
177
178
179
180
181
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
File name: logregmodel.py
Description: Class for fitting a logistic regression.
Author: Louis LIMNAVONG, Alessandro GIRELLI
Date created: 2018/10/08
Python Version: 3.6
"""
import numpy as np
from scipy import optimize
from scipy import special as scisp
from mydataset import MyDataSet
from preprocessing import get_dummies, full_one_hot_encoder, to_matrix
class LogRegModel:
"""
Summary
-------
Fit a logistic regression.
"""
def __init__(self):
pass
def fit(self, Y_full, X_train): # Y_full is a dictionary
"""
Summary
-------
Function to fit a logistic regression.
Parameters
----------
self: LogRegModel instance
Y_full: 'dict'
Dictionary of response variable.
X_train: 'numpy matrix'
Matrix of covariates.
"""
self.coef = dict()
Y_name = str(list(Y_full.keys())[0])
SubDict = get_dummies(Y_full, Y_name)
categories = list(set(Y_full[Y_name]))
for cat in categories:
Y_train = SubDict[cat]
self.fit_binary(Y_train, X_train, cat)
def fit_binary(self, Y_train, X_train, name):
"""
Summary
-------
Function to fit a binary logistic regression.
Parameters
----------
self: LogRegModel instance
Y_train: 'numpy array'
Response variable vector.
X_train: 'numpy matrix'
Matrix of covariates.
Returns
-------
res.x: 'list'
Coefficients of the features.
"""
m, p = X_train.shape
intercept = np.ones(m)
X_one = np.column_stack((intercept, X_train))
n, d = X_one.shape
init_w = np.zeros(d)
res = optimize.minimize(LogRegModel.neg_loglikelihood, init_w,
method='BFGS', args=(Y_train, X_one))
self.coef[name] = res.x
return res.x
@staticmethod
def neg_loglikelihood(beta, Y, X):
"""
Summary
-------
Loss function of the logistic regression.
Parameters
----------
beta: 'numpy array'
Parameters of the logistic regression.
Y: 'numpy array'
Response variable vector.
X: 'numpy matrix'
Matrix of covariates.
Returns
-------
Loss function.
"""
# sum without NAs
return -np.nansum(Y*np.matmul(X,beta) - scisp.log1p(1+scisp.expm1(np.matmul(X,beta))))
def predict(self, X):
"""
Summary
-------
Predict response variable.
Parameters
----------
self: LogRegModel instance.
X: 'numpy matrix'
Matrix of covariates.
Returns
-------
prediction: 'list'
Response variable prediction.
"""
m, p = X.shape
intercept = np.ones(m)
X = np.column_stack((intercept, X))
categories = list(set(self.coef.keys()))
num_cat = len(categories)
pred_probas = np.zeros((num_cat, m))
for i in range(num_cat):
cat = categories[i]
coef = self.coef[cat]
proba = np.exp(np.matmul(X, coef))/(1+(np.exp(np.matmul(X, coef))))
pred_probas[i] = proba
pred_probas = np.transpose(pred_probas)
labels = np.argmax(pred_probas, axis=1)
labels = [categories[lab] for lab in labels]
self.prediction = labels
return labels
def accuracy_score(self, Y_true):
"""
Summary
-------
Accuracy score of prediction.
Parameters
----------
self: LogRegModel instance.
Y_true: 'list'
Observed response variable.
Returns
-------
accuracy: 'float'
Accuracy of prediction.
"""
return (len([i for i, j in zip(self.prediction, Y_true) if i == j])
/ len(Y_true))
if __name__ == '__main__':
###
dataset_train = MyDataSet().read_csv('resources/dataset_train.csv')
# getting X
DictX = dataset_train[['Best Hand', 'Astronomy', 'Herbology',
'Defense Against the Dark Arts',
'Muggle Studies', 'Ancient Runes',
'History of Magic', 'Transfiguration',
'Charms', 'Flying']]
DictX_encod = full_one_hot_encoder(DictX)
X = to_matrix(DictX_encod)
# getting Y
Y = dataset_train['Hogwarts House']
###
model = LogRegModel()
model.fit(Y, X_train=X)
Y_test = list(Y.values())[0]
preds = model.predict(X[:])
print(preds[:10])
print(model.accuracy_score(Y_test))