# -*- coding: utf-8 -*-
"""
Created on Wed Aug 10 08:10:35 2016
@author: Administrator
"""
'''
关于:cross_validation.scores
此处cross_validation.scores并不是cross_validation的scores,
而是分类函数(本文是clf,svm)的scores
分成K部分
K-1是训练
1次是测试
共K个scores
'''
import time
import numpy as np
from sklearn import cross_validation
from sklearn import datasets
from sklearn import svm
start = time.time()
iris = datasets.load_iris()
print iris.data.shape, iris.target.shape
#(150L, 4L) (150L,)
X_train, X_test, y_train, y_test = cross_validation.train_test_split(iris.data, iris.target, test_size=0.4, random_state=0)
print X_train.shape, y_train.shape
print X_test.shape, y_test.shape
#(90L, 4L) (90L,)
#(60L, 4L) (60L,)
clf = svm.SVC(kernel='linear', C=1).fit(X_train, y_train)
print clf.score(X_test, y_test)
#0.966666666667
#模块2
clf = svm.SVC(kernel='linear', C=1)
scores = cross_validation.cross_val_score(clf, iris.data, iris.target, cv=5)
print scores
#[ 0.96666667 1. 0.96666667 0.96666667 1. ]
print("Accuracy: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() * 2))
#Accuracy: 0.98 (+/- 0.03)
#模块3
from sklearn import metrics
scores = cross_validation.cross_val_score(clf, iris.data, iris.target,cv=5, scoring='f1_weighted')
print scores
#[ 0.96658312 1. 0.96658312 0.96658312 1. ]
print("Took %.2f seconds for" % ((time.time() - start)))