一、理解
交互特征与多项式特征与数据预处理中的MinMaxScaler是相似的,都是对数据进行缩放处理
缩放处理、交互特征与多项式特征都是对原始数据进行缩放,缩放意义在于使得权重与偏置更具有敏感性,更易对数据预测
二、测试代码比较
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import Ridge
from sklearn.ensemble import RandomForestRegressor
"""波士顿房价数据集”“”
"""加载数据,利用MinMaxScaler将其放缩到0-1之间。"""
boston = load_boston()
X_train, X_test, y_train, y_test = train_test_split( boston.data, boston.target, random_state=0)
scaler = MinMaxScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
"""提取多项式特征和交互特征"""
poly = PolynomialFeatures(degree=2).fit(X_train_scaled)
X_train_poly = poly.transform(X_train_scaled)
X_test_poly = poly.transform(X_test_scaled)
print("X_train.shape: {}".format(X_train.shape))
print("X_train_poly.shape: {}".format(X_train_poly.shape))
"""Ridge在有交互特征地数据集和没有交互特征地数据上地性能进行对比"""
ridge = Ridge().fit(X_train, y_train)
print("Origin: {:.3f}".format(ridge.score(X_test, y_test)))
ridge = Ridge().fit(X_train_scaled, y_train)
print("Score without interactions: {:.3f}".format(ridge.score(X_test_scaled, y_test)))
ridge = Ridge().fit(X_train_poly, y_train)
print("Score with interactions: {:.3f}".format(ridge.score(X_test_poly, y_test)))
"""随机森林"""
rf = RandomForestRegressor(n_estimators=400).fit(X_train, y_train)
print("Origin: {:.3f}".format(rf.score(X_test, y_test)))
rf = RandomForestRegressor(n_estimators=400).fit(X_train_scaled, y_train)
print("Score without interactions: {:.3f}".format(rf.score(X_test_scaled, y_test)))
rf = RandomForestRegressor(n_estimators=400).fit(X_train_poly, y_train)
print("Score with interactions: {:.3f}".format(rf.score(X_test_poly, y_test)))
#对回归模型更能增加精确度,而对分类模型则有所相反
输出
X_train.shape: (379, 13)
X_train_poly.shape: (379, 105)
Origin: 0.627
Score without interactions: 0.621
Score with interactions: 0.753
Origin: 0.791
Score without interactions: 0.802
Score with interactions: 0.768