打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
python sklearn包

  1. from __future__ import print_function  
  2.   
  3. from sklearn import datasets  
  4. from sklearn.cross_validation import train_test_split  
  5. from sklearn.grid_search import GridSearchCV  
  6. from sklearn.metrics import classification_report  
  7. from sklearn.svm import SVC  
  8.   
  9. print(__doc__)  
  10.   
  11. # Loading the Digits dataset  
  12. digits = datasets.load_digits()  
  13.   
  14. # To apply an classifier on this data, we need to flatten the image, to  
  15. # turn the data in a (samples, feature) matrix:  
  16. n_samples = len(digits.images)  
  17. X = digits.images.reshape((n_samples, -1))  
  18. y = digits.target  
  19.   
  20. # Split the dataset in two equal parts  
  21. X_train, X_test, y_train, y_test = train_test_split(  
  22.     X, y, test_size=0.5, random_state=0)  
  23.   
  24. # Set the parameters by cross-validation  
  25. tuned_parameters = [{'kernel': ['rbf'], 'gamma': [1e-3, 1e-4],  
  26.                      'C': [1, 10, 100, 1000]},  
  27.                     {'kernel': ['linear'], 'C': [1, 10, 100, 1000]}]  
  28.   
  29. scores = ['precision', 'recall']  
  30.   
  31. for score in scores:  
  32.     print("# Tuning hyper-parameters for %s" % score)  
  33.     print()  
  34.   
  35.     clf = GridSearchCV(SVC(C=1), tuned_parameters, cv=5,  
  36.                        scoring='%s_weighted' % score)  
  37.     clf.fit(X_train, y_train)  
  38.   
  39.     print("Best parameters set found on development set:")  
  40.     print()  
  41.     print(clf.best_params_)  
  42.     print()  
  43.     print("Grid scores on development set:")  
  44.     print()  
  45.     for params, mean_score, scores in clf.grid_scores_:  
  46.         print("%0.3f (+/-%0.03f) for %r"  
  47.               % (mean_score, scores.std() * 2, params))  
  48.     print()  
  49.   
  50.     print("Detailed classification report:")  
  51.     print()  
  52.     print("The model is trained on the full development set.")  
  53.     print("The scores are computed on the full evaluation set.")  
  54.     print()  
  55.     y_true, y_pred = y_test, clf.predict(X_test)  
  56.     print(classification_report(y_true, y_pred))  
  57.     print()  

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Python之sklearn-pmml:sklearn-pmml的简介、安装、使用方法之详细攻略
使用sklearn做kmeans聚类分析
用python计算一组数据的最高分最低分和平均分
SVM参数
涨知识!用逻辑规则进行机器学习
使用sklearn做自然语言处理-2
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服