<jupyter_start><jupyter_text>HyperParameter Searchinghttps://towardsdatascience.com/hyperparameter-tuning-the-random-forest-in-python-using-scikit-learn-28d2aa77dd74<jupyter_code>import pandas as pd
import numpy as np
from sklearn.model_selection import GridSearchCV
from sklearn.ensemble import RandomForestClassifier
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import cross_validate
from sklearn.metrics import confusion_matrix

url = 'https://web.stanford.edu/class/archive/cs/cs109/cs109.1166/stuff/titanic.csv'
data = pd.read_csv(url)
data.head(10)

# setup method for missing data using a median imputer for age and fair
numeric_features = ['Age', 'Fare', 'Siblings/Spouses Aboard', 'Parents/Children Aboard']
numeric_transformer = Pipeline(steps=[
    ('imputer', SimpleImputer(strategy='median')),
     ('scaler', StandardScaler())])
# setup one hot enoding for catagorical features
categorical_features = [ 'Sex', 'Pclass']
categorical_transformer = Pipeline(steps=[
    ('imputer', SimpleImputer(strategy='constant', fill_value='missing')),
    ('onehot', OneHotEncoder(handle_unknown='ignore'))])

# combine numeric and catagorical transformation into one column transformer object
preprocessor = ColumnTransformer(
    transformers=[
        ('num', numeric_transformer, numeric_features),
        ('cat', categorical_transformer, categorical_features)])

# Append classifier to preprocessing pipeline.
# Now we have a full prediction pipeline.
pipe = Pipeline(steps=[('preprocessor', preprocessor)])

# drop rows where survived is unknown
y = data['Survived']
X = pipe.fit_transform(data)

# normalizes the feature names
feature_names = numeric_features +  pipe.named_steps['preprocessor'].named_transformers_['cat'].named_steps['onehot'].get_feature_names().tolist()
feature_names = list(map(lambda x: x.replace('x0',categorical_features[0]), feature_names))
feature_names = list(map(lambda x: x.replace('x1',categorical_features[1]), feature_names))<jupyter_output><empty_output><jupyter_text>Create a baseline model using CrossValidation to see what gain is made from Random FroA single Decision Tree Model is used here as a baseline<jupyter_code>import sklearn
# show all the difference metrix tp 
sklearn.metrics.SCORERS.keys()
# base line model with one Tree
from sklearn.model_selection import cross_val_predict
model = DecisionTreeClassifier()
scoring = 'accuracy'
# estimates accuracy
scores = cross_validate(model, X, y, scoring=[scoring] , cv=10)
best_score = np.max(scores['test_' + scoring])
print('Random Tree Best Classifier')
print('best {0} score : {1}'.format(scoring, best_score))
# create predictions 
y_pred = cross_val_predict(model, X, y, cv = 10)
print('true', confusion_matrix(y, y_pred), '\n predicted')
### Is randpom forest really better ?

from sklearn.model_selection import cross_val_predict
model = RandomForestClassifier(n_estimators=10)
scoring = 'accuracy'
# estimates accuracy
scores = cross_validate(model, X, y, scoring=[scoring] , cv=5)
best_score = np.max(scores['test_' + scoring])
print('Random Tree Best Classifier')
print('best {0} score : {1}'.format(scoring, best_score))
# create predictions 
y_pred = cross_val_predict(model, X, y, cv = 5)
print('true', confusion_matrix(y, y_pred), '\n predicted')
# Number of trees in random forest
n_estimators  = np.arange(10,30, 10)
# Number of features to consider at every split
# Maximum number of levels in tree
max_depth = [3,4,5]
# Minimum number of samples required to split a node
min_samples_split = np.arange(2, 6, 2)
# Minimum number of samples required at each leaf node
min_samples_leaf = np.arange(2, 6,2)
# Method of selecting samples for training each tree
param_grid= {'n_estimators': n_estimators,
               'max_depth': max_depth,
               'min_samples_split': min_samples_split,
               'min_samples_leaf': min_samples_leaf}
print(param_grid)

model = RandomForestClassifier(n_estimators=10)
model = GridSearchCV(model, param_grid = param_grid,cv = 3,scoring=scoring, n_jobs = -1,  refit=True)

model = model.fit(X, y)

print('Random Tree Best Classifier on Grid Cearh ')
from sklearn.metrics import accuracy_score
# create predictions 
y_pred = model.predict(X)
print('true', confusion_matrix(y, y_pred), '\n predicted')
print('acc: {} '.format(accuracy_score(y, y_pred)))
model.pre<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10870516185476815, 'fraction_numerical': 0.011811023622047244, 'mean_word_length': 6.340288924558587, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '360816', 'license': 'MIT', 'n_tokens_mistral': 1460, 'n_tokens_neox': 1409, 'n_words': 385, 'path': 'MachineLearning/GridSearch and Parameter Tuning.ipynb', 'repo_name': 'mdavis29/DataScience101', 'size': '4572'}