-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
205 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"source": [ | ||
"!pip install pydot" | ||
], | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"name": "stdout", | ||
"text": [ | ||
"Requirement already satisfied: pydot in c:\\python39-at\\lib\\site-packages (1.4.2)\n", | ||
"Requirement already satisfied: pyparsing>=2.1.4 in c:\\python39-at\\lib\\site-packages (from pydot) (2.4.7)\n" | ||
] | ||
}, | ||
{ | ||
"output_type": "stream", | ||
"name": "stderr", | ||
"text": [ | ||
"WARNING: You are using pip version 21.1.3; however, version 21.2.4 is available.\n", | ||
"You should consider upgrading via the 'c:\\python39-at\\python.exe -m pip install --upgrade pip' command.\n" | ||
] | ||
} | ||
], | ||
"metadata": {} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 15, | ||
"source": [ | ||
"from sklearn.tree import DecisionTreeClassifier, export_graphviz\r\n", | ||
"from sklearn.datasets import load_breast_cancer\r\n", | ||
"from sklearn.model_selection import train_test_split\r\n", | ||
"\r\n", | ||
"#Dot to png\r\n", | ||
"import pydot" | ||
], | ||
"outputs": [], | ||
"metadata": {} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 16, | ||
"source": [ | ||
"cancer = load_breast_cancer() #breast cancer dataset 가져오기!!\r\n" | ||
], | ||
"outputs": [], | ||
"metadata": {} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 17, | ||
"source": [ | ||
"#훈련, 테스트 데이터 셔플\r\n", | ||
"X_train, X_test, y_train, y_test = train_test_split(\r\n", | ||
"cancer.data, cancer.target, stratify=cancer.target, random_state=42)\r\n" | ||
], | ||
"outputs": [], | ||
"metadata": {} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 20, | ||
"source": [ | ||
"#의사결정 트리 선언\r\n", | ||
"dTreeAll = DecisionTreeClassifier(random_state=0)" | ||
], | ||
"outputs": [], | ||
"metadata": {} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 21, | ||
"source": [ | ||
"dTreeAll.fit(X_train, y_train)\r\n" | ||
], | ||
"outputs": [ | ||
{ | ||
"output_type": "execute_result", | ||
"data": { | ||
"text/plain": [ | ||
"DecisionTreeClassifier(random_state=0)" | ||
] | ||
}, | ||
"metadata": {}, | ||
"execution_count": 21 | ||
} | ||
], | ||
"metadata": {} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 22, | ||
"source": [ | ||
"#점수 출력\r\n", | ||
"print(\"Train Set Score1 : {:.2f}\".format(dTreeAll.score(X_train, y_train)))\r\n", | ||
"print(\"Test Set Score1 : {:.2f}\".format(dTreeAll.score(X_test, y_test)))\r\n" | ||
], | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"name": "stdout", | ||
"text": [ | ||
"Train Set Score1 : 1.00\n", | ||
"Test Set Score1 : 0.94\n" | ||
] | ||
} | ||
], | ||
"metadata": {} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"source": [ | ||
"\r\n", | ||
"#의사결정 트리 선언(트리 깊이 제한)\r\n", | ||
"dTreeLimit = DecisionTreeClassifier(max_depth=3, random_state=0)\r\n" | ||
], | ||
"outputs": [], | ||
"metadata": {} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"source": [ | ||
"#훈련 (가지치기 : 리프노드 깊이 제한)\r\n", | ||
"dTreeLimit.fit(X_train, y_train)" | ||
], | ||
"outputs": [], | ||
"metadata": {} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"source": [ | ||
"\r\n", | ||
"#점수 출력\r\n", | ||
"print(\"Train Set Score2 : {:.2f}\".format(dTreeLimit.score(X_train, y_train)))\r\n", | ||
"print(\"Test Set Score2 : {:.2f}\".format(dTreeLimit.score(X_test, y_test)))\r\n" | ||
], | ||
"outputs": [], | ||
"metadata": {} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"source": [ | ||
"\r\n", | ||
"\r\n", | ||
"export_graphviz(dTreeLimit, out_file=\"dicisionTree1.dot\", class_names=[\"malignant\",\"benign\"],\r\n", | ||
" feature_names=cancer.feature_names, impurity=False, filled=True)\r\n" | ||
], | ||
"outputs": [], | ||
"metadata": {} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"source": [ | ||
"\r\n", | ||
"#Encoding 중요\r\n", | ||
"(graph,) = pydot.graph_from_dot_file('dicisionTree1.dot', encoding='utf8')\r\n" | ||
], | ||
"outputs": [], | ||
"metadata": {} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"source": [ | ||
"\r\n", | ||
"#Dot 파일을 Png 이미지로 저장\r\n", | ||
"graph.write_png('dicisionTree1.png')\r\n" | ||
], | ||
"outputs": [], | ||
"metadata": {} | ||
} | ||
], | ||
"metadata": { | ||
"orig_nbformat": 4, | ||
"language_info": { | ||
"name": "python", | ||
"version": "3.9.6", | ||
"mimetype": "text/x-python", | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"pygments_lexer": "ipython3", | ||
"nbconvert_exporter": "python", | ||
"file_extension": ".py" | ||
}, | ||
"kernelspec": { | ||
"name": "python3", | ||
"display_name": "Python 3.9.6 64-bit" | ||
}, | ||
"interpreter": { | ||
"hash": "63fd5069d213b44bf678585dea6b12cceca9941eaf7f819626cde1f2670de90d" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |