๐ Scikit Learn Tree
๐ป Tree model visualization
from sklearn.tree import plot_tree
plt.figure(figsize=(12, 8))
plot_tree(iris_clf);
๐ป mlxtend.plotting
- ์ ๊ณตํ ๋ฐ์ดํฐ์ ๋ฐ๋ผ ๊ฒฝ๊ณ์ ์ ๊ทธ๋ ค์ฃผ๋ ํจ์
- feature๊ฐ ์ ์ด์ ์ฌ์ฉํจ, feature๊ฐ ๋ง๋ค๋ฉด ์ฌ์ฉํ๊ธฐ ์ด๋ ค์
- ๊ฒฝ๊ณ๋ฉด์ด ๊ณผ์ฐ ์ฌ๋ฐ๋ฅธ ๊ฑธ๊น ์์ฌ์ ํด๋ด์ผ ํ๋ค. (๋ณต์กํ ๊ฒฝ๊ณ๋ฉด์ ๋ชจ๋ธ์ ์ฑ๋ฅ์ ๊ฒฐ๊ตญ ๋์๊ฒ ๋ง๋ ๋ค.)
from mlxtend.plotting import plot_decision_regions
plt.figure(figsize=(14, 8))
plot_decision_regions(X=iris.data[:, 2:], y=iris.target, clf=iris_clf, legend=2)
plt.show()
๐ ๊ณผ์ ํฉ (Over fitting)
- ๋ด๊ฐ ๊ฐ์ง๊ณ ์๋ ๋ฐ์ดํฐ์ ๋๋ฌด fitํ์ฌ ๋ฐ์ดํฐ ์ด์ธ์ ์ผ๋ฐ์ ๋ฐ์ดํฐ์์ ์ ์ฑ๋ฅ์ด ์ ๋์ค์ง ๋ชปํ๋ ๊ฒ
๐ป ๋ฐ์ดํฐ์ ๋ถ๋ฆฌ (๊ณผ์ ํฉ์ธ์ง ์๋์ง ํ์ ํ๊ธฐ ์ํด)
- model_selection -> train_test_split ํจ์ ์ฌ์ฉ
from sklearn.datasets import load_iris
import pandas as pd
iris = load_iris()
from sklearn.model_selection import train_test_split
features = iris.data[:, 2:]
labels = iris.target
๐ป ํ๋ จ ๋ฐ์ดํฐ์ ํ ์คํธ ๋ฐ์ดํฐ๋ฅผ ๋๋๊ธฐ
- ํ ์คํธ ๋ฐ์ดํฐ๋ฅผ ๋๋ ๋ณด๋ 9, 8, 13๊ฐ๋ก ๋๋ ์ ธ ์๋ค- ์ด๋ฅผ stratify ์ต์ ์ผ๋ก ๊ฐ๊ฐ ๋์ผํ๊ฒ ๋๋ ์ ๋ฐ์ดํฐ๋ฅผ ๋๋ ์ ์๋ค.
# test_size = 0.2 --> 20%๋ฅผ ํ
์คํธ ์ฉ๋๋ก ์ฌ์ฉ
X_train, X_test, y_train, y_test = train_test_split(feature, labels, test_size=0.2, random_state=13)
X_train.shape, X_test.shape
>>>>
((120, 2), (30, 2))
# train ๋ฐ์ดํฐ์ ๊ฐ ๊ฝ์ด ๋ช๊ฐ ๋ค์ด๊ฐ ์์๊น ํ์ธ
import numpy as np
np.unique(y_test, return_counts=True)
>>>>
(array([0, 1, 2]), array([ 9, 8, 13], dtype=int64))
# stratify --> ๊ฐ๊ฐ์ ๋ฐ์ดํฐ ๋น์จ์ ๋ง์ถฐ์ฃผ๋ ์ต์
X_train, X_test, y_train, y_test = train_test_split(feature, labels, test_size=0.2, stratify=labels, random_state=13)
import numpy as np
np.unique(y_test, return_counts=True)
>>>>
(array([0, 1, 2]), array([10, 10, 10], dtype=int64))
๐ป ํ๋ จ ๋ฐ์ดํฐ์ ํ ์คํธ ๋ฐ์ดํฐ๋ฅผ ๋๋๊ธฐ
- max_depth ๊ฐ์ ์ง์ ํ์ฌ Tree๊ฐ ๋ป์ด๋๊ฐ๋ ๊ฐ์๋ฅผ ๊ท์ ํ ์ ์๋ค
--> ๊ณผ์ ํฉ ๋ฐฉ์ง
# max_depth ๊ฐ์ ์ง์ ํ์ฌ tree๊ฐ ๋ป์ด๋๊ฐ๋ ๊ฐ์๋ฅผ ์ ํด์ค๋ค. (์ฑ๋ฅ ์ ํ ๋ฐ ๊ท์ --> ๊ณผ์ ํฉ ๋ฐฉ์ง)
from sklearn.tree import DecisionTreeClassifier
iris_tree = DecisionTreeClassifier(max_depth=2, random_state=13)
iris_tree.fit(X_train, y_train)
import matplotlib.pyplot as plt
from sklearn.tree import plot_tree
plt.figure(figsize=(12, 8))
plot_tree(iris_tree);
๐ป ์ ํ๋๋ฅผ ํ์ธ
- ์ด์ ๋ณด๋ค ๋ฎ์์ง ๊ฒ์ ํ์ธํ ์ ์๋ค.
from sklearn.metrics import accuracy_score
y_pred_tr = iris_tree.predict(iris.data[:, 2:])
accuracy_score(iris.target, y_pred_tr)
>>>>
0.9533333333333334
๐ป ํ๋ จ๋ ๋ฐ์ดํฐ์ ๊ฒฐ์ ๊ฒฝ๊ณ๋ฅผ ํ์ธํด๋ณด์
from mlxtend.plotting import plot_decision_regions
plt.figure(figsize=(14, 8))
plot_decision_regions(X=X_train, y=y_train, clf=iris_tree, legend=2)
plt.show()
๐ป Test ๋ฐ์ดํฐ์ ์ ํ์ฑ์ ์์๋ณด์
# Test ๋ฐ์ดํฐ ์ ํ์ฑ ํ์ธ
y_pred_test = iris_tree.predict(X=X_test)
accuracy_score(y_test, y_pred_test)
>>>>
0.9666666666666667
#Test data์ ๊ธฐ๋ณธ ๋ฐ์ดํฐ ๋น๊ต
scatter_highlight_kwargs = {'s':150, 'label':'Test data', 'alpha':0.9}
scatter_kwargs = {'s':120, 'edgecolor':None, 'alpha':0.9}
plt.figure(figsize=(12, 8))
plot_decision_regions(X=feature, y=labels, X_highlight=X_test, clf=iris_tree, legend=2,
scatter_highlight_kwargs=scatter_highlight_kwargs,
scatter_kwargs=scatter_kwargs,
contourf_kwargs={'alpha':0.2})
๐ Feature data๋ฅผ 4๊ฐ๋ก ํด๋ณด๊ธฐ
- ๊ธฐ์กด์๋ petal width, petal length๋ก ์งํํ๋ค๋ฉด, sepal length, sepal width๋ฅผ ์ถ๊ฐํด์ ์งํํด๋ณด์
from sklearn.model_selection import train_test_split
features = iris.data
labels = iris.target
X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.2, stratify=labels, random_state=13)
iris_tree = DecisionTreeClassifier(max_depth=2, random_state=13)
iris_tree.fit(X_train, y_train)
import matplotlib.pyplot as plt
from sklearn.tree import plot_tree
plt.figure(figsize=(12, 8))
plot_tree(iris_tree);
๐ ํด๋น ๋ชจ๋ธ์ ๋ฐ์ดํฐ๋ฅผ ๋ฃ์ด๋ณด์
- feature 4๊ฐ์ ๋ชจ๋ธ์ ๋ฐ์ดํฐ๋ฅผ ๋ฃ์ด๋ณด๋, 1 ์ฆ 'versicolor' ๊ฐ ๋์ค๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
## ๋ชจ๋ธ์ ๋ง๋ค์์ผ๋ ๋ฐ์ดํฐ๋ฅผ ๋ฃ์ด๋ณด์
test_data = np.array([[4.3, 2.0, 1.2, 1.0]])
iris_tree.predict(test_data)
>>>>
array([1])
๐ป Data์ ํ๋ฅ ์ ์ฒดํฌํ๊ณ ์ด๋ฆ ํ์ธํ๊ธฐ
# data์ ํ๋ฅ (๋ด ๋ชจ๋ธ์ ๋ฐ์ดํฐ๋ฅผ ๋ฃ์ผ๋ฉด ์ด๋ค ๊ฐ์ด ๋์ฌ์ง์ ํ๋ฅ !)
iris_tree.predict_proba(test_data)
>>>>
array([[0. , 0.97222222, 0.02777778]])
# ๊ฐ์ด 1์ด ๋์๊ธฐ ๋๋ฌธ์, target_names์ ๋ณ์๋ฅผ ๋ฃ์ผ๋ฉด ํด๋นํ๋ ๊ฝ์ ์ด๋ฆ์ด ๋ฐํ๋๋ค.
iris.target_names[iris_tree.predict(test_data)]
>>>>
array(['versicolor'], dtype='<U10')
# ๋ชจ๋ธ์ ๊ฒฐ์ ํ๋ ๋ฐ ์ค์ํ feature --> max_depth ๊ฐ์ ๋ฐ๋ผ ๋ฐ๋๋ค.
iris_tree = DecisionTreeClassifier(max_depth=2, random_state=13)
iris_tree.fit(X_train, y_train)
iris_tree.feature_importances_
# zipping
dict(zip(iris.feature_names, iris_tree.feature_importances_))
>>>>
{'sepal length (cm)': 0.0,
'sepal width (cm)': 0.0,
'petal length (cm)': 0.421897810218978,
'petal width (cm)': 0.578102189781022}
๐ zipping / ์ธํจํน์ ๋ํด์
๐ป๋ฆฌ์คํธ๋ฅผ Tuple๋ก zipping
list1 =['a', 'b', 'c']
list2 =[1, 2, 3]
pairs = [pair for pair in zip(list1, list2)]
pairs
>>>>
[('a', 1), ('b', 2), ('c', 3)]
dict(pairs)
>>>>
{'a': 1, 'b': 2, 'c': 3}
# ํ๋ฒ์ ํด๊ฒฐํ๊ธฐ
dict(zip(list1, list2))
>>>>
{'a': 1, 'b': 2, 'c': 3}
๐ปunpacking ์ธ์๋ฅผ ํตํ ์ญ๋ณํํ๊ธฐ
# unpacking ์ธ์๋ฅผ ํตํ ์ญ๋ณํ
x, y = zip(*pairs)
x, y
>>>>
(('a', 'b', 'c'), (1, 2, 3))
'Study_note(zb_data) > Machine Learning' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์คํฐ๋๋ ธํธ (ML6_Wine) (0) | 2023.09.22 |
---|---|
์คํฐ๋๋ ธํธ (ML5) (0) | 2023.09.21 |
์คํฐ๋๋ ธํธ (ML4) (0) | 2023.09.21 |
์คํฐ๋๋ ธํธ (ML3) (0) | 2023.09.21 |
์คํฐ๋ ๋ ธํธ (ML) (0) | 2023.09.19 |