Logistic Regression 逻辑回归算法例子,python代码实现
2017-02-08 23:23
931 查看
转载自原文
逻辑回归 Logistic Regression
虽然名字叫做逻辑回归 Logistic regression ,但它是一种分类算法。对于文本处理方便,逻辑回归是一种非常强大的分类器。它主要通过在逻辑函数上执行回归来实现,正如其名字。
逻辑回归的一个小例子
如下图,红色表示分类1,蓝色表示分类为0.但是对于特征,横坐标上有一个区间既可以分到1也可以分到0。遇到这种情况,最好能用概率来表示分到某一类的可能性,而不是用离散的方式来表示0或1。 在数学上,向来都是很难设定模型的边界,像本例中的0和1。但是我们可以经过一些处理,使得分类的概率值落在0和1之间。
如果一个数据有0.9的可能性是属于分类1的,我们可以认为其有9:1的可能性将该数据分类为1。如果另一个数据有0.5的可能性分类为1,则认为有1:1的可能性将数据的分类为1。这种odd的表达方式的范围是0到无穷大。如果将odd加上log,取对数。则可以将odd的范围映射到0和1 之间。这样就实现了将离散的分类值转换成连续的概率值的映射。
取对数之后的曲线:
逻辑回归python代码
使用 scipy.stats的norm.rvs产生随机数,参数loc表示平均数,scale表示标准差,size是样本量np.hstack合并两个数组
import os from data import CHART_DIR import numpy as np from scipy.stats import norm from matplotlib import pyplot np.random.seed(3) num_per_class = 40 #生成样本 X = np.hstack((norm.rvs(2, size=num_per_class, scale=2), norm.rvs(8, size=num_per_class, scale=3))) y = np.hstack((np.zeros(num_per_class), np.ones(num_per_class))) def lr_model(clf, X): return 1.0 / (1.0 + np.exp(-(clf.intercept_ + clf.coef_ * X))) from sklearn.linear_model import LogisticRegression logclf = LogisticRegression() print(logclf) logclf.fit(X.reshape(num_per_class * 2, 1), y) print(np.exp(logclf.intercept_), np.exp(logclf.coef_.ravel())) print("P(x=-1)=%.2f\tP(x=7)=%.2f" % (lr_model(logclf, -1), lr_model(logclf, 7))) X_test = np.arange(-5, 20, 0.1) pyplot.figure(figsize=(10, 4)) pyplot.xlim((-5, 20)) pyplot.scatter(X, y, c=y) pyplot.xlabel("feature value") pyplot.ylabel("class") pyplot.grid(True, linestyle='-', color='0.75') pyplot.savefig( os.path.join(CHART_DIR, "log_reg_example_data.png"), bbox_inches="tight") def lin_model(clf, X): return clf.intercept_ + clf.coef_ * X from sklearn.linear_model import LinearRegression clf = LinearRegression() print(clf) clf.fit(X.reshape(num_per_class * 2, 1), y) X_odds = np.arange(0, 1, 0.001) pyplot.figure(figsize=(10, 4)) pyplot.subplot(1, 2, 1) pyplot.scatter(X, y, c=y) pyplot.plot(X_test, lin_model(clf, X_test)) pyplot.xlabel("feature value") pyplot.ylabel("class") pyplot.title("linear fit on original data") pyplot.grid(True, linestyle='-', color='0.75') X_ext = np.hstack((X, norm.rvs(20, size=100, scale=5))) y_ext = np.hstack((y, np.ones(100))) clf = LinearRegression() clf.fit(X_ext.reshape(num_per_class * 2 + 100, 1), y_ext) pyplot.subplot(1, 2, 2) pyplot.scatter(X_ext, y_ext, c=y_ext) pyplot.plot(X_ext, lin_model(clf, X_ext)) pyplot.xlabel("feature value") pyplot.ylabel("class") pyplot.title("linear fit on additional data") pyplot.grid(True, linestyle='-', color='0.75') pyplot.savefig( os.path.join(CHART_DIR, "log_reg_log_linear_fit.png"), bbox_inches="tight") pyplot.figure(figsize=(10, 4)) pyplot.xlim((-5, 20)) pyplot.scatter(X, y, c=y) pyplot.plot(X_test, lr_model(logclf, X_test).ravel()) pyplot.plot(X_test, np.ones(X_test.shape[0]) * 0.5, "--") pyplot.xlabel("feature value") pyplot.ylabel("class") pyplot.grid(True, linestyle='-', color='0.75') pyplot.savefig( os.path.join(CHART_DIR, "log_reg_example_fitted.png"), bbox_inches="tight") X = np.arange(0, 1, 0.001) pyplot.figure(figsize=(10, 4)) pyplot.subplot(1, 2, 1) pyplot.xlim((0, 1)) pyplot.ylim((0, 10)) pyplot.plot(X, X / (1 - X)) pyplot.xlabel("P") pyplot.ylabel("odds = P / (1-P)") pyplot.grid(True, linestyle='-', color='0.75') pyplot.subplot(1, 2, 2) pyplot.xlim((0, 1)) pyplot.plot(X, np.log(X / (1 - X))) pyplot.xlabel("P") pyplot.ylabel("log(odds) = log(P / (1-P))") pyplot.grid(True, linestyle='-', color='0.75') pyplot.savefig( os.path.join(CHART_DIR, "log_reg_log_odds.png"), bbox_inches="tight")
相关文章推荐
- Logistic Regression 逻辑回归算法例子,python代码实现
- [深度学习]Python/Theano实现逻辑回归网络的代码分析
- 逻辑回归原理(python代码实现)
- python实现信用卡欺诈检测 logistic回归逻辑回归算法
- 21-城里人套路深之用python实现逻辑回归算法
- 逻辑回归-线性判定边界Python代码实现
- 分类算法系列2----逻辑回归Logistic原理和Python实现
- KNN算法例子(java,scala,python 代码实现)
- 机器学习逻辑回归:算法兑现为python代码
- [深度学习]Python/Theano实现逻辑回归网络的代码分析
- 逻辑回归-非线性判定边界Python代码实现
- 机器学习-逻辑回归(python3代码实现)
- 机器学习:逻辑回归与Python代码实现
- 逻辑回归和朴素贝叶斯算法实现二值分类(matlab代码)
- 逻辑回归算法简介及用python实现
- logistic回归算法详细分析与Python代码实现注释
- 机器学习:逻辑回归python实现
- 逻辑回归-梯度下降法 python实现
- 逻辑回归(LR)算法java实现
- tf–idf算法解释及其python代码实现(下)