{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Assignment. 자동차 평가 데이터에 대한 EDA 및 Decision Tree를 활용한 예측\n", "- 데이터 집합 소스\n", " - 설명: http://archive.ics.uci.edu/ml/datasets/Car+Evaluation\n", " - Features\n", " - buying (자동차 가격): vhigh, high, med, low.\n", " - maint (유지보수 가격): vhigh, high, med, low.\n", " - doors (자동차 문의 개수): 2, 3, 4, 5more.\n", " - persons (자동차 안에 탈 수 있는 사람 인원): 2, 4, more.\n", " - lug_boot (짐트렁크 크기): small, med, big.\n", " - safety (안전도): low, med, high.\n", " - Target \n", " - eval (평가만족도): unacc, acc, good, vgood\n", " - 모든 Attributes들을 Categorical 데이터로 취급해도 됨 \n", " - 데이터: http://archive.ics.uci.edu/ml/machine-learning-databases/car/car.data" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import urllib2\n", "from scipy import stats\n", "from pandas import Series, DataFrame\n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "%matplotlib inline\n", "\n", "path = 'http://archive.ics.uci.edu/ml/machine-learning-databases/car/car.data'\n", "raw_csv = urllib2.urlopen(path)\n", "feature_names = ('buying', 'maint', 'doors', 'persons', \"log_boot\", \"safety\")\n", "target_name = 'eval'\n", "all_names = feature_names + (target_name,)\n", "df = pd.read_csv(raw_csv, names=all_names)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
buyingmaintdoorspersonslog_bootsafetyeval
0vhighvhigh22smalllowunacc
1vhighvhigh22smallmedunacc
2vhighvhigh22smallhighunacc
3vhighvhigh22medlowunacc
4vhighvhigh22medmedunacc
5vhighvhigh22medhighunacc
6vhighvhigh22biglowunacc
7vhighvhigh22bigmedunacc
8vhighvhigh22bighighunacc
9vhighvhigh24smalllowunacc
10vhighvhigh24smallmedunacc
11vhighvhigh24smallhighunacc
12vhighvhigh24medlowunacc
13vhighvhigh24medmedunacc
14vhighvhigh24medhighunacc
15vhighvhigh24biglowunacc
16vhighvhigh24bigmedunacc
17vhighvhigh24bighighunacc
18vhighvhigh2moresmalllowunacc
19vhighvhigh2moresmallmedunacc
20vhighvhigh2moresmallhighunacc
21vhighvhigh2moremedlowunacc
22vhighvhigh2moremedmedunacc
23vhighvhigh2moremedhighunacc
24vhighvhigh2morebiglowunacc
25vhighvhigh2morebigmedunacc
26vhighvhigh2morebighighunacc
27vhighvhigh32smalllowunacc
28vhighvhigh32smallmedunacc
29vhighvhigh32smallhighunacc
........................
1698lowlow4morebiglowunacc
1699lowlow4morebigmedgood
1700lowlow4morebighighvgood
1701lowlow5more2smalllowunacc
1702lowlow5more2smallmedunacc
1703lowlow5more2smallhighunacc
1704lowlow5more2medlowunacc
1705lowlow5more2medmedunacc
1706lowlow5more2medhighunacc
1707lowlow5more2biglowunacc
1708lowlow5more2bigmedunacc
1709lowlow5more2bighighunacc
1710lowlow5more4smalllowunacc
1711lowlow5more4smallmedacc
1712lowlow5more4smallhighgood
1713lowlow5more4medlowunacc
1714lowlow5more4medmedgood
1715lowlow5more4medhighvgood
1716lowlow5more4biglowunacc
1717lowlow5more4bigmedgood
1718lowlow5more4bighighvgood
1719lowlow5moremoresmalllowunacc
1720lowlow5moremoresmallmedacc
1721lowlow5moremoresmallhighgood
1722lowlow5moremoremedlowunacc
1723lowlow5moremoremedmedgood
1724lowlow5moremoremedhighvgood
1725lowlow5moremorebiglowunacc
1726lowlow5moremorebigmedgood
1727lowlow5moremorebighighvgood
\n", "

1728 rows × 7 columns

\n", "
" ], "text/plain": [ " buying maint doors persons log_boot safety eval\n", "0 vhigh vhigh 2 2 small low unacc\n", "1 vhigh vhigh 2 2 small med unacc\n", "2 vhigh vhigh 2 2 small high unacc\n", "3 vhigh vhigh 2 2 med low unacc\n", "4 vhigh vhigh 2 2 med med unacc\n", "5 vhigh vhigh 2 2 med high unacc\n", "6 vhigh vhigh 2 2 big low unacc\n", "7 vhigh vhigh 2 2 big med unacc\n", "8 vhigh vhigh 2 2 big high unacc\n", "9 vhigh vhigh 2 4 small low unacc\n", "10 vhigh vhigh 2 4 small med unacc\n", "11 vhigh vhigh 2 4 small high unacc\n", "12 vhigh vhigh 2 4 med low unacc\n", "13 vhigh vhigh 2 4 med med unacc\n", "14 vhigh vhigh 2 4 med high unacc\n", "15 vhigh vhigh 2 4 big low unacc\n", "16 vhigh vhigh 2 4 big med unacc\n", "17 vhigh vhigh 2 4 big high unacc\n", "18 vhigh vhigh 2 more small low unacc\n", "19 vhigh vhigh 2 more small med unacc\n", "20 vhigh vhigh 2 more small high unacc\n", "21 vhigh vhigh 2 more med low unacc\n", "22 vhigh vhigh 2 more med med unacc\n", "23 vhigh vhigh 2 more med high unacc\n", "24 vhigh vhigh 2 more big low unacc\n", "25 vhigh vhigh 2 more big med unacc\n", "26 vhigh vhigh 2 more big high unacc\n", "27 vhigh vhigh 3 2 small low unacc\n", "28 vhigh vhigh 3 2 small med unacc\n", "29 vhigh vhigh 3 2 small high unacc\n", "... ... ... ... ... ... ... ...\n", "1698 low low 4 more big low unacc\n", "1699 low low 4 more big med good\n", "1700 low low 4 more big high vgood\n", "1701 low low 5more 2 small low unacc\n", "1702 low low 5more 2 small med unacc\n", "1703 low low 5more 2 small high unacc\n", "1704 low low 5more 2 med low unacc\n", "1705 low low 5more 2 med med unacc\n", "1706 low low 5more 2 med high unacc\n", "1707 low low 5more 2 big low unacc\n", "1708 low low 5more 2 big med unacc\n", "1709 low low 5more 2 big high unacc\n", "1710 low low 5more 4 small low unacc\n", "1711 low low 5more 4 small med acc\n", "1712 low low 5more 4 small high good\n", "1713 low low 5more 4 med low unacc\n", "1714 low low 5more 4 med med good\n", "1715 low low 5more 4 med high vgood\n", "1716 low low 5more 4 big low unacc\n", "1717 low low 5more 4 big med good\n", "1718 low low 5more 4 big high vgood\n", "1719 low low 5more more small low unacc\n", "1720 low low 5more more small med acc\n", "1721 low low 5more more small high good\n", "1722 low low 5more more med low unacc\n", "1723 low low 5more more med med good\n", "1724 low low 5more more med high vgood\n", "1725 low low 5more more big low unacc\n", "1726 low low 5more more big med good\n", "1727 low low 5more more big high vgood\n", "\n", "[1728 rows x 7 columns]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dfb" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### [Mission 1] 모든 속성들을 수치형 데이터로 변환\n", "- Mushroom 데이터 집합에 대한 \"Categorical Attribute를 Numerical Attribute로 변환\" 강의 내용 참조\n", "- 최종적으로 DataFrame.describe() 함수를 통하여 각 속성별로 기본 통계치를 산출" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### [Mission 2] Target 4개별로 그룹핑하여 각 속성별 기본 통계치 및 Box Plot 산출 \n", "- Mushromm 데이터 집합에 대한 \"Edible Mushrooms과 Poisonous Mushrooms 의 두 개의 그룹핑 작업 및 각 그룹별 Boxplot 그리기\" 강의 내용 참조" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### [Mission 3] 각 속성들을 두 개씩 쌍을 지어서 각 쌍마다 Scatter Plot 그리기\n", "- Iris 데이터 집합에 대한 \"탐색적 자료 분석 (Exploratory data analysis)\" 강의 내용 참조\n", "- 총 6개의 Feature가 존재하므로 두 개씩 쌍을 만든다면 총 15개의 쌍이 나옴\n", " - 예. (buying, maint), (buying, doors), ...\n", "- Categorical Data를 Numerical Attribute로 변환한 DataFrame으로 작업해야 함\n", "- 그러므로, 총 15개의 Scatter Plot을 산출해야 함\n", "- 15개의 Scatter Plot을 눈으로 분석하여 Guess 할 수 있는 분석 내용을 작성" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### [Mission 4] 전체 데이터 집합에 대한 Entropy 산출\n", "- Binary Target 이 아닌 4개의 Target 이 존재함을 유의해야 함" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### [Mission 5] Decision Tree 모델 구축을 고려하여 루트노드에서 첫번째 Branch를 위한 IG가 가장 높은 속성 산출 \n", "- 해당 속성을 잘 기억하고 Mission 6에서의 결과와 동일한지 확인해야 함" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### [Mission 6] sklearn 모듈을 활용하여 Decision Tree 모델을 구축하고 예측 시도\n", "- 루트노드에서 첫번째 Branch를 구성할 때 활용한 속성이 Mission 5에서 산출한 것과 동일한지 파악\n", " - 해당 속성에 대하여 sklearn 모듈 내에서 어떠한 방법으로 기준을 정했는지 판단 및 해석\n", "- 모델 구축 후 기존 Training Data중 일부에 대한 예측\n", "- 기존 Training Data에 존재하지 않는 새로운 데이터에 대한 예측" ] } ], "metadata": { "kernelspec": { "display_name": "Python [Root]", "language": "python", "name": "Python [Root]" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.12" } }, "nbformat": 4, "nbformat_minor": 0 }