Ejemplo n.º 1
0
def predict_sub(model_lgb, testdex, test, subfilename):
    print_header('Submission')
    print_doing_in_task('predicting')
    lgpred = model_lgb.predict(test)
    lgsub = pd.DataFrame(lgpred, columns=["deal_probability"], index=testdex)
    lgsub['deal_probability'].clip(0.0, 1.0, inplace=True)
    print('saving submission file to', subfilename)
    lgsub.to_csv(subfilename, index=True, header=True)
    print('done')
Ejemplo n.º 2
0
def prepare_training(mat_filename, dir_feature, predictors, is_textadded):
    print_header('Load features')
    df, y, len_train, traindex, testdex = load_train_test(['item_id'], TARGET,
                                                          DEBUG)
    del len_train
    gc.collect()
    df = drop_col(df, REMOVED_LIST)

    # add features
    print_doing('add tabular features')
    for feature in predictors:
        dir_feature_file = dir_feature + feature + '.pickle'
        if not os.path.exists(dir_feature_file):
            print('can not find {}. Please check'.format(dir_feature_file))
        else:
            if feature in df:
                print('{} already added'.format(feature))
            else:
                print_doing_in_task('adding {}'.format(feature))
                df = add_feature(df, dir_feature_file)
    print_memory()

    if is_textadded:
        # add text_feature
        print_doing_in_task('add text features')
        ready_df, tfvocab = get_text_matrix(mat_filename, 'all', 2, 0)

        # stack
        print_doing_in_task('stack')
        X = hstack([
            csr_matrix(df.loc[traindex, :].values),
            ready_df[0:traindex.shape[0]]
        ])  # Sparse Matrix
        testing = hstack([
            csr_matrix(df.loc[testdex, :].values), ready_df[traindex.shape[0]:]
        ])
        print_memory()

        print_doing_in_task('prepare vocab')
        tfvocab = df.columns.tolist() + tfvocab
        for shape in [X, testing]:
            print("{} Rows and {} Cols".format(*shape.shape))
        print("Feature Names Length: ", len(tfvocab))

    else:
        tfvocab = df.columns.tolist()
        testing = hstack([csr_matrix(df.loc[testdex, :].values)])
        X = hstack([csr_matrix(df.loc[traindex, :].values)])  # Sparse Matrix

    return X, y, testing, tfvocab, df.columns.tolist(), testdex
Ejemplo n.º 3
0
def get_svdtruncated_vectorizer(todir):
    print_doing('doing svdtruncated text feature')
    filename = todir + 'text_feature_kernel.pickle'
    savename = todir + 'truncated_text_feature_kernel.pickle'
    if os.path.exists(savename):
        print('done already...')
        with open(savename, "rb") as f:
            svd_matrix, vocab = pickle.load(f)
        with open(filename, "rb") as f:
            tfid_matrix, tfvocab = pickle.load(f)
    else:
        with open(filename, "rb") as f:
            tfid_matrix, tfvocab = pickle.load(f)
        svdT = TruncatedSVD(n_components=400)
        print_doing_in_task('truncated svd')
        svd_matrix = svdT.fit_transform(tfid_matrix)
        print_doing_in_task('convert to sparse')
        svd_matrix = sparse.csr_matrix(svd_matrix, dtype=np.float32)
        vocab = []
        for i in range(np.shape(svd_matrix)[1]):
            vocab.append('lsa' + str(i + 1))
        with open(savename, "wb") as f:
            pickle.dump((svd_matrix, vocab),
                        f,
                        protocol=pickle.HIGHEST_PROTOCOL)
    print('---- before truncate')
    print(tfid_matrix.shape), print('len of feature:', len(tfvocab))
    print('---- after truncate')
    print(svd_matrix.shape), print('len of feature:', len(vocab))

    if DEBUG:
        print(tfid_matrix)
        print('\n')
        print(svd_matrix)

    del svd_matrix, vocab, tfid_matrix, tfvocab
    gc.collect()
    print_memory()
