728x90
반응형
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report
from sklearn.tree import DecisionTreeClassifier
import os
import pydotplus
from sklearn.tree import export_graphviz
from IPython.display import Image
wine_data = pd.read_csv('winequality-white.csv',delimiter=';',dtype=float)
wine_data.head(10)
x_data = wine_data.iloc[:,0:-1]
y_data = wine_data.iloc[:,-1]
# Score 값이 7보다 작으면 0, 7보다 크거나 같으면 1로 값 변경.
y_data = np.array([1 if i>=7 else 0 for i in y_data])
x_data.head(5)
train_x, test_x, train_y, test_y = train_test_split(x_data, y_data, test_size = 0.3,random_state=42)
wine_tree = DecisionTreeClassifier(criterion='gini', max_depth=5,splitter='random')
wine_tree.fit(train_x, train_y)
y_pred_train = wine_tree.predict(train_x)
y_pred_test = wine_tree.predict(test_x)
print("Train Data:", accuracy_score(train_y, y_pred_train))
print("Test Data" , accuracy_score(test_y, y_pred_test))
y_true, y_pred = test_y, wine_tree.predict(test_x)
print(classification_report(y_true, y_pred))
# path 설정 - 자신의 설치한 경로.
# Graphviz설치 : https://graphviz.org/download/ (설치시 path 추가)
os.environ["PATH"] += os.pathsep + r'C:/Program Files/Graphviz/bin'
dot_data = export_graphviz(wine_tree, out_file=None,feature_names=x_data.columns,
class_names='Qual', filled=True, rounded=True, special_characters=True)
graph = pydotplus.graph_from_dot_data(dot_data)
# 이미지의 depth 는 모델 학습의 depth의 영향을 받음.
# 현재 이미지는 depth = 5
# error : Graphviz's executables not found (Graphviz's설치로 해결)
Image(graph.create_png())
- Data download
https://question99.tistory.com/191
[파이썬] data (winequality-white)
import pandas as pd data = pd.read_csv('winequality-white.csv',sep=';', header=0) data
question99.tistory.com
728x90
반응형
'프로그램' 카테고리의 다른 글
[파이썬] tkinter Label, button image resize방법 (0) | 2022.11.01 |
---|---|
[파이썬] pydotplus (Graphviz's executables not found) (0) | 2022.11.01 |
[파이썬] data (winequality-white) (0) | 2022.11.01 |
[파이썬] 가위바위보 게임(승부 판단 로직) (0) | 2022.11.01 |
[파이썬] 합성 파형 그래프 (0) | 2022.10.30 |
댓글