<jupyter_start><jupyter_text>Train Classifier<jupyter_code># cross validation method to train random forest classifier
def random_forest_classifier_cv(feature_train, label_train, n_estimator):
    skf = StratifiedKFold(n_splits=5, shuffle=True)
    scores = []
    for train_index, val_index in skf.split(feature_train, label_train):
        X_cv_train, X_cv_val = feature_train[train_index], feature_train[val_index]
        y_cv_train, y_cv_val = label_train[train_index], label_train[val_index]
        rf = RandomForestClassifier(n_estimators=n_estimator).fit(X_cv_train, y_cv_train)
        score = rf.score(X_cv_val, y_cv_val)
        scores.append(score)
    return scores
n_estimators_list = [i*100 for i in range(1, 10)]
opt_n_estimators = -1
best_score = 0
for n_estimator in n_estimators_list:
    print(f'N_estimator: {n_estimator}')
    scores = random_forest_classifier_cv(X_train, y_train, n_estimator)
    avg_score = np.mean(scores)
    print(f'avg_score: {avg_score}')
    if avg_score > best_score:
        opt_n_estimators = n_estimator
        best_score = avg_score
print(f'Best score: {best_score}. n_estimators: {opt_n_estimators}')<jupyter_output>N_estimator: 100
avg_score: 0.8249890151277384
N_estimator: 200
avg_score: 0.8293955181721172
N_estimator: 300
avg_score: 0.8338773460548616
N_estimator: 400
avg_score: 0.8383968363567886
N_estimator: 500
avg_score: 0.8372920720607621
N_estimator: 600
avg_score: 0.8338899001945892
N_estimator: 700
avg_score: 0.8372732408511705
N_estimator: 800
avg_score: 0.8282844768062269
N_estimator: 900
avg_score: 0.835069989328981
Best score: 0.8383968363567886. n_estimators: 400<jupyter_text>Final Model<jupyter_code>rf = RandomForestClassifier(n_estimators=opt_n_estimators).fit(X_train, y_train)
rf.score(X_train, y_train)<jupyter_output><empty_output><jupyter_text>Prediction<jupyter_code>X_test = df_test.drop('Survived', axis=1)
X_test['Fare'].fillna(0, inplace=True)
X_test = scalar.transform(X_test)
y_pred = rf.predict(X_test)
y_pred = y_pred.astype(int)
df_sub = pd.DataFrame({'PassengerId':df_test_passengerId.values, 'Survived': y_pred})
df_sub.head()
df_sub.to_csv('submission.csv',index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1004089050431622, 'fraction_numerical': 0.09541117673784644, 'mean_word_length': 6.808510638297872, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '39275', 'license': 'MIT', 'n_tokens_mistral': 1046, 'n_tokens_neox': 903, 'n_words': 139, 'path': 'titanic-randomforestclassifier.ipynb', 'repo_name': 'lit26/TitanicKaggleCompetition', 'size': '2201'}