| import numpy as np |
| import matplotlib.pyplot as plt |
| from matplotlib.colors import ListedColormap |
| from matplotlib.font_manager import FontProperties |
| from sklearn.datasets import load_iris |
| from sklearn.ensemble import GradientBoostingClassifier |
| from sklearn import metrics |
| from sklearn.model_selection import GridSearchCV |
| %matplotlib inline |
| font = FontProperties(fname='/Library/Fonts/Heiti.ttc') |
| iris_data = load_iris() |
| X = iris_data.data[0:100, [2, 3]] |
| y = iris_data.target[0:100] |
| label_list = ['山鸢尾', '杂色鸢尾'] |
| def plot_decision_regions(X, y, classifier=None): |
| marker_list = ['o', 'x', 's'] |
| color_list = ['r', 'b', 'g'] |
| cmap = ListedColormap(color_list[:len(np.unique(y))]) |
| |
| x1_min, x1_max = X[:, 0].min()-1, X[:, 0].max()+1 |
| x2_min, x2_max = X[:, 1].min()-1, X[:, 1].max()+1 |
| t1 = np.linspace(x1_min, x1_max, 666) |
| t2 = np.linspace(x2_min, x2_max, 666) |
| |
| x1, x2 = np.meshgrid(t1, t2) |
| y_hat = classifier.predict(np.array([x1.ravel(), x2.ravel()]).T) |
| y_hat = y_hat.reshape(x1.shape) |
| plt.contourf(x1, x2, y_hat, alpha=0.2, cmap=cmap) |
| plt.xlim(x1_min, x1_max) |
| plt.ylim(x2_min, x2_max) |
| |
| for ind, clas in enumerate(np.unique(y)): |
| plt.scatter(X[y == clas, 0], X[y == clas, 1], alpha=0.8, s=50, |
| c=color_list[ind], marker=marker_list[ind], label=label_list[clas]) |
| gbc = GradientBoostingClassifier(random_state=1) |
| gbc.fit(X, y) |
| y_pred = gbc.predict(X) |
| y_predprob = gbc.predict_proba(X)[:, 1] |
| print("精准度:{:.4f}".format(metrics.accuracy_score(y, y_pred))) |
| print("AUC分数(训练集):{:.4f}".format(metrics.roc_auc_score(y, y_predprob))) |
| 精准度:1.0000 |
| AUC分数(训练集):1.0000 |
| plot_decision_regions(X, y, classifier=gbc) |
| plt.xlabel('花瓣长度(cm)', fontproperties=font) |
| plt.ylabel('花瓣宽度(cm)', fontproperties=font) |
| plt.title('梯度提升法算法代码(鸢尾花分类)', |
| fontproperties=font, fontsize=20) |
| plt.legend(prop=font) |
| plt.show() |