<commit_before>"""Run parameter estimation using grid search
in a nested cross-validation setting.
"""

import numpy as np
from scikits.learn.svm import SVC
from scikits.learn.cross_val import StratifiedKFold
from scikits.learn import datasets
from scikits.learn.grid_search import GridSearchCV

# The Digits dataset
digits = datasets.load_digits()

# To apply an classifier on this data, we need to flatten the image, to
# turn the data in a (samples, feature) matrix:
n_samples = len(digits.images)
X = digits.images.reshape((n_samples, -1))
y = digits.target

parameters = {'kernel':('linear', 'rbf'), 'C':[0.1, 1]}

def loss_func(y1, y2):
    return np.mean(y1 != y2)

svc = SVC()
clf = GridSearchCV(svc, parameters, loss_func, n_jobs=2)

"""
Run crossvalidation (different than the nested crossvalidation that is used
to select the best classifier). The classifier is optimized by "nested"
crossvalidation
"""
n_samples, n_features = X.shape
y_pred = []
y_true = []
for train, test in StratifiedKFold(y, 2):
    cv = StratifiedKFold(y[train], 2)
    y_pred.append(clf.fit(X[train], y[train], cv=cv).predict(X[test]))
    y_true.append(y[test])

y_pred = np.concatenate(y_pred)
y_true = np.concatenate(y_true)
classif_rate = np.mean(y_pred == y_true) * 100
print "Classification rate : %f" % classif_rate<commit_msg>ENH : update grid search example<commit_after>"""Run parameter estimation using grid search
in a nested cross-validation setting.
"""

import numpy as np
from scikits.learn.svm import SVC
from scikits.learn.cross_val import StratifiedKFold
from scikits.learn import datasets
from scikits.learn.grid_search import GridSearchCV

# The Digits dataset
digits = datasets.load_digits()

# To apply an classifier on this data, we need to flatten the image, to
# turn the data in a (samples, feature) matrix:
n_samples = len(digits.images)
X = digits.images.reshape((n_samples, -1))
y = digits.target

parameters = {'kernel':('rbf', 'linear'), 'C':[0.1, 1], 'gamma':[1e-3, 1e-4]}

def loss_func(y1, y2):
    return np.mean(y1 != y2)

svc = SVC()
clf = GridSearchCV(svc, parameters, loss_func, n_jobs=2)

"""
Run crossvalidation (different than the nested crossvalidation that is used
to select the best classifier). The classifier is optimized by "nested"
crossvalidation
"""
n_samples, n_features = X.shape
y_pred = []
y_true = []
for train, test in StratifiedKFold(y, 2):
    cv = StratifiedKFold(y[train], 5)
    y_pred.append(clf.fit(X[train], y[train], cv=cv).predict(X[test]))
    y_true.append(y[test])

y_pred = np.concatenate(y_pred)
y_true = np.concatenate(y_true)
classif_rate = np.mean(y_pred == y_true) * 100
print "Classification rate : %f" % classif_rate
{'fraction_non_alphanumeric': 0.12308839985080194, 'fraction_numerical': 0.011935844834017158, 'mean_word_length': 5.179723502304148, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3926409', 'n_tokens_mistral': 968, 'n_tokens_neox': 932, 'n_words': 277}