2017-04-05 4 views
0

주어진 피쳐 수와 해당 속성의 변종 수를 평가 한 다음 각 브랜치가 하나의 조합/변형을 나타내는 브랜치 트리로 조합을 그립니다.Python을 사용하여 변형 트리를 만들려면 어떻게해야합니까?

예 : 특징 : 색상, 크기, 희미 함. 각 기능마다 속성이 (색상 : 빨강, 초록, 파랑, 크기 : 크고 작음)이 다릅니다.

순열을 통해 변형 수를 찾습니다. 각 조합/변형은 내 변형 트리의 한 지점입니다. 예 : ('red', 'big', 1)은 하나의 가지이고, 'red', 'big', 2는 또 다른 것입니다.

노드와 호가있는이 분기를 끌어낼 수있는 libary가 있습니까? 순열에 대한

코드 :

colors = ['red', 'green', 'blue'] 
size = ['big','small'] 
dim = [1,2,3] 

from itertools import product 

x =list (product(colors,size,dim)) 

print (x) 
print ("Number of variants:",len(x)) 

[('red', 'big', 1), ('red', 'big', 2), ('red', 'big', 3), 
('red', 'small', 1), ('red', 'small', 2), ('red', 'small', 3), 
('green', 'big', 1), ('green', 'big', 2), ('green', 'big', 3), 
('green', 'small', 1), ('green', 'small', 2), ('green', 'small', 3), 
('blue', 'big', 1), ('blue', 'big', 2), ('blue', 'big', 3), 
('blue', 'small', 1), ('blue', 'small', 2), ('blue', 'small', 3)] 

enter image description here

+0

dim 이미지가 예제 이미지에서 red/big 브랜치에만 표시되는 이유는 무엇입니까? – ikkuh

+0

plotly ...... https : //plot.ly/python/tree-plots/ –

+0

dim은 한 가지 예에 대해서만 설명되었지만 다른 색상/크기 조합에도 나타납니다. – BjoernBorkenkaefer

답변

0

나는 일반 그래프 설명 형식 DOT을 사용하여 this을 만들 수 있었다. 도트 파일을 생성 파이썬 파일 main.py : 내가 사용

$ python main.py > graph.dot 
$ dot -Tpng graph.dot -o graph.png 

도트 명령은 graphviz 패키지입니다 :

from itertools import product 

colors = ['red', 'green', 'blue'] 
size = ['big','small'] 
dim = [1,2,3] 

print('digraph G {\n rankdir=LR\n node [shape=box]\n') 
print(' start [label=""]') 

for i, c in enumerate(colors): 
    print(f' color_{i} [label="{c}"]; start -> color_{i}') 

    for j, s in enumerate(size): 
     print(f' size_{i}_{j} [label="{s}"]; color_{i} -> size_{i}_{j}') 

     for k, d in enumerate(dim): 
      print(f' dim_{i}_{j}_{k} [label="{d}"]; size_{i}_{j} -> dim_{i}_{j}_{k}') 
print('}') 

나는 다음과 같은 명령을 사용하여 이미지를 만들었습니다. DOT를 많이 사용하지 않아 텍스트와 파란색을 추가 할 수 없었습니다.