week_11_features_interaction
week_11_features_interaction
Import Modules
In [1]: import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
Read data
In [3]: df = pd.read_csv('linear_additive_example.csv')
In [4]: df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 35 entries, 0 to 34
Data columns (total 4 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 x1 35 non-null float64
1 x2 35 non-null float64
2 trend 35 non-null float64
3 y 35 non-null float64
dtypes: float64(4)
memory usage: 1.2 KB
In [5]: df.head()
Out[5]: x1 x2 trend y
1 of 7 11/16/2024, 10:26 AM
week_11_features_interaction https://d3c33hcgiwev3.cloudfront.net/d3p9joI9RIWmaZxVB9aToA_a3...
In [7]: fit_a.params
Let's use the FORMULA INTERFACES actual SYNTAX for the MULTIPLICATION or
INTERACTION FEATURE.
In [9]: fit_b.params
In [10]: fit_a.bse
In [11]: fit_b.bse
In [12]: fit_a.pvalues
In [13]: fit_b.pvalues
2 of 7 11/16/2024, 10:26 AM
week_11_features_interaction https://d3c33hcgiwev3.cloudfront.net/d3p9joI9RIWmaZxVB9aToA_a3...
In [15]: fit_c.params
In [16]: fit_b.bse
In [17]: fit_c.bse
In [18]: fit_b.pvalues
In [19]: fit_c.pvalues
This means if we would ONLY want PRODUCTS then we should use the : operator in the
FORMULA.
3 of 7 11/16/2024, 10:26 AM
week_11_features_interaction https://d3c33hcgiwev3.cloudfront.net/d3p9joI9RIWmaZxVB9aToA_a3...
In [21]: fit_d.params
There is ONE MORE way of representing the SHORT CUT and this is related to WHY we need
to use np.power() when we want to RAISE an input to a polynomial degree.
In [23]: df.x1.head() ** 2
Out[23]: 0 1.049092
1 3.005615
2 0.007973
3 1.196285
4 0.103038
Name: x1, dtype: float64
In [26]: fit_e.params
In [28]: fit_f.params
In [30]: fit_g.params
4 of 7 11/16/2024, 10:26 AM
week_11_features_interaction https://d3c33hcgiwev3.cloudfront.net/d3p9joI9RIWmaZxVB9aToA_a3...
In [32]: fit_h.params
A shortcut is that you can CREATE up to and INCLUDING ALL DEGREE OF INTERACTIONS
using the ** operator in the FORMULA interface!!!
In [34]: fit_i.params
You can use this notation to create very COMPLEX interactions between many different types
of FEATURES!!!
In [36]: fit_j.params
When you want to INTERACT polynomials...I like to separate the POLYNOMIALS derived from
each input using () in the formula interface!!!
In [38]: fit_k.params
5 of 7 11/16/2024, 10:26 AM
week_11_features_interaction https://d3c33hcgiwev3.cloudfront.net/d3p9joI9RIWmaZxVB9aToA_a3...
Make Predictions
You do NOT need to create the interaction features yourself!!!!
You ONLY need to define the INPUTS and the model object will generate all necessary
features for you...because the FORMULA INTERACE REMEMBERS what to do!!!
In [43]: input_grid
Out[43]: x1 x2
0 -1.733671 -1.813330
1 -1.733671 -1.269344
2 -1.733671 -0.725357
3 -1.733671 -0.181371
4 -1.733671 0.362615
In [40]: input_grid.nunique()
Out[40]: x1 101
x2 9
dtype: int64
6 of 7 11/16/2024, 10:26 AM
week_11_features_interaction https://d3c33hcgiwev3.cloudfront.net/d3p9joI9RIWmaZxVB9aToA_a3...
PREDICT the AVERAGE OUTPUT or TREND using the last model we fit.
plt.show()
In [ ]:
7 of 7 11/16/2024, 10:26 AM