Created
September 9, 2022 13:31
-
-
Save callmemaze/95020ce4fa675f745b9a0ef137c80546 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import joblib\n", | |
"import pandas as pd\n", | |
"from sklearn.ensemble import RandomForestClassifier\n", | |
"from sklearn.model_selection import GridSearchCV\n", | |
"import warnings\n", | |
"warnings.filterwarnings('ignore', category=FutureWarning)\n", | |
"warnings.filterwarnings('ignore', category=DeprecationWarning)\n", | |
"\n", | |
"tr_features = pd.read_csv('../../../train_features.csv')\n", | |
"tr_labels = pd.read_csv('../../../train_labels.csv', header=None)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Hyperparameter tuning\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def print_results(results):\n", | |
" print('BEST PARAMS: {}\\n'.format(results.best_params_))\n", | |
"\n", | |
" means = results.cv_results_['mean_test_score']\n", | |
" stds = results.cv_results_['std_test_score']\n", | |
" for mean, std, params in zip(means, stds, results.cv_results_['params']):\n", | |
" print('{} (+/-{}) for {}'.format(round(mean, 3), round(std * 2, 3), params))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"BEST PARAMS: {'max_depth': 4, 'n_estimators': 50}\n", | |
"\n", | |
"0.796 (+/-0.112) for {'max_depth': 2, 'n_estimators': 5}\n", | |
"0.796 (+/-0.106) for {'max_depth': 2, 'n_estimators': 50}\n", | |
"0.798 (+/-0.124) for {'max_depth': 2, 'n_estimators': 250}\n", | |
"0.824 (+/-0.106) for {'max_depth': 4, 'n_estimators': 5}\n", | |
"0.828 (+/-0.091) for {'max_depth': 4, 'n_estimators': 50}\n", | |
"0.824 (+/-0.109) for {'max_depth': 4, 'n_estimators': 250}\n", | |
"0.807 (+/-0.087) for {'max_depth': 8, 'n_estimators': 5}\n", | |
"0.826 (+/-0.073) for {'max_depth': 8, 'n_estimators': 50}\n", | |
"0.822 (+/-0.063) for {'max_depth': 8, 'n_estimators': 250}\n", | |
"0.811 (+/-0.041) for {'max_depth': 16, 'n_estimators': 5}\n", | |
"0.816 (+/-0.04) for {'max_depth': 16, 'n_estimators': 50}\n", | |
"0.813 (+/-0.023) for {'max_depth': 16, 'n_estimators': 250}\n", | |
"0.801 (+/-0.041) for {'max_depth': 32, 'n_estimators': 5}\n", | |
"0.8 (+/-0.034) for {'max_depth': 32, 'n_estimators': 50}\n", | |
"0.813 (+/-0.032) for {'max_depth': 32, 'n_estimators': 250}\n", | |
"0.801 (+/-0.051) for {'max_depth': None, 'n_estimators': 5}\n", | |
"0.809 (+/-0.04) for {'max_depth': None, 'n_estimators': 50}\n", | |
"0.811 (+/-0.03) for {'max_depth': None, 'n_estimators': 250}\n" | |
] | |
} | |
], | |
"source": [ | |
"rf = RandomForestClassifier()\n", | |
"parameters = {\n", | |
" 'n_estimators': [5, 50, 250],\n", | |
" 'max_depth': [2, 4, 8, 16, 32, None]\n", | |
"}\n", | |
"\n", | |
"cv = GridSearchCV(rf, parameters, cv=5)\n", | |
"cv.fit(tr_features, tr_labels.values.ravel())\n", | |
"\n", | |
"print_results(cv)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Write out pickled model" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['../../../RF_model.pkl']" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"joblib.dump(cv.best_estimator_, '../../../RF_model.pkl')" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment