## PolynomialFeatures調査
- 動作を調べ、ラベル付けを行う


```python
import pandas as pd
import numpy as np
from sklearn.preprocessing import PolynomialFeatures
import sympy
```

## ダミーデータ作り


```python
# 1番目からn番目までの素数をnp.arrayで返す関数
def primes(n=0):
    ret_primes = list()
    for i in np.arange(n):
        ret_primes.append(sympy.prime(i+1))
    return ret_primes
```


```python
# コラム名リストからコラムに対してユニークな素数が割り付けられたデータフレームを返す関数
def generate_df_prime_from_column(columns_original = np.array(["a","b","c"])):
    data_original = np.array(primes(len(columns_original)))
    return pd.DataFrame(data=data_original[np.newaxis,:],columns=columns_original,index=["original"])

# テスト
display(generate_df_prime_from_column())
```


<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>a</th>
      <th>b</th>
      <th>c</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>original</th>
      <td>2</td>
      <td>3</td>
      <td>5</td>
    </tr>
  </tbody>
</table>
</div>


## PolynomialFeaturesの動作を調べる
### PolynomialFeatures
- degree : integer
The degree of the polynomial features. Default = 2.
- interaction_only : boolean, default = False
If true, only interaction features are produced: features that are products of at most degree distinct input features (so not x[1] ** 2, x[0] * x[2] ** 3, etc.).
- include_bias : boolean
If True (default), then include a bias column, the feature in which all polynomial powers are zero (i.e. a column of ones - acts as an intercept term in a linear model).
- order : str in {‘C’, ‘F’}, default ‘C’
Order of output array in the dense case. ‘F’ order is faster to compute, but may slow down subsequent estimators.

from https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.PolynomialFeatures.html

### データフレームとしてPolynomialFeatureの結果を返す関数
コラム名もa×bなど、何と何を掛けたものであるかが分かるようにしている


```python
def investigate_PolynomialFeatures(poly=PolynomialFeatures(2),columns_from=["a","b","c"],power=False):
    df_from = generate_df_prime_from_column(columns_from)
    columns_from = df_from.columns
    data_from = df_from.values
    data_poly = poly.fit_transform(df_from)
    # columnをもう一度作り直す
    columns_poly = list()
    for i in np.arange(data_poly.shape[1]):
        if (data_poly[0][i] == 1):
            columns_poly.append("bias")
        else:
            prime_dict=sympy.factorint(data_poly[0][i])
            keys = list(prime_dict.keys())
            column_str = ""
            if power:
                # 累乗で書ける部分は累乗で書く(例:a^2)
                for j in np.arange(len(keys)):
                    column_str += columns_from[list(data_from[0]).index(keys[j])]
                    if prime_dict[keys[j]] > 1:
                        column_str += "^" + str(prime_dict[keys[j]])
                    if (j < len(keys)-1):
                        column_str += "×"
            else:
                # 単純に×で項目をつなげていく(例:a×a×b)
                for j in np.arange(len(keys)):
                    for k in np.arange(prime_dict[keys[j]]):
                        column_str += columns_from[list(data_from[0]).index(keys[j])]
                        if (j < len(keys)-1) | (k < prime_dict[keys[j]]-1):
                            column_str += "×"
            columns_poly.append(column_str)
    return pd.DataFrame(data=data_poly,columns=columns_poly,index=["poly"])
```

### 調査結果
- バイアス→係数1個→係数2個→・・・の順
- 係数2個の中では、a×(a→b→c)→b×(b→c)→c×cの順
- interaction_only = True (デフォルトはFalse)の時は同じ係数が2個以上登場しないもののみとなる


```python
print("degree=2のとき")
display(investigate_PolynomialFeatures())

print("")
print("degree=2, interaction_only=Trueのとき")
display(investigate_PolynomialFeatures(poly=PolynomialFeatures(degree=2,interaction_only=True)))

print("")
print("degree=2, interaction_only=True, include_bias=Falseのとき")
display(investigate_PolynomialFeatures(poly=PolynomialFeatures(degree=2,interaction_only=True,include_bias=False)))

print("")
print("degree=3, interaction_only=False, include_bias=Falseのとき")
display(investigate_PolynomialFeatures(poly=PolynomialFeatures(degree=3,include_bias=False),power=True))

print("")
print("degree=3, interaction_only=True, include_bias=Falseのとき")
display(investigate_PolynomialFeatures(poly=PolynomialFeatures(degree=3,interaction_only=True,include_bias=False)))
```

    degree=2のとき



