From 958cfbe131e95e099e33bf24aa0adda504ee743e Mon Sep 17 00:00:00 2001 From: Andreas Mueller <amueller@ais.uni-bonn.de> Date: Fri, 27 Apr 2012 22:53:09 +0200 Subject: [PATCH] COSMIT cleanup + pep8 in examples --- examples/exercises/plot_cv_diabetes.py | 10 +++--- examples/exercises/plot_iris_exercise.py | 45 +++++++++++------------- examples/linear_model/plot_logistic.py | 16 +++++---- 3 files changed, 33 insertions(+), 38 deletions(-) diff --git a/examples/exercises/plot_cv_diabetes.py b/examples/exercises/plot_cv_diabetes.py index 6a0182afaa..65efd6fd99 100644 --- a/examples/exercises/plot_cv_diabetes.py +++ b/examples/exercises/plot_cv_diabetes.py @@ -34,17 +34,15 @@ pl.figure(1, figsize=(2.5, 2)) pl.clf() pl.axes([.1, .25, .8, .7]) pl.semilogx(alphas, scores) -pl.semilogx(alphas, np.array(scores) + np.array(scores_std)/20, 'b--') -pl.semilogx(alphas, np.array(scores) - np.array(scores_std)/20, 'b--') +pl.semilogx(alphas, np.array(scores) + np.array(scores_std) / 20, 'b--') +pl.semilogx(alphas, np.array(scores) - np.array(scores_std) / 20, 'b--') pl.yticks(()) pl.ylabel('CV score') pl.xlabel('alpha') pl.axhline(np.max(scores), linestyle='--', color='.5') -pl.text(2e-4, np.max(scores)+1e-4, '.489') +pl.text(2e-4, np.max(scores) + 1e-4, '.489') -################################################################################ +############################################################################## # Bonus: how much can you trust the selection of alpha? -from scikits.learn import cross_val k_fold = cross_validation.KFold(len(X), 3) print [lasso.fit(X[train], y[train]).alpha for train, _ in k_fold] - diff --git a/examples/exercises/plot_iris_exercise.py b/examples/exercises/plot_iris_exercise.py index d0984b066f..72bb6df0db 100644 --- a/examples/exercises/plot_iris_exercise.py +++ b/examples/exercises/plot_iris_exercise.py @@ -3,25 +3,22 @@ SVM Exercise ================================ -This exercise is used in the -:ref:`using_kernels_tut` part of the -:ref:`supervised_learning_tut` section of the -:ref:`stat_learn_tut_index`. +This exercise is used in the :ref:`using_kernels_tut` part of the +:ref:`supervised_learning_tut` section of the :ref:`stat_learn_tut_index`. """ print __doc__ - import numpy as np import pylab as pl -from scikits.learn import datasets, svm +from sklearn import datasets, svm iris = datasets.load_iris() X = iris.data y = iris.target -X = X[y!=0, :2] -y = y[y!=0, :2] +X = X[y != 0, :2] +y = y[y != 0, :2] n_sample = len(X) @@ -30,12 +27,12 @@ order = np.random.permutation(n_sample) X = X[order] y = y[order].astype(np.float) -X_train = X[:.9*n_sample] -y_train = y[:.9*n_sample] -X_test = X[.9*n_sample:] -y_test = y[.9*n_sample:] +X_train = X[:.9 * n_sample] +y_train = y[:.9 * n_sample] +X_test = X[.9 * n_sample:] +y_test = y[.9 * n_sample:] -h = .02 # step size in the mesh +h = .02 # step size in the mesh # fit the model for fig_num, kernel in enumerate(('linear', 'rbf', 'poly')): @@ -45,19 +42,19 @@ for fig_num, kernel in enumerate(('linear', 'rbf', 'poly')): pl.figure(fig_num) pl.clf() - pl.scatter(X[:,0], X[:,1], c=y, zorder=10) + pl.scatter(X[:, 0], X[:, 1], c=y, zorder=10) # Circle out the test data - pl.scatter(X_test[:,0], X_test[:, 1], + pl.scatter(X_test[:, 0], X_test[:, 1], s=80, facecolors='none', zorder=10) pl.axis('tight') - x_min = X[:,0].min() - x_max = X[:,0].max() - y_min = X[:,1].min() - y_max = X[:,1].max() - y_min = X[:,1].min() - y_max = X[:,1].max() + x_min = X[:, 0].min() + x_max = X[:, 0].max() + y_min = X[:, 1].min() + y_max = X[:, 1].max() + y_min = X[:, 1].min() + y_max = X[:, 1].max() XX, YY = np.mgrid[x_min:x_max:200j, y_min:y_max:200j] Z = clf.decision_function(np.c_[XX.ravel(), YY.ravel()]) @@ -65,10 +62,8 @@ for fig_num, kernel in enumerate(('linear', 'rbf', 'poly')): # Put the result into a color plot Z = Z.reshape(XX.shape) pl.pcolormesh(XX, YY, Z > 0) - pl.contour(XX, YY, Z, colors=['k', 'k', 'k'], - linestyles=['--', '-', '--'], + pl.contour(XX, YY, Z, colors=['k', 'k', 'k'], + linestyles=['--', '-', '--'], levels=[-.5, 0, .5]) pl.title(kernel) - - diff --git a/examples/linear_model/plot_logistic.py b/examples/linear_model/plot_logistic.py index ff1adbd098..6e5089357a 100644 --- a/examples/linear_model/plot_logistic.py +++ b/examples/linear_model/plot_logistic.py @@ -7,7 +7,7 @@ Logit function ========================================================= Show in the plot is how the logistic regression would, in this -synthetic dataset, classify values as either 0 or 1, +synthetic dataset, classify values as either 0 or 1, i.e. class one or two, using the logit-curve. """ @@ -20,7 +20,7 @@ print __doc__ import numpy as np import pylab as pl -from scikits.learn import linear_model +from sklearn import linear_model # this is our test set, it's just a straight line with some # gaussian noise @@ -29,8 +29,8 @@ n_samples = 100 np.random.seed(0) X = np.random.normal(size=n_samples) y = (X > 0).astype(np.float) -X[X>0] *= 4 -X += .3*np.random.normal(size=n_samples) +X[X > 0] *= 4 +X += .3 * np.random.normal(size=n_samples) X = X[:, np.newaxis] # run the classifier @@ -42,14 +42,16 @@ pl.figure(1, figsize=(4, 3)) pl.clf() pl.scatter(X.ravel(), y, color='black', zorder=20) X_test = np.linspace(-5, 10, 300) + + def model(x): - return 1/(1+np.exp(-x)) -loss = model(X_test*clf.coef_ + clf.intercept_).ravel() + return 1 / (1 + np.exp(-x)) +loss = model(X_test * clf.coef_ + clf.intercept_).ravel() pl.plot(X_test, loss, color='blue', linewidth=3) ols = linear_model.LinearRegression() ols.fit(X, y) -pl.plot(X_test, ols.coef_*X_test + ols.intercept_, linewidth=1) +pl.plot(X_test, ols.coef_ * X_test + ols.intercept_, linewidth=1) pl.axhline(.5, color='.5') pl.ylabel('y') -- GitLab