[ Python ] combination 조합 만들기

2019. 7. 24. 15:22분석 Python/구현 및 자료

728x90

가끔씩 어떤 조합의 경우의 수를 뽑고 싶은 경우가 있다.

print(Seg)

['X1', 'X2', 'X3', 'X4', 'X5', 'X6', 'X7', 'X8', 'X9', 'X10', 'X11', 'X12', 'X13', 'X14', 'X15', 'X16', 'X17']

 

여기서 17C2 또는 17C3을 구하고 싶다고 하자.

 

그러면 다음과 같은 코드면 된다.

import itertools
combination = []
for ii in range(2,  4 ):
    for subset in itertools.combinations(Seg , ii):
        combination.append(list(subset))
        
        
        
[['X1', 'X2'], ['X1', 'X3'], ['X1', 'X4'], ['X1', 'X5'],
['X1', 'X6'], ['X1', 'X7'], ['X1', 'X8'], ['X1', 'X9'],
['X1', 'X10'], ['X1', 'X11'], ['X1', 'X12'], ['X1', 'X13'],
['X1', 'X14'], ['X1', 'X15'], ['X1', 'X16'], ['X1', 'X17'], 
...
...
['X1', 'X2', 'X6'], ['X1', 'X2', 'X7'], ['X1', 'X2', 'X8'],
['X1', 'X2', 'X9'], ['X1', 'X2', 'X10'], ['X1', 'X2', 'X11'],
['X1', 'X2', 'X12'], ['X1', 'X2', 'X13'], ['X1', 'X2', 'X14'], 
['X1', 'X2', 'X15'], ['X1', 'X2', 'X16'], ['X1', 'X2', 'X17'], 
....

 

https://towardsdatascience.com/stacking-classifiers-for-higher-predictive-performance-566f963e4840

 

Stacking Classifiers for Higher Predictive Performance

Using the Wisdom of the Multiple Classifiers to Boost Performance

towardsdatascience.com

 

728x90