
iris 데이터 활용하기
데이터프레임 만들기
칼럼에 품종 정보 추가하기
boxplot(x='sepal length (cm)')
plt.figure(figsize=(12,6)) sns.boxplot(x='sepal length (cm)', y='species', data=iris_pd, orient='h')
boxplot(x='sepal width (cm)')
plt.figure(figsize=(12,6)) sns.boxplot(x='sepal width (cm)', y='species', data=iris_pd, orient='h')
boxplot(x='petal length (cm)')
plt.figure(figsize=(12,6)) sns.boxplot(x='petal length (cm)', y='species', data=iris_pd, orient='h')
boxplot(x='petal width (cm)')
plt.figure(figsize=(12,6)) sns.boxplot(x='petal width (cm)', y='species', data=iris_pd, orient='h')
sns.pairplot(iris_pd, hue='species')
sns.pairplot(data=iris_pd, vars=['petal length (cm)', 'petal width (cm)'], hue='species', height=4)

plt.figure(figsize=(12,6)) sns.scatterplot(x='petal length (cm)', y='petal width (cm)', data=iris_pd, hue='species', palette='Set2')
setosa는 구분이 명확하니 setosa를 제외한 두 품종만 비교하기
plt.figure(figsize=(12,6)) sns.scatterplot(x='petal length (cm)', y='petal width (cm)', data=iris_12, hue='species', palette='Set2')



결정 경계 확인해보기
!pip install mlxtend from mlxtend.plotting import plot_decision_regions plt.figure(figsize=(14, 8)) plot_decision_regions(X=iris.data[:, 2:], y=iris.target, clf=iris_tree, legend=2) plt.show()
accuracy이 높지만 경계면이 올바른지 확인할 필요가 있다.

훈련용, 테스트용으로 분리하기
잘 분리 되었는지 확인
각 클래스 별 동일한 비율로 나눠지지 않아 srratify 옵션 사용하여 비율 맞추기
train 데이터만 대상으로 결정트리 모델 만들기
- random_state : 호출할 때마다 동일한 학습/테스트용 데이터 세트를 생성하기 위해 주어지는 난수 값.
- max_depth : 모델을 단순화시키기 위해 조정 (성능 낮아짐)
결정나무 모델 확인하기
from sklearn.tree import plot_tree plt.figure(figsize=(12,8)) plot_tree(iris_tree);
성능 확인하기
훈련용 데이터에 대한 결정경계
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()
테스트 데이터에 대한 성능 확인하기
![]()
전체 데이터에서 관찰하기
scatter_highlight_kwargs = {'s' : 150, 'label':'Test data', 'alpha':0.9} scatter_kwargs = {'s' : 120, 'edgecolor':None, 'alpha':0.7} plt.figure(figsize=(12,8)) plot_decision_regions(X=features, 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}) plt.show()
전체 특성 학습시키기
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)전체 특성을 사용한 결정나무 모델
모델 사용 방법
- 추론
- 주요 특성 확인하기
predict 함수
predict_proba함수:
"이 글은 제로베이스 데이터 취업 스쿨의 강의 자료 일부를 발췌하여 작성되었습니다.”