<jupyter_start><jupyter_text>Data Cleaning<jupyter_code>numeric_cols = preprocess.dtype_select(dtypes=np.number)
cat_cols = preprocess.dtype_select(dtypes='object')
#Impute Numeric Values
X[numeric_cols] = preprocess.impute(numeric_cols=numeric_cols)
#Scale Numeric Values
X[numeric_cols] = preprocess.minMax(numeric_cols=numeric_cols)
X.describe()
#Replacing NaN With Unknown So As To Avoid The Encoding To Throw An Error
X[cat_cols] = X[cat_cols].fillna('Unknown')
X[cat_cols].isna().sum()
#Encode Categorical Column Values
X[cat_cols] = preprocess.one_hot(cat_cols=cat_cols)
for col in cat_cols:
    X.pop(col)
X
#Check Y For NaN Values
print(Y.isna().sum())
# Change NaN To Most Common Category
Y = Y.replace(np.nan, 'No')
#ReCheck Y For NaN Values
Y.isna().sum()<jupyter_output>RainTomorrow    0
dtype: int64<jupyter_text>Data Exploration/Data Analysis<jupyter_code>X.describe()
exploration_copy = X.copy()
exploration_copy['RainTomorrow'] = df['RainTomorrow']
#Mapping Y Values
raintom_vals = {'No': 0, 'Yes': 1}
exploration_copy['RainTomorrow'] = df.RainTomorrow.map(raintom_vals)
Y['RainTomorrow'] = df.RainTomorrow.map(raintom_vals)
exploration_copy.plot(x='RainToday_Yes',y='RainTomorrow',kind='hist')
X<jupyter_output><empty_output><jupyter_text>Train/Test Split<jupyter_code>year = pd.to_datetime(df.Date).dt.year

train_X = exploration_copy[year<2015]
val_X = exploration_copy[year==2015]
test_X = exploration_copy[year>2015]
train_Y = train_X.pop('RainTomorrow')
val_Y = val_X.pop('RainTomorrow')
test_Y = test_X.pop('RainTomorrow')<jupyter_output><empty_output><jupyter_text>Model<jupyter_code>from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import plot_tree
model = RandomForestClassifier(n_estimators=200, n_jobs=-1, random_state=42)
%%time
model.fit(train_X, train_Y)
print("Train Acc:", model.score(train_X, train_Y), "\nVal Acc:", model.score(val_X, val_Y))
importance_df = pd.DataFrame({
    'feature': train_X.columns,
    'importance': model.feature_importances_
}).sort_values('importance', ascending=False)

importance_df.head(10)
import seaborn as sns
sns.barplot(data=importance_df.head(10), x='importance', y='feature');<jupyter_output><empty_output><jupyter_text>HyperParameters<jupyter_code>from sklearn.metrics import accuracy_score
def test_params(**params):
    model = RandomForestClassifier(n_jobs=-1, random_state=42, **params)
    model.fit(train_X, train_Y)
    
    train_preds = model.predict(train_X)
    val_preds = model.predict(val_X)
    test_preds = model.predict(test_X)

    train_acc = accuracy_score(train_Y, train_preds)
    val_acc = accuracy_score(val_Y, val_preds)
    test_acc = accuracy_score(test_Y, test_preds)
    return {f"Params": params, "Train Acc":train_acc, "Val Acc":val_acc, "Test Acc":test_acc, "Avg Acc":(val_acc+test_acc)/2}<jupyter_output><empty_output><jupyter_text>`n_estimators`<jupyter_code>%%time
estimators_df = pd.DataFrame([test_params(n_estimators=ne) for ne in range(1,101)])
estimators_df.sort_values(by=['Avg Acc'], ascending=False)<jupyter_output>CPU times: user 22min 56s, sys: 12.8 s, total: 23min 9s
Wall time: 3min 35s<jupyter_text>`max_depth`<jupyter_code>%%time
depth_df = pd.DataFrame([test_params(n_estimators=87, max_depth=md) for md in range(1,51)])
depth_df.sort_values(by=['Avg Acc'], ascending=False)<jupyter_output>CPU times: user 14min 53s, sys: 8.39 s, total: 15min 1s
Wall time: 2min 9s<jupyter_text>`max_leaf_nodes`<jupyter_code>%%time
accuracies_df = pd.DataFrame([test_params(n_estimators=87, max_leaf_nodes=2**num) for num in range(1,21)])
accuracies_df<jupyter_output>CPU times: user 5min 34s, sys: 3 s, total: 5min 37s
Wall time: 47.6 s
{'fraction_non_alphanumeric': 0.13315143246930422, 'fraction_numerical': 0.022100954979536153, 'mean_word_length': 8.165, 'pattern_counts': {'":': 5, '<': 30, '<?xml version=': 0, '>': 30, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '533002', 'license': 'MIT', 'n_tokens_mistral': 1492, 'n_tokens_neox': 1390, 'n_words': 247, 'path': 'Code/random_forest.ipynb', 'repo_name': 'Ansh3101/MachineLearning', 'size': '3665'}