Guest User

Untitled

a guest
Nov 19th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. from sklearn import datasets
  2. from sklearn.cross_validation import train_test_split, KFold
  3. from sklearn.grid_search import GridSearchCV
  4. from sklearn.metrics import classification_report
  5. from sklearn.metrics import precision_score
  6. from sklearn.metrics import recall_score
  7. from sklearn.svm import SVC
  8.  
  9. # Loading the Digits dataset
  10. digits = datasets.load_digits()
  11.  
  12. # To apply an classifier on this data, we need to flatten the image, to
  13. # turn the data in a (samples, feature) matrix:
  14. n_samples = len(digits.images)
  15. X = digits.images.reshape((n_samples, -1))
  16.  
  17. y = digits.target
  18.  
  19. # Split the dataset in two equal parts
  20. X_train, X_test, y_train, y_test = train_test_split(
  21. X, y, test_fraction=0.5, random_state=0)
  22.  
  23. # Set the parameters by cross-validation
  24. tuned_parameters = [{'kernel': ['rbf'], 'gamma': [1e-3, 1e-4],
  25. 'C': [1, 10, 100, 1000]},
  26. {'kernel': ['linear'], 'C': [1, 10, 100, 1000]}]
  27.  
  28. scores = [
  29. ('precision', precision_score),
  30. ('recall', recall_score),
  31. ]
  32.  
  33. for score_name, score_func in scores:
  34. print "# Tuning hyper-parameters for %s" % score_name
  35. print
  36.  
  37. #modified to produce error
  38. clf = GridSearchCV(SVC(C=1), tuned_parameters, score_func=score_func,cv=KFold(n=len(X_train),k=10))
  39. clf.fit(X_train.tolist(), y_train) #happens only for lists
  40.  
  41. #modification end
  42. print "Best parameters set found on development set:"
  43. print
  44. print clf.best_estimator_
  45. print
  46. print "Grid scores on development set:"
  47. print
  48. for params, mean_score, scores in clf.grid_scores_:
  49. print "%0.3f (+/-%0.03f) for %r" % (
  50. mean_score, scores.std() / 2, params)
  51. print
  52.  
  53. print "Detailed classification report:"
  54. print
  55. print "The model is trained on the full development set."
  56. print "The scores are computed on the full evaluation set."
  57. print
  58. y_true, y_pred = y_test, clf.predict(X_test.tolist())
  59. print classification_report(y_true, y_pred)
  60. print
Add Comment
Please, Sign In to add comment