[sklearn] 实现随即梯度下降(SGD)&分类器评价参数查看

直接贴代码吧:

  1 # -*- coding:UTF-8 -*-

  2 from sklearn import datasets

  3 from sklearn.cross_validation import train_test_split

  4 from sklearn import preprocessing

  5 from sklearn.linear_model import SGDClassifier

  6 from sklearn import metrics

  7 iris = datasets.load_iris()

  8 X_iris, y_iris = iris.data, iris.target

  9 X, y = X_iris[:,:2], y_iris

 10 # 将数据分为训练集和测试集

 11 X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.25, random_state=33)

 12 # 标准化数据

 13 scaler = preprocessing.StandardScaler().fit(X_train)

 14 X_train = scaler.transform(X_train)

 15 X_test = scaler.transform(X_test)

 16 # 初始化分类器对象

 17 clf = SGDClassifier()

 18 clf.fit(X_train,y_train)

 19 y_pred = clf.predict(X_test)

 20 # 评估分类器的表现

 21 print metrics.accuracy_score(y_test,y_pred)

 22 print metrics.classification_report(y_test, y_pred, target_names=iris.target_names)

 23 # confusion 矩阵

 24 print metrics.confusion_matrix(y_test,y_pred)

 

参考:《learning scikit-learn machine learning in python》

posted @ 2018-01-28 16:45  凌波微步_Arborday  阅读(610)  评论(0编辑  收藏  举报