Skip to content

Instantly share code, notes, and snippets.

@tobigue
Created July 27, 2012 15:45

Revisions

  1. Tobias revised this gist Sep 5, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cvdiff.py
    Original file line number Diff line number Diff line change
    @@ -35,5 +35,5 @@
    clf = SGDClassifier(**best_parameters)
    scores = cross_validation.cross_val_score(clf, sample_vector, targets,
    cv=cv, score_func=score_func)
    print 'Best %s: %0.2f (+/- %0.2f)' % \
    print 'Best %s: %0.3f (+/- %0.2f)' % \
    (score_func.__name__, scores.mean(), scores.std() / 2)
  2. Tobias created this gist Jul 27, 2012.
    39 changes: 39 additions & 0 deletions cvdiff.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    from sklearn.linear_model import SGDClassifier
    from sklearn import cross_validation
    from sklearn import metrics
    from sklearn.grid_search import GridSearchCV

    from sklearn.datasets import load_iris

    data = load_iris()
    sample_vector = data.data
    targets = data.target

    cv = cross_validation.StratifiedKFold(targets, 10)
    score_func = metrics.f1_score

    parameters = {
    'seed': [0],
    'loss': ('log', 'hinge'),
    'penalty': ['l1', 'l2', 'elasticnet'],
    'alpha': [0.001, 0.0001, 0.00001, 0.000001]
    }

    print
    print "GRID SEARCH:"
    grid_search = GridSearchCV(SGDClassifier(), parameters,
    score_func=score_func, cv=cv)
    grid_search.fit(sample_vector, targets)
    print "Best %s: %0.3f" % (score_func.__name__, grid_search.best_score_)
    print "Best parameters set:"
    best_parameters = grid_search.best_estimator_.get_params()
    for param_name in sorted(parameters.keys()):
    print "\t%s: %r" % (param_name, best_parameters[param_name])

    print
    print "CROSS VALIDATION:"
    clf = SGDClassifier(**best_parameters)
    scores = cross_validation.cross_val_score(clf, sample_vector, targets,
    cv=cv, score_func=score_func)
    print 'Best %s: %0.2f (+/- %0.2f)' % \
    (score_func.__name__, scores.mean(), scores.std() / 2)