<jupyter_start><jupyter_text>Random Forest<jupyter_code>train_df = premodel_formating(train_df, split=False)
X = train_df.drop(["Real.Spots", "any_spot"], axis=1)
y = train_df["any_spot"]
def cv_k(X, y, k, model):
    kf = KFold(n_splits = k)
    kf.get_n_splits(X)
    # f0.5 score
    f_score = 0
    # precision
    p_score = 0
    # recall
    r_score = 0
    for train_index, val_index in kf.split(X):
        X_train, X_val = X.ix[train_index], X.ix[val_index]    
        y_train, y_val = y.ix[train_index], y.ix[val_index]
        model.fit(X_train, y_train)
        preds = model.predict(X_val)
        f_score += fbeta_score(y_val, preds, beta=0.5)
        p_score += precision_score(y_val, preds)
        r_score += recall_score(y_val, preds)
    return [f_score/(k*1.0), p_score/(k*1.0), r_score/(k*1.0)]<jupyter_output><empty_output><jupyter_text>Parameter Tuning Model1: n_estimators=16,min_samples_leaf=5,max_features=0.9, max_depth=5<jupyter_code>import warnings; warnings.simplefilter('ignore')
# Tune three parameters: n_estimators, min_samepls_leaf, max_features and max_depth
numOfestimators = np.arange(18) + 1
numOfleafs = np.arange(15) + 1
numOffeatures = np.arange(0.1, 1.1, 0.2)
depths = np.arange(0, 7) + 1
best_result = []
for numOfestimator in numOfestimators:
    for numOfleaf in numOfleafs:
        for numOffeature in numOffeatures:  
            for depth in depths:
                result = [numOfestimator, numOfleaf, numOffeature, depth]
                m = RandomForestClassifier(n_jobs=-1, n_estimators=numOfestimator,\
                                    min_samples_leaf=numOfleaf,\
                                    max_features=numOffeature, max_depth = depth)
                score = cv_k(X, y, 3, m)
                result.append(score[0])
                result.append(score[1])
                if len(best_result) == 0: best_result = result
                elif best_result[5] < result[5]: 
                    print(result)
                    best_result = result<jupyter_output>[1, 1, 0.10000000000000001, 2, 0.27149084568439408, 0.12387387387387387]
[1, 1, 0.10000000000000001, 3, 0.32419441406686489, 0.25927748413207419]
[1, 1, 0.10000000000000001, 4, 0.52261076302863974, 0.3742517046277965]
[1, 1, 0.10000000000000001, 7, 0.61086273561045823, 0.43284393423910411]
[1, 1, 0.30000000000000004, 6, 0.65389562396214285, 0.44080495300007488]
[1, 1, 0.30000000000000004, 7, 0.63895907654441741, 0.47322742493394099]
[1, 1, 0.70000000000000007, 5, 0.63164475117104446, 0.49022530596258634]
[1, 1, 0.90000000000000013, 5, 0.61870360596812823, 0.514423454115715]
[1, 5, 0.70000000000000007, 5, 0.60975509566251807, 0.53570588679165698]
[1, 10, 0.30000000000000004, 5, 0.61394678076784848, 0.53801541192608726]
[1, 11, 0.90000000000000013, 6, 0.6283385945029113, 0.56998286051419556]
[4, 11, 0.30000000000000004, 7, 0.68731190609968384, 0.5924782514255732]<jupyter_text>RF without mean encoding<jupyter_code># 4, 11, 0.30000000000000004, 7,
# leaderboard score 0.57071
m = RandomForestClassifier(n_jobs=-1, n_estimators=4,min_samples_leaf=11,max_features=0.3, max_depth = 7)
m.fit(X, y)
cv_k(X, y, 3, m)<jupyter_output><empty_output><jupyter_text>Prep for submission<jupyter_code>trn = premodel_formating(train_df, split=False)
tst = premodel_formating(test_df, split=False, test=True)
trn, tst = mean_target_enc(trn, tst)
print(trn.shape, tst.shape)
trn.shape, tst.shape
X_tr, y_tr = get_XY(trn)
X_tr.info()
print(X_tr.shape, y_tr.shape)
rfc = RandomForestClassifier(n_estimators= 10, max_depth=10)
m = rfc.fit(X_tr, y_tr)
xgc = XGBClassifier(max_depth = 5, n_estimators=100)
m = xgc.fit(X_tr, y_tr)
tst_pred = m.predict(tst)
to_sub = pd.DataFrame({'id': range(1, len(tst_pred)+1), 'any_spot': tst_pred})
to_sub[['id','any_spot']].to_csv('submissions/mean_enc_sub.csv', index=False)
to_sub.shape<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12887396694214875, 'fraction_numerical': 0.1991219008264463, 'mean_word_length': 4.082677165354331, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 13, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '101260', 'license': 'MIT', 'n_tokens_mistral': 2112, 'n_tokens_neox': 1586, 'n_words': 274, 'path': 'modeling/cheng_rf_model.ipynb', 'repo_name': 'timdavidlee/sf-parking-ml', 'size': '3872'}