This article explains the interaction features. This article is mainly based on "Features Engineering for Machine Learning". Please check it out if you become.
It is a method of creating a new feature by multiplying multiple features. Of these, the combination of the two features is called ** pairwise interaction features **. If the feature quantity is binary, it is a logical product. For example, if there are regions and age groups as features, by multiplying the regions and age groups, information that can better express the objective variable "20s living in Tokyo" from the information of "20s" and "living in Tokyo" can be obtained. You can create it.
However, the disadvantages are that the learning cost increases and unnecessary features are created. This increase in learning cost and the problem of unnecessary features can be solved by selecting features.
For example, suppose you have the following feature data.
When the interaction features were created for this data, the following data set was created.
Below is a sample code that actually implements the interaction features.
import numpy as np
import pandas as pd
import sklearn.preprocessing as preproc
##Random number fixed
np.random.seed(100)
data_array1 = []
for i in range(1, 100):
s = np.random.randint(0, i * 10, 10)
data_array1.extend(s)
##Random number fixed
np.random.seed(20)
data_array2 = []
for i in range(1, 100):
s = np.random.randint(0, i * 10, 10)
data_array2.extend(s)
data = pd.DataFrame({'A': data_array1, 'B': data_array2})
##Interaction features
data2 = pd.DataFrame(preproc.PolynomialFeatures(include_bias=False).fit_transform(data))
## interaction_only=You can remove the square of your own value by setting it to True.
# data2 = preproc.PolynomialFeatures(include_bias=False, interaction_only=True).fit_transform(data)
I'm thinking of posting a video about IT on YouTube. Please like, subscribe to the channel, and give us a high rating, as it will motivate youtube and Qiita updates. YouTube: https://www.youtube.com/channel/UCywlrxt0nEdJGYtDBPW-peg Twitter: https://twitter.com/tatelabo
Recommended Posts