飞道的博客

【机器学习】决策树、随机森林

362人阅读  评论(0)
from sklearn.tree import DecisionTreeClassifier,export_graphviz
from sklearn.model_selection import train_test_split,GridSearchCV
from sklearn.datasets import load_wine,load_iris
import numpy as np
import matplotlib.pyplot as plt

from sklearn.externals.six import StringIO
import pydotplus
#每个类别各多少个
np.bincount(load_wine().target)
wine = load_wine()
data = wine.data
target = wine.target

X_train,X_test,y_train,y_test = train_test_split(data,target)

dtree = DecisionTreeClassifier(criterion='gini',max_depth=3,max_leaf_nodes=10).fit(X_train,y_train)

dtree.score(X_train,y_train),dtree.score(X_test,y_test)

(0.9849624060150376, 0.8222222222222222)

#导出树模型
str_ = StringIO()
export_graphviz(dtree,str_,feature_names=wine.feature_names,\
                class_names=wine.target_names,filled=True,rounded=True)

graph = pydotplus.graph_from_dot_data(str_.getvalue())
graph.write_jpg('./wine.jpg')

随机森林

from sklearn.ensemble import RandomForestClassifier,RandomForestRegressor
from sklearn.ensemble import BaggingClassifier,BaggingRegressor  #自由组合算法
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split,GridSearchCV
iris = load_iris()
data = iris.data
target = iris.target

X_train,X_test,y_train,y_test = train_test_split(data,target)

dtree = DecisionTreeClassifier().fit(X_train,y_train)
params = {"max_depth":[3,4,5],'max_leaf_nodes':[5,10,15]}
gc = GridSearchCV(dtree,params,cv=4,n_jobs=6).fit(X_train,y_train)
y_pred = gc.best_estimator_.predict(X_test)

gc.best_score_
0.9375

gc.best_estimator_.score(X_train,y_train)
0.9910714285714286
gc.best_estimator_.score(X_test,y_test)
0.9473684210526315


rfc = RandomForestClassifier(n_jobs=6)
params = {"n_estimators":[10,100,500,800,1000],"max_depth":[3,4,5],'max_leaf_nodes':[5,10,15]}
gc = GridSearchCV(rfc,params,cv=4,n_jobs=6).fit(X_train,y_train)

gc.best_score_
0.9732142857142857
gc.best_estimator_.score(X_train,y_train)
0.9732142857142857
gc.best_estimator_.score(X_test,y_test)
0.9210526315789473

转载:https://blog.csdn.net/weixin_44727383/article/details/104639544
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场