Краткий ответ
Ваши столбцы похожи на следующий формат:
[1,
'x1',
'x2',
'x3',
'x4',
'x5',
'x6',
'x1 * x2',
'x1 * x3',
'x1 * x4',
'x1 * x5',
'x1 * x6',
'x2 * x3',
'x2 * x4',
'x2 * x5',
'x2 * x6',
'x3 * x4',
'x3 * x5',
'x3 * x6',
'x4 * x5',
'x4 * x6',
'x5 * x6']
Если вы присваиваете эти значения переменной gen_col_names
и конвертируете в DataFrame, вы можете увидеть, что продолжается.
pd.DataFrame(df_interactions_T,columns=gen_col_names)
Длинный ответ
Давайте посмотрим на исходный код и посмотрим, что происходит: https://github.com/scikit-learn/scikit-learn/blob/b194674c4/sklearn/preprocessing/_data.py#L1516
Источник код для комбинаций следующий:
from itertools import chain, combinations
from itertools import combinations_with_replacement as combinations_w_r
def _combinations(n_features, degree, interaction_only, include_bias):
comb = (combinations if interaction_only else combinations_w_r)
start = int(not include_bias)
return chain.from_iterable(comb(range(n_features), i)
for i in range(start, degree + 1))
Создайте данные:
import numpy as np
import pandas as pd
np.random.seed(0)
cols = ['x1','x2','x3','x4','x5','x6']
df = pd.DataFrame()
for col in cols:
df[col] = np.random.randint(1,10,100)
df_interactions = df[['x1','x2','x3','x4','x5','x6']]
from sklearn.preprocessing import PolynomialFeatures
poly = PolynomialFeatures(interaction_only=True,degree=2)
df_interactions_T = poly.fit_transform(df_interactions)
Ваши параметры похожи на следующие:
n_features = 6
degree = 2
interaction_only = True
include_bias = True
combs = list(_combinations(n_features=6, degree=2, interaction_only=True, include_bias=True))
combs
[(),
(0,),
(1,),
(2,),
(3,),
(4,),
(5,),
(0, 1),
(0, 2),
(0, 3),
(0, 4),
(0, 5),
(1, 2),
(1, 3),
(1, 4),
(1, 5),
(2, 3),
(2, 4),
(2, 5),
(3, 4),
(3, 5),
(4, 5)]
Вы можете использовать эту информацию для генерации имен столбцов:
gen_col_names = []
for i in combs:
if i == ():
gen_col_names.append(1)
if len(i) == 1:
gen_col_names.append(cols[i[0]])
if len(i) == 2:
gen_col_names.append(cols[i[0]] + ' * ' + cols[i[1]])
gen_col_names
[1,
'x1',
'x2',
'x3',
'x4',
'x5',
'x6',
'x1 * x2',
'x1 * x3',
'x1 * x4',
'x1 * x5',
'x1 * x6',
'x2 * x3',
'x2 * x4',
'x2 * x5',
'x2 * x6',
'x3 * x4',
'x3 * x5',
'x3 * x6',
'x4 * x5',
'x4 * x6',
'x5 * x6']