Ejemplo n.º 4
0
def train(X, y, num_leave, full_predictors, categorical, predictors,
          boosting_type, option, seed):
    if DEBUG:
        subfilename = '../sub/debug_findseed_{}_{}_{}features_num_leave{}_OPTION{}.csv'. \
                format(yearmonthdate_string,boosting_type,str(len(predictors)),num_leave,option)
        modelfilename = '../trained_models/debug_findseed_{}_{}_{}features_num_leave{}_OPTION{}.txt'. \
                format(yearmonthdate_string,boosting_type,str(len(predictors)),num_leave,option)
    else:
        subfilename = '../sub/findseed_{}_{}_{}features_num_leave{}_OPTION{}.csv'. \
                format(yearmonthdate_string,boosting_type,str(len(predictors)),num_leave,option)
        modelfilename = '../trained_models/findseed_{}_{}_{}features_num_leave{}_OPTION{}.txt'. \
                format(yearmonthdate_string,boosting_type,str(len(predictors)),num_leave,option)

    print_header("Training")
    start_time = time.time()

    print_doing_in_task('prepare dataset...')

    X, X_local_valid, y, y_local_valid = train_test_split(X,
                                                          y,
                                                          test_size=0.2,
                                                          random_state=seed)

    X_train, X_valid, y_train, y_valid = train_test_split(X,
                                                          y,
                                                          test_size=0.10,
                                                          random_state=seed)

    print('training shape: {} \n'.format(X.shape))

    print("Light Gradient Boosting Regressor")
    lgbm_params = {
        'task': 'train',
        'boosting_type': boosting_type,
        'objective': 'regression',
        'metric': 'rmse',
        'max_depth': 15,
        'feature_fraction': 0.7,
        'bagging_fraction': 0.8,
        'learning_rate': 0.1,
        'verbose': 0
    }
    print('params:', lgbm_params)

    lgtrain = lgb.Dataset(X_train,
                          y_train,
                          feature_name=full_predictors,
                          categorical_feature=categorical)
    lgvalid = lgb.Dataset(X_valid,
                          y_valid,
                          feature_name=full_predictors,
                          categorical_feature=categorical)

    if DEBUG:
        num_boost_round = 300
        early_stopping_rounds = 10
    else:
        num_boost_round = 20000
        early_stopping_rounds = 30

    lgb_clf = lgb.train(lgbm_params,
                        lgtrain,
                        num_boost_round=num_boost_round,
                        valid_sets=[lgtrain, lgvalid],
                        valid_names=['train', 'valid'],
                        early_stopping_rounds=early_stopping_rounds,
                        verbose_eval=10)
    print_memory()
    print_header("Model Report")

    runnning_time = '{0:.2f}'.format((time.time() - start_time) / 60)
    num_boost_rounds_lgb = lgb_clf.best_iteration
    print_doing_in_task('fit val')
    val_rmse = '{0:.4f}'.format(
        np.sqrt(metrics.mean_squared_error(y_valid, lgb_clf.predict(X_valid))))
    print_doing_in_task('fit train')
    train_rmse = '{0:.4f}'.format(
        np.sqrt(metrics.mean_squared_error(y_train, lgb_clf.predict(X_train))))
    print_doing_in_task('fit local val')
    local_valid_rmse = '{0:.4f}'.format(
        np.sqrt(
            metrics.mean_squared_error(y_local_valid,
                                       lgb_clf.predict(X_local_valid))))
    diff_lb = '{0:.4f}'.format(abs(float(local_valid_rmse) - 0.2300))

    print('OPTION', option)
    print('model training time:     {} mins'.format(runnning_time))
    print('seed number:             {}'.format(seed))
    print('num_boost_rounds_lgb:    {}'.format(num_boost_rounds_lgb))
    print('train rmse:              {}'.format(train_rmse))
    print('val rmse:                {}'.format(val_rmse))
    print('local valid rmse:        {}'.format(local_valid_rmse))
    print('diff comapred to lb:     {}'.format(diff_lb))

    print('saving model to', modelfilename)
    lgb_clf.save_model(modelfilename)

    seed_name = 'seed_' + str(seed)
    LOCAL_VALIDATION_RESULT['seed'][seed_name] = seed
    LOCAL_VALIDATION_RESULT['running_time'][seed_name] = runnning_time
    LOCAL_VALIDATION_RESULT['num_round'][seed_name] = num_boost_rounds_lgb
    LOCAL_VALIDATION_RESULT['train'][seed_name] = train_rmse
    LOCAL_VALIDATION_RESULT['val'][seed_name] = val_rmse
    LOCAL_VALIDATION_RESULT['local_test'][seed_name] = local_valid_rmse
    LOCAL_VALIDATION_RESULT['diff'][seed_name] = diff_lb
    return lgb_clf, subfilename
