This article describes how to implement logistic regression in python. this is an experiment of the machine learning course. you can share the experiment with us. This article describes how to implement logistic regression in python. this is an experiment of the machine learning course. you can share the experiment with us.
The implementation principle in this article is very simple. the optimization method is gradient descent. The test results are shown later.
Let's take a look at the implementation sample code:
# Coding = utf-8from math import expimport matplotlib. pyplot as pltimport numpy as npfrom sklearn. datasets. samples_generator import make_blobsdef sigmoid (num): ''': param num: value after x: return: sigmoid: ''' if type (num) = int or type (num) = float: return 1.0/(1 + exp (-1 * num) else: raise ValueError, 'only int or float data can compute sigmoid' class logistic (): def init (self, x, y): if type (x) = type (y) = list: self. x = np. array (x) self. y = np. array (y) elif type (x) = type (y) = np. ndarray: self. x = x self. y = y else: raise ValueError, 'input data error' def sigmoid (self, x): ''': param x: input vector: return: the result of the simgoid calculation for the input vector is ''s = np. frompyfunc (lambda x: sigmoid (x), 1, 1) return s (x) def train_with_punish (self, alpha, errors, punish = 0.0001): ''': param alpha: alpha is the learning rate: param errors: Threshold for stopping iteration when the error is less than: param punish: penalty coefficient: param times: maximum number of iterations: return: '''self. punish = punish dimension = self. x. shape [1] self. theta = np. random. random (dimension) compute_error = 100000000 times = 0 while compute_error> errors: res = np. dot (self. x, self. theta) delta = self. sigmoid (res)-self. y self. theta = self. theta-alpha * np. dot (self. x. t, delta)-punish * self. theta # gradient descent method with penalty compute_error = np. sum (delta) times + = 1 def predict (self, x): ''': param x: returns a new unlabeled vector: return: return the determined class ''' x = np based on the calculated parameter. array (x) if self. sigmoid (np. dot (x, self. theta)> 0.5: return 1 else: return 0def test1 (): ''' is used for testing and drawing. The result is returned: ''' x, y = make_blobs (n_samples = 200, centers = 2, n_features = 2, random_state = 0, center_box = (10, 20 )) x1 = [] y1 = [] x2 = [] y2 = [] for I in range (len (y): if y [I] = 0: x1.append (x [I] [0]) y1.append (x [I] [1]) elif y [I] = 1: x2.append (x [I] [0]) y2.append (x [I] [1]) # The above data is processed. two types of data are generated: p = logistic (x, y) p. train_with_punish (alpha = 0.00001, errors = 0.005, punish = 0.01) # The step size is 0.00001, the maximum allowable error is 0.005, and the penalty coefficient is 0.01 x_test = np. arange (10, 20, 0.01) y_test = (-1 * p. theta [0]/p. theta [1]) * x_test plt. plot (x_test, y_test, c = 'G', label = 'logistic _ line') plt. scatter (x1, y1, c = 'R', label = 'positive ') plt. scatter (x2, y2, c = 'B', label = 'negative ') plt. legend (loc = 2) plt. title ('punish value = '+ p. punish. str () plt. show () if name = 'main': test1 ()
The running result is as follows:
Summary