特征工程:交互特征与多项式特征理解

特征工程:交互特征与多项式特征理解

一、理解

交互特征与多项式特征与数据预处理中的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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值