공부 기록
ML(0816_day9) - 결정 트리 본문
결정 트리¶
In [1]:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
In [93]:
iris = load_iris()
X = iris.data[:, 2:] # petal length, petal width
y = iris.target
In [94]:
tree_clf = DecisionTreeClassifier(max_depth=2, random_state=42)
tree_clf.fit(X, y)
Out[94]:
DecisionTreeClassifier(max_depth=2, random_state=42)
In [95]:
from sklearn.tree import export_graphviz
In [96]:
export_graphviz(tree_clf,
out_file="iris_tree.dot",
class_names=iris.target_names,
feature_names=iris.feature_names[2:],
rounded=True,
filled=True)
In [97]:
import graphviz
with open("iris_tree.dot") as f:
dot_graph = f.read()
graphviz.Source(dot_graph)
Out[97]:
In [98]:
tree_clf.predict_proba([[5, 1.5]])
Out[98]:
array([[0. , 0.90740741, 0.09259259]])
In [99]:
tree_clf.predict([[5, 1.5]])
Out[99]:
array([1])
In [100]:
X.shape
Out[100]:
(150, 2)
- 특성 중요도
In [104]:
iris.feature_names[2:]
Out[104]:
['petal length (cm)', 'petal width (cm)']
In [101]:
tree_clf.feature_importances_
Out[101]:
array([0.56199095, 0.43800905])
- 회귀 트리
In [84]:
np.random.seed(42)
In [85]:
m = 200
X = np.random.rand(m, 1)
y = 4 * (X-0.5) ** 2
y = y + np.random.randn(m, 1) / 10
In [86]:
from sklearn.tree import DecisionTreeRegressor
In [87]:
tree_reg = DecisionTreeRegressor(max_depth=2, random_state=42)
tree_reg.fit(X, y)
Out[87]:
DecisionTreeRegressor(max_depth=2, random_state=42)
In [88]:
export_graphviz(tree_reg,
out_file = "regression_tree.dot",
feature_names=["x1"],
rounded=True,
filled=True)
In [89]:
with open("regression_tree.dot") as f:
dot_graph = f.read()
graphviz.Source(dot_graph)
Out[89]:
In [90]:
tree_reg.predict([[0.6]])
Out[90]:
array([0.11063973])
In [ ]:
'playdata' 카테고리의 다른 글
ML(0816_day9) - 실습_Human Activity Recognition with Smartphones (0) | 2021.08.16 |
---|---|
ML(0816_day9) - 앙상블 학습과 랜덤포레스트 (0) | 2021.08.16 |
ML(0812_day7) - 선형 회귀 (0) | 2021.08.13 |
ML(0813_day8) - 결정 트리 (0) | 2021.08.13 |
ML(0813_day8) - 실습_붓꽃 데이터 품종 예측하기(로지스틱 회귀, 소프트맥스 회귀) (0) | 2021.08.13 |
Comments