Ejemplo n.º 5
0
def train(X, y, num_leave, max_depth, full_predictors, categorical, predictors,
          boosting_type, option):

    print_header("Training")
    start_time = time.time()

    print_doing_in_task('prepare dataset...')
    X_train, X_valid, y_train, y_valid = train_test_split(X,
                                                          y,
                                                          test_size=0.10,
                                                          random_state=SEED)

    print('training shape: {} \n'.format(X.shape))

    print("Light Gradient Boosting Regressor")
    lgbm_params = {
        'task': 'train',
        'boosting_type': boosting_type,
        'objective': 'regression',
        'metric': 'rmse',
        'max_depth': max_depth,
        'num_leave': num_leave,
        'feature_fraction': 0.8,
        'bagging_fraction': 0.8,
        'learning_rate': 0.1,
        'lambda_l1': 10,
        'max_bin': 512,
        'verbose': -1
    }
    print('params:', lgbm_params)

    lgtrain = lgb.Dataset(X_train,
                          y_train,
                          feature_name=full_predictors,
                          categorical_feature=categorical)
    lgvalid = lgb.Dataset(X_valid,
                          y_valid,
                          feature_name=full_predictors,
                          categorical_feature=categorical)

    if DEBUG:
        num_boost_round = 300
        early_stopping_rounds = 10
    else:
        num_boost_round = 20000
        early_stopping_rounds = 100

    lgb_clf = lgb.train(lgbm_params,
                        lgtrain,
                        num_boost_round=num_boost_round,
                        valid_sets=[lgtrain, lgvalid],
                        valid_names=['train', 'valid'],
                        early_stopping_rounds=early_stopping_rounds,
                        verbose_eval=10)

    print_memory()

    print_header("Model Report")

    runnning_time = '{0:.2f}'.format((time.time() - start_time) / 60)
    num_boost_rounds_lgb = lgb_clf.best_iteration
    print_doing_in_task('fit val')
    val_rmse = '{0:.4f}'.format(
        np.sqrt(metrics.mean_squared_error(y_valid, lgb_clf.predict(X_valid))))
    print_doing_in_task('fit train')
    train_rmse = '{0:.4f}'.format(
        np.sqrt(metrics.mean_squared_error(y_train, lgb_clf.predict(X_train))))
    print_header("Model Report")

    print('boosting_type {}, num_leave {}, max_depth {}'.format(
        boosting_type, num_leave, max_depth))
    print('model training time:     {0:.2f} mins'.format(
        (time.time() - start_time) / 60))
    print('num_boost_rounds_lgb:    {}'.format(lgb_clf.best_iteration))
    print('best rmse:               {0:.4f}'.format(
        np.sqrt(metrics.mean_squared_error(y_valid,
                                           lgb_clf.predict(X_valid)))))

    model = '{}_{}_{}'.format(boosting_type, num_leave, max_depth)
    LOCAL_TUNE_RESULT['running_time'][model] = runnning_time
    LOCAL_TUNE_RESULT['num_round'][model] = num_boost_rounds_lgb
    LOCAL_TUNE_RESULT['train'][model] = train_rmse
    LOCAL_TUNE_RESULT['val'][model] = val_rmse