Phython라이브러리에 순열 조합에 대한 제공함수가 있다.
1. 순열
A B C의 2개를 뽑았을대 조합 (인자로 뽑을 개수를 지정할 수 있다)
import itertools
a = ['A','B','C']
for i in itertools.permutations('ABC',2):
print(i)
('A', 'B')
('A', 'C')
('B', 'A')
('B', 'C')
('C', 'A')
('C', 'B')
2. 조합
import itertools
a = ['A','B','C']
for i in itertools.combinations('ABC',2):
print(i)
('A', 'B')
('A', 'C')
('B', 'C')
3. 데카르트 곱
a = ['A', 'B','C']
b = ['1', '2']
for i in itertools.product(a,b,repeat=1) # a과 b의 모든 쌍을 리턴한다
print(i)
('A', '1')
('A', '2')
('B', '1')
('B', '2')
('C', '1')
('C', '2')
이걸 변수선택 시 활용할 수 있다.
예를 들어 선형회귀 모델에서 변수 순서에 따라 aic 값의 차이가 있을까?
순열로 변수의 모든 조합을 for문으로 돌려 결과를 확인 할 수 있다. 변수 순서에 따른 차이는 없는것 같다.
for clist in itertools.permutations(['displacement', 'horsepower', 'weight', 'acceleration'],4):
model = ols(formula='mpg ~ ' + '+'.join(clist), data=df).fit()
print(model.aic, clist)
2251.195457422522 ('displacement', 'horsepower', 'weight', 'acceleration')
2251.195457422522 ('displacement', 'horsepower', 'acceleration', 'weight')
2251.195457422522 ('displacement', 'weight', 'horsepower', 'acceleration')
2251.195457422522 ('displacement', 'weight', 'acceleration', 'horsepower')
2251.195457422522 ('displacement', 'acceleration', 'horsepower', 'weight')
2251.195457422522 ('displacement', 'acceleration', 'weight', 'horsepower')
2251.195457422522 ('horsepower', 'displacement', 'weight', 'acceleration')
2251.195457422522 ('horsepower', 'displacement', 'acceleration', 'weight')
2251.195457422522 ('horsepower', 'weight', 'displacement', 'acceleration')
2251.195457422522 ('horsepower', 'weight', 'acceleration', 'displacement')
2251.195457422522 ('horsepower', 'acceleration', 'displacement', 'weight')
2251.195457422522 ('horsepower', 'acceleration', 'weight', 'displacement')
2251.195457422522 ('weight', 'displacement', 'horsepower', 'acceleration')
2251.195457422522 ('weight', 'displacement', 'acceleration', 'horsepower')
2251.195457422522 ('weight', 'horsepower', 'displacement', 'acceleration')
2251.195457422522 ('weight', 'horsepower', 'acceleration', 'displacement')
2251.195457422522 ('weight', 'acceleration', 'displacement', 'horsepower')
2251.195457422522 ('weight', 'acceleration', 'horsepower', 'displacement')
2251.195457422522 ('acceleration', 'displacement', 'horsepower', 'weight')
2251.195457422522 ('acceleration', 'displacement', 'weight', 'horsepower')
2251.195457422522 ('acceleration', 'horsepower', 'displacement', 'weight')
2251.195457422522 ('acceleration', 'horsepower', 'weight', 'displacement')
2251.195457422522 ('acceleration', 'weight', 'displacement', 'horsepower')
2251.195457422522 ('acceleration', 'weight', 'horsepower', 'displacement')
그러면, 모든 변수 조합에 따른 aic가 낮은 높은 조합은 무엇일까?
df = sns.load_dataset('mpg')
df = df.select_dtypes(exclude=object)
col = df.columns.drop('mpg').to_list()
print(col)
mydic = {
"aic":[],
"collist":[],
}
# 1개에서 변수 개수만큼 변수 조합별 aic값을 dataframe으로 출력한다.
for i in range(1,len(col)):
for clist in itertools.combinations(col,i+1):
model = ols(formula='mpg ~ ' + '+'.join(clist), data=df).fit()
# print(model.aic, clist)
mydic["aic"].append(model.aic)
mydic["collist"].append(",".join(clist))
aicdf = pd.DataFrame(mydic)
# aic값이 높은 순서로 정렬한다.
aicdf.sort_values(by='aic', ascending=True)
* itertools 의 combinations 를 통해 모든 변수 조합의 값을 확인할 수 있다.
손으로 풀면 상당한 작업일듯한데, 기억해두면 유용한 함수이다.
'전처리' 카테고리의 다른 글
ColumnTransformer (0) | 2024.04.20 |
---|---|
Sklearn Featureselection방법들 (0) | 2023.12.28 |
EDA 탐색적 데이터 분석 (0) | 2023.10.06 |
feature_selection (변수 선택 법) (0) | 2023.08.03 |
Seaborn (0) | 2023.08.01 |