#关于 OneVsRestClassifier
#注意以下代码中,有三个类
from sklearn import datasets
X, y = datasets.make_classification(n_samples=10000, n_classes=3)
from sklearn.tree import DecisionTreeClassifier
dt = DecisionTreeClassifier()
dt.fit(X, y)
print(dt.predict(X))
print ("Accuracy:\t", (y == dt.predict(X)).mean())

#利用 OneVsRestClassifier,进行分类
#它好像是个外壳,还是利用里面的分类器进行分类
#只不过加快了速度(并行)

from sklearn.multiclass import OneVsRestClassifier
from sklearn.linear_model import LogisticRegression
'''
Now, we'll override the LogisticRegression classifier.
Also, notice that we can parallelize this.
If we think about how OneVsRestClassifier works,
it's just training separate models and then comparing them.
So, we can train the data separately at the same time:
'''
#LogisticRegression 速度很慢
mlr = OneVsRestClassifier(LogisticRegression(), n_jobs=2)
mlr.fit(X, y)
print(mlr.predict(X))
print ("Accuracy:\t", (y == mlr.predict(X)).mean())

 

posted on 2016-04-01 08:04  qqhfeng16  阅读(5555)  评论(0编辑  收藏  举报