<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>bias</th>
      <th>a</th>
      <th>b</th>
      <th>c</th>
      <th>a×a</th>
      <th>a×b</th>
      <th>a×c</th>
      <th>b×b</th>
      <th>b×c</th>
      <th>c×c</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>poly</th>
      <td>1.0</td>
      <td>2.0</td>
      <td>3.0</td>
      <td>5.0</td>
      <td>4.0</td>
      <td>6.0</td>
      <td>10.0</td>
      <td>9.0</td>
      <td>15.0</td>
      <td>25.0</td>
    </tr>
  </tbody>
</table>
</div>


    
    degree=2, interaction_only=Trueのとき



<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>bias</th>
      <th>a</th>
      <th>b</th>
      <th>c</th>
      <th>a×b</th>
      <th>a×c</th>
      <th>b×c</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>poly</th>
      <td>1.0</td>
      <td>2.0</td>
      <td>3.0</td>
      <td>5.0</td>
      <td>6.0</td>
      <td>10.0</td>
      <td>15.0</td>
    </tr>
  </tbody>
</table>
</div>


    
    degree=2, interaction_only=True, include_bias=Falseのとき



<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>a</th>
      <th>b</th>
      <th>c</th>
      <th>a×b</th>
      <th>a×c</th>
      <th>b×c</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>poly</th>
      <td>2.0</td>
      <td>3.0</td>
      <td>5.0</td>
      <td>6.0</td>
      <td>10.0</td>
      <td>15.0</td>
    </tr>
  </tbody>
</table>
</div>


    
    degree=3, interaction_only=False, include_bias=Falseのとき



<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>a</th>
      <th>b</th>
      <th>c</th>
      <th>a^2</th>
      <th>a×b</th>
      <th>a×c</th>
      <th>b^2</th>
      <th>b×c</th>
      <th>c^2</th>
      <th>a^3</th>
      <th>a^2×b</th>
      <th>a^2×c</th>
      <th>a×b^2</th>
      <th>a×b×c</th>
      <th>a×c^2</th>
      <th>b^3</th>
      <th>b^2×c</th>
      <th>b×c^2</th>
      <th>c^3</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>poly</th>
      <td>2.0</td>
      <td>3.0</td>
      <td>5.0</td>
      <td>4.0</td>
      <td>6.0</td>
      <td>10.0</td>
      <td>9.0</td>
      <td>15.0</td>
      <td>25.0</td>
      <td>8.0</td>
      <td>12.0</td>
      <td>20.0</td>
      <td>18.0</td>
      <td>30.0</td>
      <td>50.0</td>
      <td>27.0</td>
      <td>45.0</td>
      <td>75.0</td>
      <td>125.0</td>
    </tr>
  </tbody>
</table>
</div>


    
    degree=3, interaction_only=True, include_bias=Falseのとき



<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>a</th>
      <th>b</th>
      <th>c</th>
      <th>a×b</th>
      <th>a×c</th>
      <th>b×c</th>
      <th>a×b×c</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>poly</th>
      <td>2.0</td>
      <td>3.0</td>
      <td>5.0</td>
      <td>6.0</td>
      <td>10.0</td>
      <td>15.0</td>
      <td>30.0</td>
    </tr>
  </tbody>
</table>
</div>



```python

```

0fb9cf552d6e2866a932a758e4ff7629d50d263b
ipynb
Jupyter Notebook
001_poly/PolynominalFeatures.ipynb
company-rapper/blog
3dda7dcbfa2d2436db03bfb7e01813e8a05f4acc
MIT
001_poly/PolynominalFeatures.ipynb
company-rapper/blog
3dda7dcbfa2d2436db03bfb7e01813e8a05f4acc
MIT
001_poly/PolynominalFeatures.ipynb
company-rapper/blog
3dda7dcbfa2d2436db03bfb7e01813e8a05f4acc
MIT
Qwen/Qwen-72B
1. YES
2. YES