-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquora_lr.py
98 lines (73 loc) · 2.82 KB
/
quora_lr.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
import quora_classifiers as qc
import numpy as np
from sklearn.base import BaseEstimator
from sklearn.base import ClassifierMixin
from sklearn.externals import six
from abc import ABCMeta
class QuoraMlLR(qc.QuoraClassifier):
"""
Class encapsulating a custom implementation of a Logistic
Regression classifier.
"""
def __init__(self, features, targets):
classifier = LogisticRegression()
qc.QuoraClassifier.__init__(self, classifier, features, targets)
class LogisticRegression(six.with_metaclass(ABCMeta, BaseEstimator, ClassifierMixin)):
"""
Custom implementation of a Logistic Regression classifier
done during the first assignment of the course CSC2515.
"""
def __init__(self, eps=0.05, l2=0.05, iterations=2000):
self.eps = eps
self.l2 = l2
self.iterations = iterations
def score(self, features, targets):
"""
Compute the score associated to the provided dataset. It follows the
protocol imposed by scikit-learn classifiers (BaseEstimator).
"""
predicted_target = self.predict(features)
num_samples = targets.shape[0]
fraction_correct = 1.0 * ((predicted_target>.5)==(targets==1)).sum(0) / num_samples
return fraction_correct
def fit(self, train_features, train_targets):
"""
Train the current classifier.
"""
# seed the random number generator so results are the same each run.
np.random.seed(1)
self.coef_ = np.zeros(train_features.shape[1])
for t in xrange(self.iterations):
self.update_weights(train_features, train_targets)
def predict(self, features):
"""
Perform prediction of targets given an array of features.
"""
return self.sigmoid(self.raw_predict(features))
def raw_predict(self, features):
"""
Raw prediction for Logistic Regression classifier.
"""
features_bias = features
return np.dot(features_bias, self.coef_)
def update_weights(self, train_features, train_targets):
"""
Training logic associated to model parameters update.
"""
predicted_target = self.predict(train_features)
error = train_targets - predicted_target
train_features_bias = train_features
dw = np.dot(train_features_bias.T, error) + 2*self.l2*self.coef_
self.coef_ += self.eps * dw
def neg_log_likelihood(self, data, labels):
"""
Compute the negative likelihood.
"""
z = self.raw_predict(data)
ll = labels*z - np.log(1 + np.exp(z))
return - ll.sum(0) + (self.l2 * (np.linalg.norm(self.coef_)**2))
def sigmoid(self, a):
"""
Compute sigmoid function for a given number/array of values.
"""
return 1.0 / (1.0 + np.exp(-a))