简单记录下学习过程
1.K折交叉验证 + 随机森林回归预测
from sklearn.model_selection import KFold
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import r2_score
n_estimators = 100
max_depth = 80
min_samples_split = 3
min_samples_leaf = 2
fold = 5
kf = KFold(n_splits=fold, shuffle=shuffle, random_state=random_state)
models = []
all_train_r2_score = []
all_valid_r2_score = []
for train_idx, valid_idx in kf.split(train_x):
print(f'train_idx={train_idx} | valid_idx={valid_idx}')
x_train, x_valid = train_x[train_idx], train_x[valid_idx]
y_train, y_valid = train_y[train_idx], train_y[valid_idx]
model = RandomForestRegressor(n_estimators=n_estimators,
max_depth=max_depth,
min_samples_split=min_samples_split,
min_samples_leaf=min_samples_leaf,
n_jobs=-1,
verbose=1)
model.fit(x_train,y_train)
models.append(model)
y_train_pred = model.predict(x_train)
train_r2_score = r2_score(y_train, y_train_pred)
y_valid_pred = model.predict(x_valid)
valid_r2_score = r2_score(y_valid, y_valid_pred)
all_train_r2_score.append(train_r2_score)
all_valid_r2_score.append(valid_r2_score)
print(f'train_r2_score={train_r2_score:.4f} | valid_r2_score={valid_r2_score:.4f}')
mean_train_r2_score = sum(all_train_r2_score) / k_num
mean_valid_r2_score = sum(all_valid_r2_score) / k_num
print(f'mean_train_r2_score={mean_train_r2_score:.4f} | mean_valid_r2_score={mean_valid_r2_score:.4f}')
![运行结果](https://i-blog.csdnimg.cn/direct/9b414ca06d104cdf91178892dfe43479.png#pic_center)
2.K折交叉验证+随机森林回归预测+网格搜索
'''
n_estimators三个参数
max_depth三个参数
KFold用于内部交叉验证,n_splits=3
如果外部在用一个KFold交叉验证,n_splits=5
那么就需要跑5*3*3*3=135次随机森林预测
上面n_estimators=100的随机森林都要跑11min,且n_estimators越大跑越久,135次的时间上划不来
网格搜索在cuML是没有的,但是可以手动for循环暴力枚举
例如for n_es in param_grid['n_estimators']:
for m_dep in param_grid['max_depth']:
...
'''
base_model = RandomForestRegressor(n_estimators=n_estimators,
max_depth=max_depth,
min_samples_split=min_samples_split,
min_samples_leaf=min_samples_leaf,
n_jobs=-1,
verbose=1)
param_grid = {
'n_estimators': [50, 100, 200],
'max_depth': [60, 80, 100]
}
grid_search = GridSearchCV(
estimator=base_model ,
param_grid=param_grid,
cv=KFold(n_splits=3),
scoring='r2',
verbose=2,
return_train_score=True,
n_jobs=-1
)
3.K折交叉 + cuML(让机器学习可以使用GPU加速)
import cudf
from cuml.ensemble import RandomForestRegressor
from sklearn.utils import resample
from sklearn.model_selection import KFold
import time
from sklearn.metrics import r2_score
start_time = time.time()
kf = KFold(n_splits=fold, shuffle=shuffle, random_state=random_state)
base_model = RandomForestRegressor(n_estimators=n_estimators,
max_depth=max_depth,
min_samples_split=min_samples_split,
min_samples_leaf=min_samples_leaf,
verbose=1)
train_x = train_x.astype(np.float32)
train_y = train_y.astype(np.float32)
X_cudf = cudf.DataFrame.from_pandas(pd.DataFrame(train_x))
y_cudf = cudf.Series(train_y)
all_train_r2_score = []
all_valid_r2_score = []
best_model_number = 0
models = []
for k, (train_idx, valid_idx) in enumerate(kf.split(X_cudf)):
print(f'第{k}折 | train_idx={train_idx} | valid_idx={valid_idx}')
x_train, x_valid = X_cudf.iloc[train_idx], X_cudf.iloc[valid_idx]
y_train, y_valid = y_cudf.iloc[train_idx], y_cudf.iloc[valid_idx]
x_train = x_train.to_cupy()
y_train = y_train.to_cupy()
x_valid = x_valid.to_cupy()
y_valid = y_valid.to_cupy()
base_model.fit(x_train, y_train)
models.append(base_model)
y_train_pred = base_model.predict(x_train)
y_train_pred = y_train_pred.get()
y_train = y_train.get()
train_r2_score = r2_score(y_train, y_train_pred)
y_valid_pred = base_model.predict(x_valid)
y_valid_pred = y_valid_pred.get()
y_valid = y_valid.get()
valid_r2_score = r2_score(y_valid, y_valid_pred)
if all_train_r2_score and valid_r2_score > all_valid_r2_score[-1]:
best_model_number = k
all_train_r2_score.append(train_r2_score)
all_valid_r2_score.append(valid_r2_score)
print(f'第{k}折 | train_r2_score={train_r2_score:.4f} | valid_r2_score={valid_r2_score:.4f}')
mean_train_r2_score = sum(all_train_r2_score) / fold
mean_valid_r2_score = sum(all_valid_r2_score) / fold
print(f'mean_train_r2_score={mean_train_r2_score:.4f} | mean_valid_r2_score={mean_valid_r2_score:.4f}')
end_time = time.time()
elapsed_time_minute = (end_time - start_time)/60
print(f"Total running time: {elapsed_time_minute:.2f} minutes")
with open(model_path, 'wb') as file:
pickle.dump(models[best_model_number], file)
最后调到比较好的参数后,再把所有的训练集都丢进去,不要划分验证集了,保存最终模型