机器学习—回归与分类4-3(AdaBoost算法)
使用AdaBoost预测黑色星期五花销
主要步骤流程:
- 1. 导入包
- 2. 导入数据集
- 3. 数据预处理
- 3.1 检测并处理缺失值
- 3.2 删除无用的列
- 3.3 检查类别型变量
- 3.4 标签编码&独热编码
- 3.5 得到自变量和因变量
- 3.6 拆分训练集和测试集
- 3.7 特征缩放
- 4. 使用不同的参数构建AdaBoost回归模型
- 4.1 模型1:构建AdaBoost回归模型
- 4.1.1 构建模型
- 4.1.2 测试集做预测
- 4.1.3 评估模型性能
- 4.2 模型2:构建AdaBoost回归模型
- 4.1 模型1:构建AdaBoost回归模型
1. 导入包
In [2]:
# 导入包
import numpy as np
import pandas as pd
2. 导入数据集
In [3]:
# 导入数据集
data = pd.read_csv('BlackFriday.csv')
data
Out[3]:
3. 数据预处理
3.1 检测并处理缺失值
In [4]:
# 检测缺失值
null_df = data.isnull().sum()
null_df
Out[4]:
In [5]:
# 删除缺失列
data = data.drop(['Product_Category_2', 'Product_Category_3'], axis = 1)
In [6]:
# 再次检测缺失值
null_df = data.isnull().sum()
null_df
Out[6]:
3.2 删除无用的列
In [7]:
# 删除无用的列
data = data.drop(['User_ID', 'Product_ID'], axis = 1)
3.3 检查类别型变量
In [8]:
# 检查类别型变量
print(data.dtypes)
In [9]:
# 转换变量类型
data['Stay_In_Current_City_Years'].replace('4+', 4, inplace = True)
data['Stay_In_Current_City_Years'] = data['Stay_In_Current_City_Years'].astype('int64')
data['Product_Category_1'] = data['Product_Category_1'].astype('object')
data['Occupation'] = data['Occupation'].astype('object')
data['Marital_Status'] = data['Marital_Status'].astype('object')
In [10]:
# 检查类别型变量
print(data.dtypes)
3.4 标签编码&独热编码
In [11]:
# 标签编码&独热编码
data = pd.get_dummies(data, drop_first = True)
data
Out[11]:
3.5 得到自变量和因变量
In [12]:
# 得到自变量和因变量
y = data['Purchase'].values
data = data.drop(['Purchase'], axis = 1)
x = data.values
3.6 拆分训练集和测试集
In [13]:
# 拆分训练集和测试集
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.3, random_state = 1)
print(x_train.shape)
print(x_test.shape)
print(y_train.shape)
print(y_test.shape)
3.7 特征缩放
In [14]:
# 特征缩放
from sklearn.preprocessing import StandardScaler
sc_x = StandardScaler()
x_train = sc_x.fit_transform(x_train)
x_test = sc_x.transform(x_test)
sc_y = StandardScaler()
y_train = np.ravel(sc_y.fit_transform(y_train.reshape(-1, 1)))
4. 使用不同的参数构建AdaBoost回归模型
4.1 模型1:构建AdaBoost回归模型
4.1.1 构建模型
In [15]:
# 使用不同的参数构建AdaBoost回归模型
# 模型1:构建AdaBoost回归模型(base_estimator=None, n_estimators=50, learning_rate=1)
from sklearn.ensemble import AdaBoostRegressor
regressor = AdaBoostRegressor(n_estimators=50, learning_rate=1, loss='linear', random_state=0)
regressor.fit(x_train, y_train)
Out[15]:
4.1.2 测试集做预测
In [16]:
# 在测试集做预测
y_pred = regressor.predict(x_test)
y_pred[:5]
Out[16]:
In [17]:
# y_pred变回特征缩放之前的
y_pred = sc_y.inverse_transform(y_pred)
y_pred[:5]
Out[17]:
4.1.3 评估模型性能
In [18]:
# 评估模型性能
from sklearn.metrics import r2_score
r2 = r2_score(y_test, y_pred)
print("R2 Score:", r2)
4.2 模型2:构建AdaBoost回归模型
In [29]:
# 模型2:构建AdaBoost回归模型(base_estimator=DecisionTreeRegressor, n_estimators=2000, learning_rate=0.1)
from sklearn.tree import DecisionTreeRegressor
regressor = AdaBoostRegressor(base_estimator = DecisionTreeRegressor(min_samples_split=100, max_depth=10, min_samples_leaf=10),
n_estimators=1000, learning_rate=0.2, loss='linear', random_state=0)
regressor.fit(x_train, y_train)
Out[29]:
In [30]:
# 在测试集做预测
y_pred = regressor.predict(x_test)
y_pred = sc_y.inverse_transform(y_pred) # y_pred变回特征缩放之前的
In [31]:
# 评估模型性能
r2 = r2_score(y_test, y_pred)
print("R2 Score:", r2)
结论: 由上面2个模型可见,不同超参数对模型性能的影响不同