let us not love with words or tongue but actions and truth.

IT/파이썬

의사결정나무(Decision Tree) 그래프 그리기

sarah0518 2020. 12. 10. 09:32
728x90

간혹 사내망에서 혹은 네트워크 보안 관련 이슈로 특정 library가 import 되지 않는 경우가 있습니다.

 

한 예로, 의사결정나무 그래프를 그리기 위해 graphviz를 import 할 때의 이슈가 있겠습니다.

그럴땐 아래 코드에서 처럼, --trust-host 뒤에 명시해준 사이트에서 import 할 수 있도록 install하여 해결할 수 있을거에요.

>> !pip install --trusted-host pypi.python.org --trusted-host pypi.org --trusted-host files.pythonhosted.org graphviz 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
!pip install --trusted-host pypi.python.org --trusted-host pypi.org --trusted-host files.pythonhosted.org graphviz
from sklearn.tree import export_graphviz
import graphviz
 
export_graphviz(dtc, out_file="decisionTree1.dot",feature_names=x_df2.columns,
                class_names=["Fail","Pass"],rounded=True, filled=True)
 
with open("decisionTree1.dot", encoding='utf8'as f:
    dot_graph = f.read()
 
dot=graphviz.Source(dot_graph)
dot.format='png'
dot.render(filename='decisionTree1',directory='images/tree',cleanup=True)
# 'images/tree/decisionTree1.png'
# dot 이라는 변수에 들어있는 graph를 가져옴
dot
cs

>> dot 의 print 결과는 아래와 같습니다.

728x90