In [1]:
%matplotlib inline
import pylab as plt
In [2]:
#import matplotlib.pyplot as plt
from scipy.stats import randint
from scipy.stats import uniform
import numpy as np
from scipy import stats
def plot_learning_curve(estimator, title, X, y, ylim=None, cv=None,
n_jobs=1, train_sizes=np.linspace(.1, 1.0, 10)):
"""
Generate a simple plot of the test and traning learning curve.
Parameters
----------
estimator : object type that implements the "fit" and "predict" methods
An object of that type which is cloned for each validation.
title : string
Title for the chart.
X : array-like, shape (n_samples, n_features)
Training vector, where n_samples is the number of samples and
n_features is the number of features.
y : array-like, shape (n_samples) or (n_samples, n_features), optional
Target relative to X for classification or regression;
None for unsupervised learning.
ylim : tuple, shape (ymin, ymax), optional
Defines minimum and maximum yvalues plotted.
cv : integer, cross-validation generator, optional
If an integer is passed, it is the number of folds (defaults to 3).
Specific cross-validation objects can be passed, see
sklearn.cross_validation module for the list of possible objects
n_jobs : integer, optional
Number of jobs to run in parallel (default 1).
"""
plt.figure(figsize=(12,6))
plt.title(title)
if ylim is not None:
plt.ylim(*ylim)
plt.xlabel("Training examples")
plt.ylabel("Score")
train_sizes, train_scores, test_scores = learning_curve(estimator, X, y, cv=cv, n_jobs=n_jobs, train_sizes=train_sizes)
train_scores_mean = np.mean(train_scores, axis=1)
train_scores_std = np.std(train_scores, axis=1)
test_scores_mean = np.mean(test_scores, axis=1)
test_scores_std = np.std(test_scores, axis=1)
plt.grid()
plt.fill_between(train_sizes, train_scores_mean - train_scores_std,
train_scores_mean + train_scores_std, alpha=0.1,
color="r")
plt.fill_between(train_sizes, test_scores_mean - test_scores_std,
test_scores_mean + test_scores_std, alpha=0.1, color="g")
plt.plot(train_sizes, train_scores_mean, 'o-', color="r",
label="Training score")
plt.plot(train_sizes, test_scores_mean, 'o-', color="g",
label="Cross-validation score")
plt.legend(loc="best")
return plt
In [3]:
input_target_url = 'http://www.bioinf.uni-freiburg.de/~costa/bursi.target'
from eden.util import load_target
y = load_target(input_target_url)
In [4]:
#load data
input_data_url = 'http://www.bioinf.uni-freiburg.de/~costa/bursi.gspan'
from eden.converter.graph.gspan import gspan_to_eden
graphs = gspan_to_eden(input_data_url)
In [6]:
from eden.graph import Vectorizer
vectorizer = Vectorizer( complexity = 3)
In [7]:
%%time
from eden import vectorize
X = vectorize(graphs, vectorizer=vectorizer)
print 'Instances: %d Features: %d with an avg of %d features per instance' % (X.shape[0], X.shape[1], X.getnnz()/X.shape[0])
In [8]:
%%time
from sklearn.linear_model import SGDClassifier
clf = SGDClassifier()
clf.fit(X, y)
from sklearn import cross_validation
scores = cross_validation.cross_val_score(clf, X, y,cv = 10, scoring = 'roc_auc', n_jobs = -1)
print('AUC ROC: %.4f +- %.4f' % (np.mean(scores),np.std(scores)))
In [9]:
%%time
from sklearn.grid_search import RandomizedSearchCV
from sklearn.learning_curve import learning_curve
title = "Learning Curve"
# Cross validation with 100 iterations to get smoother mean test and train
# score curves, each time with 20% data randomly selected as a validation set.
cv = cross_validation.ShuffleSplit(y.shape[0], n_iter = 50, test_size = 0.15, random_state = 0)
estimator = clf
plot_learning_curve(estimator, title, X, y, ylim = (0.65, 1.01), cv = cv, n_jobs = -1, train_sizes = np.linspace(.1, 1.0, 10))
plt.show()