加载库与数据
直接给出示例
import numpy as np
import pandas as pd
import lightgbm as lgb
from lightgbm import LGBMRegressor
from sklearn.model_selection import GridSearchCV
from sklearn.feature_selection import SelectKBest, VarianceThreshold
from sklearn.pipeline import Pipeline, FeatureUnion
from sklearn.preprocessing import Imputer, PolynomialFeatures, StandardScaler, OneHotEncoder, MinMaxScaler
from sklearn.decomposition import PCA
from sklearn.ensemble import RandomForestRegressor
import os
import time
import warnings
warnings.filterwarnings("ignore")
def MAE(y, ypred): #定义损失函数
import numpy as np
return np.sum([abs(y[i]-ypred[i]) for i in range(len(y))]) / len(y)
pipeline = Pipeline(
[('imp', Imputer(missing_values='NaN', axis=0)),
('feat_select', SelectKBest()),
('lgbm', LGBMRegressor())
])
parameters = {}
parameters['imp__strategy'] = ['mean', 'median', 'most_frequent']
parameters['feat_select__k'] = [5, 10]
CV = GridSearchCV(pipeline, parameters, scoring = 'mean_absolute_error', n_jobs= 1)
CV.fit(x_train_cont, y_train)
print('Best score and parameter combination = ')
print(CV.best_score_)
print(CV.best_params_)
y_pred = CV.predict(x_valid_cont)
print('MAE on validation set: %s' % (round(MAE(y_valid, y_pred), 5)))