Python - 使用scikit绘制几条学习曲线 - 在同一图上学习

时间:2015-01-23 11:21:04

标签: python matplotlib plot scikit-learn

我正在编写这个程序来绘制不同数据集上SVM和NB的学习曲线,这是绘制传递数据集的学习曲线的函数:

import numpy as np
import matplotlib.pyplot as plt
from sklearn import cross_validation
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from sklearn.datasets import load_digits
from sklearn.datasets import fetch_mldata
from sklearn.learning_curve import learning_curve


def plot_learning_curve(plotter, X, y, ylim=None, cv=None,
                    n_jobs=1, train_sizes=np.linspace(.1, 1.0, 5)):
    if ylim is not None:
        plt.ylim(*ylim)

estimator1 = GaussianNB()
estimator2 = SVC(kernel='linear')
train_sizes1, train_scores1, test_scores1 = learning_curve(
    estimator1, X, y, cv=cv, n_jobs=n_jobs, train_sizes=train_sizes)
train_sizes2, train_scores2, test_scores2 = learning_curve(
    estimator2, X, y, cv=cv, n_jobs=n_jobs, train_sizes=train_sizes)
test_scores_mean1 = np.mean(test_scores1, axis=1)
test_scores_mean2 = np.mean(test_scores2, axis=1)

plotter.plot(train_sizes2, test_scores_mean2, 'x-', color="b", label="linear SVM")
plotter.plot(train_sizes1, test_scores_mean1, 'x-', color="r",
             label="NB")

plt.legend(loc="best")
return plotter

这是对2个不同数据集的函数调用:

plt.figure()
plt.xlabel("Training examples")
plt.ylabel("Score")
plt.title("Comparison between NB(Naive Bayes) and SVM(Support Vector Machine)")
labels = ['sonar', 'iris']
plt.grid()
sonar = fetch_mldata('sonar', target_name='target', data_name='data',        transpose_data=True)
iris = fetch_mldata('iris', target_name='target', data_name='data', transpose_data=True)
X, y = sonar.data, sonar.target
x1, y1 = iris.data, iris.target



cv = cross_validation.ShuffleSplit(sonar.data.shape[0], n_iter=10,
                               test_size=0.2, random_state=0)

ax1 = plt.subplot(211)
plot_learning_curve(ax1, X, y, ylim=(0.5, 1.01), cv=cv, n_jobs=1)

cv = cross_validation.ShuffleSplit(iris.data.shape[0], n_iter=10,
                               test_size=0.2, random_state=0)
ax2 = plt.subplot(212, sharex=ax1)
plot_learning_curve(ax2, x1, y1, ylim=(0.5, 1.01), cv=cv, n_jobs=1)

plt.show()

绘图功能工作得很好,问题是程序在同一个图像中绘制两个调用的图形。现在我想在几个数据集上运行它,它应该显示图像的研磨,每次迭代一个该功能,在同一个窗口中。

我真的不明白subplot或add_subplot是如何工作的,所以如果有人能帮助我,我将非常感激。

0 个答案:

没有答案