Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from sklearn import datasets
- from sklearn.cross_validation import train_test_split, KFold
- from sklearn.grid_search import GridSearchCV
- from sklearn.metrics import classification_report
- from sklearn.metrics import precision_score
- from sklearn.metrics import recall_score
- from sklearn.svm import SVC
- # Loading 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
- # Split the dataset in two equal parts
- X_train, X_test, y_train, y_test = train_test_split(
- X, y, test_fraction=0.5, random_state=0)
- # Set the parameters by cross-validation
- tuned_parameters = [{'kernel': ['rbf'], 'gamma': [1e-3, 1e-4],
- 'C': [1, 10, 100, 1000]},
- {'kernel': ['linear'], 'C': [1, 10, 100, 1000]}]
- scores = [
- ('precision', precision_score),
- ('recall', recall_score),
- ]
- for score_name, score_func in scores:
- print "# Tuning hyper-parameters for %s" % score_name
- print
- #modified to produce error
- clf = GridSearchCV(SVC(C=1), tuned_parameters, score_func=score_func,cv=KFold(n=len(X_train),k=10))
- clf.fit(X_train.tolist(), y_train) #happens only for lists
- #modification end
- print "Best parameters set found on development set:"
- print
- print clf.best_estimator_
- print
- print "Grid scores on development set:"
- print
- for params, mean_score, scores in clf.grid_scores_:
- print "%0.3f (+/-%0.03f) for %r" % (
- mean_score, scores.std() / 2, params)
- print
- print "Detailed classification report:"
- print
- print "The model is trained on the full development set."
- print "The scores are computed on the full evaluation set."
- print
- y_true, y_pred = y_test, clf.predict(X_test.tolist())
- print classification_report(y_true, y_pred)
- print
Add Comment
Please, Sign In to add comment