Logistic regression (logistic Regression, LR), also known as logistic regression analysis, is one of the classification and prediction algorithms. The probability of future results is predicted by the performance of historical data. For example, we can set the probability of a purchase as the dependent variable, setting the user's characteristic attributes, such as gender, age, registration time, and so on as arguments. predict the probability of purchase based on feature attributes.
Predictive models for logistic regression:
Logistic regression is not a regression problem, but a two classification problem, because the variable is not 0 or 1, then we naturally think that the probability function obeys the Bernoulli distribution, and the exponential form of the Bernoulli distribution is a sigmoid function.
The function hθ (x) indicates the probability that the result takes 1, then the probabilities for the classifications 1 and 0 are:
The experiment code is as follows:
Import NumPy as NP import Matplotlib.pyplot as PLT from Sklearn import Linear_model, Datasets # import some data to play
With Iris = Datasets.load_iris () X = iris.data[:,: 2] # We have take the first and the first and the features. Y = Iris.target h =. # # step size in the mesh Logreg = Linear_model.
Logisticregression (c=1e5) # We create an instance of Neighbours Classifier and fit the data. Logreg.fit (X, Y) # Plot the decision boundary.
For that, we'll assign a color to each of the # in the mesh [X_min, X_max]x[y_min, Y_max]. X_min, X_max = x[:, 0].min ()-. 5, x[:, 0].max () +. 5 y_min, Y_max = x[:, 1].min ()-. 5, x[:, 1].max () +. 5 xx, yy = np.me Shgrid (Np.arange (X_min, X_max, h), Np.arange (Y_min, Y_max, h)) Z = Logreg.predict (Np.c_[xx.ravel (), Yy.ravel ()]) # Put th e result into a color plot Z = Z.reshape (Xx.shape) plt.figure (1, figsize= (4, 3)) Plt.pcolormesh (xx, yy, Z, Cmap=plt.cm.pai Red) # Plot also the training points plt.scatter (x[:, 0], x[:, 1], c=y, edgecolors= ' K ', cmap=plt.cm.paired) plT.xlabel (' sepal length ') Plt.ylabel (' sepal width ') plt.xlim (Xx.min (), Xx.max ()) Plt.ylim (Yy.min (), Yy.max ()) Plt.xticks (()) Plt.yticks (()) Plt.show ()
The test results are as follows: