当前位置: 首页>>代码示例>>Python>>正文


Python RandomForestRegressor.warm_start方法代码示例

本文整理汇总了Python中sklearn.ensemble.RandomForestRegressor.warm_start方法的典型用法代码示例。如果您正苦于以下问题:Python RandomForestRegressor.warm_start方法的具体用法?Python RandomForestRegressor.warm_start怎么用?Python RandomForestRegressor.warm_start使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sklearn.ensemble.RandomForestRegressor的用法示例。


在下文中一共展示了RandomForestRegressor.warm_start方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: train_and_predict

# 需要导入模块: from sklearn.ensemble import RandomForestRegressor [as 别名]
# 或者: from sklearn.ensemble.RandomForestRegressor import warm_start [as 别名]
def train_and_predict(train_data_file, test_data_file, target_col, test_pred_file,
                      test_data_file2, test_pred_file2,
                      model_type, model_file, fit_args, test_metric, na_fill_value,
                      silent, skip_mapping, load_model, train_filter, metric_type, load_type,
                      bootstrap, bootstrap_seed, weight_col):
    start = timeit.default_timer()

    train_x = load_pd_df(train_data_file)

    len_train_before = len(train_x)
    train_x = data_filter(train_x, train_filter)
    if not silent:
        print "Train has %d instances (was %d before filtering)" % (len(train_x), len_train_before)

    mappings = None if skip_mapping else dict()
    if mappings is not None:
        data_all = train_x.append(load_pd_df(test_data_file))
        if test_data_file2 is not None:
            data_all = data_all.append(load_pd_df(test_data_file2))
        if not silent:
            print "Mapping unkown and category values..."
        for col in train_x.columns:
            if col not in ['target_col']:
                if data_all[col].dtype == np.dtype('object'):
                    s = np.unique(data_all[col].fillna(na_fill_value).values)
                    mappings[col] = pd.Series([x[0] for x in enumerate(s)], index=s)
                    train_x[col] = train_x[col].map(mappings[col]).fillna(na_fill_value)
                else:
                    train_x[col] = train_x[col].fillna(na_fill_value)
        del data_all
    train_y = train_x[target_col]
    del train_x[target_col]

    extra_fit_args = dict()
    if weight_col is not None:
        extra_fit_args['sample_weight'] = train_x[weight_col].values
        del train_x[weight_col]

    if 0 < bootstrap < 1.0:
        if bootstrap_seed is not None:
            if not silent:
                print "Setting bootstrap seed to %d" % bootstrap_seed
            np.random.seed(bootstrap_seed)
            random.seed(bootstrap_seed)
        bootstrap_len = int(math.floor(bootstrap * len(train_x)))
        bootstrap_ix = random.sample(range(len(train_x)), bootstrap_len)
        train_x = train_x.iloc[bootstrap_ix]
        train_x.reset_index()
        train_y = train_y.iloc[bootstrap_ix]
        train_y.reset_index()

    x_cols = train_x.columns
    feat_importance_fun = lambda (fitted_model): fitted_model.feature_importances_
    predict = lambda (fitted_model, pred_x): fitted_model.predict(pred_x)
    staged_predict = lambda (fitted_model, pred_x): [predict((fitted_model, pred_x))]

    model = None
    if load_model and os.path.exists(model_file):
        if not silent:
            print "Loading model %s" % model_file
        model = load_model_bin(model_file=model_file)

    if model_type == "RandomForestRegressor":
        if model is None:
            model = RandomForestRegressor(**fit_args)
            model.fit(X=train_x, y=train_y, **extra_fit_args)
        predict = lambda (fitted_model, pred_x): continuous_predict(model=fitted_model, x=pred_x)

    elif model_type == "RandomForestClassifier":
        if model is None:
            model = RandomForestClassifier(**fit_args)
            model.fit(X=train_x, y=train_y, **extra_fit_args)
        predict = lambda (fitted_model, pred_x): pred_proba(model=fitted_model, x=pred_x)
        staged_predict = lambda (fitted_model, pred_x): [predict((fitted_model, pred_x))]

    elif model_type == "ExtraTreesRegressor":
        if model is None:
            model = ExtraTreesRegressor(**fit_args)
            model.fit(X=train_x, y=train_y, **extra_fit_args)
        predict = lambda (fitted_model, pred_x): continuous_predict(model=fitted_model, x=pred_x)

    elif model_type == "ExtraTreesClassifier":
        if model is None:
            model = ExtraTreesClassifier(**fit_args)
            model.fit(X=train_x, y=train_y, **extra_fit_args)
        predict = lambda (fitted_model, pred_x): pred_proba(model=fitted_model, x=pred_x)
        staged_predict = lambda (fitted_model, pred_x): [predict((fitted_model, pred_x))]

    elif model_type == "GradientBoostingRegressor":
        if model is None:
            model = GradientBoostingRegressor(**fit_args)
            model.fit(X=train_x, y=train_y, **extra_fit_args)
        elif load_type == "fit_more":
            model.warm_start = True
            model.n_estimators += fit_args['n_estimators']
            model.fit(X=train_x, y=train_y)
        predict = lambda (fitted_model, pred_x): continuous_predict(model=fitted_model, x=pred_x)
        staged_predict = lambda (fitted_model, pred_x): staged_pred_continuous(model=fitted_model, x=pred_x)
        if load_type == "pred_at" and fit_args['n_estimators'] < model.n_estimators:
            if not silent:
#.........这里部分代码省略.........
开发者ID:nancyzq11,项目名称:west_nile_virus_2015,代码行数:103,代码来源:sci_learn_train.py


注:本文中的sklearn.ensemble.RandomForestRegressor.warm_start方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。