From be26414de2e2e48c27f5159e688433dcbc344237 Mon Sep 17 00:00:00 2001 From: jiangzhonglian Date: Mon, 10 Jul 2017 12:55:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0svm=E7=94=BB=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/python/6.SVM/sklearn-svm-demo.py | 5 +- .../7.AdaBoost/sklearn-adaboost-demo.py | 91 +++++++++++++++++++ 2 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 src/python/7.AdaBoost/sklearn-adaboost-demo.py diff --git a/src/python/6.SVM/sklearn-svm-demo.py b/src/python/6.SVM/sklearn-svm-demo.py index 88e7aa28..23f42a58 100644 --- a/src/python/6.SVM/sklearn-svm-demo.py +++ b/src/python/6.SVM/sklearn-svm-demo.py @@ -7,6 +7,7 @@ Updated on 2017-06-28 SVM:最大边距分离超平面 @author: 片刻 《机器学习实战》更新地址:https://github.com/apachecn/MachineLearning +sklearn-SVM译文链接: http://cwiki.apachecn.org/pages/viewpage.action?pageId=10031359 """ import numpy as np import matplotlib.pyplot as plt @@ -52,7 +53,7 @@ clf.fit(X, Y) # 获取分割超平面 w = clf.coef_[0] # 斜率 -a = -w[0] / w[1] +a = -w[0]/w[1] # 从-5到5,顺序间隔采样50个样本,默认是num=50 # xx = np.linspace(-5, 5) # , num=50) xx = np.linspace(-2, 10) # , num=50) @@ -74,7 +75,7 @@ plt.plot(xx, yy_down, 'k--') plt.plot(xx, yy_up, 'k--') plt.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1], s=80, facecolors='none') -plt.scatter(X[:, 0], X[:, 1], c=Y, cmap=plt.cm.Paired) +plt.scatter([X[:, 0]], [X[:, 1]], c=Y, cmap=plt.cm.Paired) plt.axis('tight') plt.show() diff --git a/src/python/7.AdaBoost/sklearn-adaboost-demo.py b/src/python/7.AdaBoost/sklearn-adaboost-demo.py new file mode 100644 index 00000000..6d3b1ac2 --- /dev/null +++ b/src/python/7.AdaBoost/sklearn-adaboost-demo.py @@ -0,0 +1,91 @@ +#!/usr/bin/python +# coding:utf8 + +""" +Created on 2017-07-10 +Updated on 2017-07-10 +@author: 片刻 +《机器学习实战》更新地址:https://github.com/apachecn/MachineLearning +sklearn-AdaBoost译文链接: http://cwiki.apachecn.org/pages/viewpage.action?pageId=10813457 +""" +print(__doc__) + +# Author: Noel Dawe +# +# License: BSD 3 clause + +import matplotlib.pyplot as plt +import numpy as np +from sklearn.datasets import make_gaussian_quantiles +from sklearn.ensemble import AdaBoostClassifier +from sklearn.tree import DecisionTreeClassifier + +# Construct dataset +X1, y1 = make_gaussian_quantiles(cov=2., + n_samples=200, n_features=2, + n_classes=2, random_state=1) +X2, y2 = make_gaussian_quantiles(mean=(3, 3), cov=1.5, + n_samples=300, n_features=2, + n_classes=2, random_state=1) +X = np.concatenate((X1, X2)) +y = np.concatenate((y1, - y2 + 1)) + +# Create and fit an AdaBoosted decision tree +bdt = AdaBoostClassifier(DecisionTreeClassifier(max_depth=1), + algorithm="SAMME", + n_estimators=200) + +bdt.fit(X, y) + +plot_colors = "br" +plot_step = 0.02 +class_names = "AB" + +plt.figure(figsize=(10, 5)) + +# Plot the decision boundaries +plt.subplot(121) +x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 +y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1 +xx, yy = np.meshgrid(np.arange(x_min, x_max, plot_step), + np.arange(y_min, y_max, plot_step)) + +Z = bdt.predict(np.c_[xx.ravel(), yy.ravel()]) +Z = Z.reshape(xx.shape) +cs = plt.contourf(xx, yy, Z, cmap=plt.cm.Paired) +plt.axis("tight") + +# Plot the training points +for i, n, c in zip(range(2), class_names, plot_colors): + idx = np.where(y == i) + plt.scatter(X[idx, 0], X[idx, 1], + c=c, cmap=plt.cm.Paired, + label="Class %s" % n) +plt.xlim(x_min, x_max) +plt.ylim(y_min, y_max) +plt.legend(loc='upper right') +plt.xlabel('x') +plt.ylabel('y') +plt.title('Decision Boundary') + +# Plot the two-class decision scores +twoclass_output = bdt.decision_function(X) +plot_range = (twoclass_output.min(), twoclass_output.max()) +plt.subplot(122) +for i, n, c in zip(range(2), class_names, plot_colors): + plt.hist(twoclass_output[y == i], + bins=10, + range=plot_range, + facecolor=c, + label='Class %s' % n, + alpha=.5) +x1, x2, y1, y2 = plt.axis() +plt.axis((x1, x2, y1, y2 * 1.2)) +plt.legend(loc='upper right') +plt.ylabel('Samples') +plt.xlabel('Score') +plt.title('Decision Scores') + +plt.tight_layout() +plt.subplots_adjust(wspace=0.35) +plt.show()