Python을 이용한 Decision Tree (2)

DT Assignment2

Entropy를 구하고, 각 변수에 대한 Gain을 구하는 함수를 구현하는 과제입니다.

DT_Assignment2.ipynb 파일에 있는 두가지 함수를 만들어 주시면 됩니다. 결과는 주어져 있습니다.

두번째 함수는 출력값이 꼭 주어진 형태와 일치할 필요는 없습니다. 봤을 때 각 변수에 대한 Gain을 알아볼 수 있도록 구성해 주세요.

마찬가지로 주석 꼼꼼히 달아주세요!

우수과제 선정이유

함수마다 잘 작동하는지 확인하는 과정을 거치는 것이 인상적이였습니다.

또한 1번 과제의 3번 문제를 해결할 때 데이터 프레임을 나누는 함수를 만들고 문제에서 요구하는 것보다 한층 더 깊게 살펴보는 모습이 굉장히 좋았습니다.

import pandas as pd 
import numpy as np
import warnings
warnings.filterwarnings("ignore")

df = pd.read_csv('https://raw.githubusercontent.com/AugustLONG/ML01/master/01decisiontree/AllElectronics.csv')
df.drop("RID",axis=1, inplace = True) #RID는 그냥 Index라서 삭제

In [117]:

df

Out[117]:

age

income

student

credit_rating

class_buys_computer

0

youth

high

no

fair

no

1

youth

high

no

excellent

no

2

middle_aged

high

no

fair

yes

3

senior

medium

no

fair

yes

4

senior

low

yes

fair

yes

5

senior

low

yes

excellent

no

6

middle_aged

low

yes

excellent

yes

7

youth

medium

no

fair

no

8

youth

low

yes

fair

yes

9

senior

medium

yes

fair

yes

10

youth

medium

yes

excellent

yes

11

middle_aged

medium

no

excellent

yes

12

middle_aged

high

yes

fair

yes

13

senior

medium

no

excellent

no

함수 만들기

In [118]:

In [119]:

Out[119]:

In [5]:

Out[5]:

In [159]:

In [161]:

Out[161]:

In [160]:

Out[160]:

정답

결과 확인하기

In [139]:

In [7]:

Last updated

Was this helpful?