具体查询官方文档
DecisionTrees可以用于分类问题和预测问题。
决策树的优点是模型具有可读性,分类速度快。
决策树学习通常包括三个步骤:特征选择、决策树的生成和决策树的修剪。
常见的决策树算法:ID3,C4.5,CART
sklearn更新最新版本时,显示“无法定位程序输入点……”之类的问题。我的处理流程:
conda update scikit-learn
from sklearn import tree
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
# X, y = load_iris(return_X_y=True)
# iris = load_iris(return_X_y=False)
# model = tree.DecisionTreeClassifier()
# model.fit(X, y)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
estimator = DecisionTreeClassifier(max_leaf_nodes=3, random_state=0)
estimator.fit(X_train, y_train)
tree.plot_tree(model.fit(iris.data, iris.target))
conda install python-graphviz
import graphviz
dot_data = tree.export_graphviz(model, out_file=None,
... feature_names=iris.feature_names,
... class_names=iris.target_names,
... filled=True, rounded=True,
... special_characters=True)
graph = graphviz.Source(dot_data)
graph.render('iris2')
graph
from sklearn.tree.export import export_text
r = export_text(model, feature_names=iris['feature_names'])
print(r)
from sklearn.tree import DecisionTreeRegressor
X = ___
y = ___
model = DecisionTreeRegressor()
model.fit(X, y)
sor
X = ___
y = ___
model = DecisionTreeRegressor()
model.fit(X, y)