ml-finance-python
python scripts for finance machine learning
git clone https://9o.is/git/ml-finance-python.git
logistic_regression.py
(2059B)
1 from __future__ import print_function, division
2 import numpy as np
3 import math
4 from mlfromscratch.utils import make_diagonal, Plot
5 from mlfromscratch.deep_learning.activation_functions import Sigmoid
6
7
8 class LogisticRegression():
9 """ Logistic Regression classifier.
10 Parameters:
11 -----------
12 learning_rate: float
13 The step length that will be taken when following the negative gradient during
14 training.
15 gradient_descent: boolean
16 True or false depending if gradient descent should be used when training. If
17 false then we use batch optimization by least squares.
18 """
19 def __init__(self, learning_rate=.1, gradient_descent=True):
20 self.param = None
21 self.learning_rate = learning_rate
22 self.gradient_descent = gradient_descent
23 self.sigmoid = Sigmoid()
24
25 def _initialize_parameters(self, X):
26 n_features = np.shape(X)[1]
27 # Initialize parameters between [-1/sqrt(N), 1/sqrt(N)]
28 limit = 1 / math.sqrt(n_features)
29 self.param = np.random.uniform(-limit, limit, (n_features,))
30
31 def fit(self, X, y, n_iterations=4000):
32 self._initialize_parameters(X)
33 # Tune parameters for n iterations
34 for i in range(n_iterations):
35 # Make a new prediction
36 y_pred = self.sigmoid(X.dot(self.param))
37 if self.gradient_descent:
38 # Move against the gradient of the loss function with
39 # respect to the parameters to minimize the loss
40 self.param -= self.learning_rate * -(y - y_pred).dot(X)
41 else:
42 # Make a diagonal matrix of the sigmoid gradient column vector
43 diag_gradient = make_diagonal(self.sigmoid.gradient(X.dot(self.param)))
44 # Batch opt:
45 self.param = np.linalg.pinv(X.T.dot(diag_gradient).dot(X)).dot(X.T).dot(diag_gradient.dot(X).dot(self.param) + y - y_pred)
46
47 def predict(self, X):
48 y_pred = np.round(self.sigmoid(X.dot(self.param))).astype(int)
49 return y_pred