Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save BadreeshShetty/31cc189f321ae859ec9740f89048b143 to your computer and use it in GitHub Desktop.
Save BadreeshShetty/31cc189f321ae859ec9740f89048b143 to your computer and use it in GitHub Desktop.
NLP ML (Built-In)
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## For CountVectorizer "
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\Dell\\Anaconda3\\lib\\site-packages\\sklearn\\utils\\deprecation.py:122: FutureWarning: You are accessing a training score ('split0_train_score'), which will not be available by default any more in 0.21. If you need training scores, please set return_train_score=True\n",
" warnings.warn(*warn_args, **warn_kwargs)\n",
"C:\\Users\\Dell\\Anaconda3\\lib\\site-packages\\sklearn\\utils\\deprecation.py:122: FutureWarning: You are accessing a training score ('split1_train_score'), which will not be available by default any more in 0.21. If you need training scores, please set return_train_score=True\n",
" warnings.warn(*warn_args, **warn_kwargs)\n",
"C:\\Users\\Dell\\Anaconda3\\lib\\site-packages\\sklearn\\utils\\deprecation.py:122: FutureWarning: You are accessing a training score ('split2_train_score'), which will not be available by default any more in 0.21. If you need training scores, please set return_train_score=True\n",
" warnings.warn(*warn_args, **warn_kwargs)\n",
"C:\\Users\\Dell\\Anaconda3\\lib\\site-packages\\sklearn\\utils\\deprecation.py:122: FutureWarning: You are accessing a training score ('split3_train_score'), which will not be available by default any more in 0.21. If you need training scores, please set return_train_score=True\n",
" warnings.warn(*warn_args, **warn_kwargs)\n",
"C:\\Users\\Dell\\Anaconda3\\lib\\site-packages\\sklearn\\utils\\deprecation.py:122: FutureWarning: You are accessing a training score ('split4_train_score'), which will not be available by default any more in 0.21. If you need training scores, please set return_train_score=True\n",
" warnings.warn(*warn_args, **warn_kwargs)\n",
"C:\\Users\\Dell\\Anaconda3\\lib\\site-packages\\sklearn\\utils\\deprecation.py:122: FutureWarning: You are accessing a training score ('mean_train_score'), which will not be available by default any more in 0.21. If you need training scores, please set return_train_score=True\n",
" warnings.warn(*warn_args, **warn_kwargs)\n",
"C:\\Users\\Dell\\Anaconda3\\lib\\site-packages\\sklearn\\utils\\deprecation.py:122: FutureWarning: You are accessing a training score ('std_train_score'), which will not be available by default any more in 0.21. If you need training scores, please set return_train_score=True\n",
" warnings.warn(*warn_args, **warn_kwargs)\n"
]
},
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>mean_fit_time</th>\n",
" <th>std_fit_time</th>\n",
" <th>mean_score_time</th>\n",
" <th>std_score_time</th>\n",
" <th>param_max_depth</th>\n",
" <th>param_n_estimators</th>\n",
" <th>params</th>\n",
" <th>split0_test_score</th>\n",
" <th>split1_test_score</th>\n",
" <th>split2_test_score</th>\n",
" <th>...</th>\n",
" <th>mean_test_score</th>\n",
" <th>std_test_score</th>\n",
" <th>rank_test_score</th>\n",
" <th>split0_train_score</th>\n",
" <th>split1_train_score</th>\n",
" <th>split2_train_score</th>\n",
" <th>split3_train_score</th>\n",
" <th>split4_train_score</th>\n",
" <th>mean_train_score</th>\n",
" <th>std_train_score</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>38.373489</td>\n",
" <td>0.509486</td>\n",
" <td>0.522701</td>\n",
" <td>0.057746</td>\n",
" <td>90</td>\n",
" <td>150</td>\n",
" <td>{'max_depth': 90, 'n_estimators': 150}</td>\n",
" <td>0.978475</td>\n",
" <td>0.976640</td>\n",
" <td>0.973944</td>\n",
" <td>...</td>\n",
" <td>0.973774</td>\n",
" <td>0.003754</td>\n",
" <td>1</td>\n",
" <td>0.998877</td>\n",
" <td>0.999326</td>\n",
" <td>0.998877</td>\n",
" <td>0.999326</td>\n",
" <td>0.998877</td>\n",
" <td>0.999057</td>\n",
" <td>0.000220</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>55.351193</td>\n",
" <td>8.387999</td>\n",
" <td>0.621246</td>\n",
" <td>0.184220</td>\n",
" <td>None</td>\n",
" <td>300</td>\n",
" <td>{'max_depth': None, 'n_estimators': 300}</td>\n",
" <td>0.977578</td>\n",
" <td>0.973046</td>\n",
" <td>0.973944</td>\n",
" <td>...</td>\n",
" <td>0.972696</td>\n",
" <td>0.003257</td>\n",
" <td>2</td>\n",
" <td>1.000000</td>\n",
" <td>1.000000</td>\n",
" <td>1.000000</td>\n",
" <td>1.000000</td>\n",
" <td>1.000000</td>\n",
" <td>1.000000</td>\n",
" <td>0.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>63.410393</td>\n",
" <td>1.602087</td>\n",
" <td>0.739778</td>\n",
" <td>0.078957</td>\n",
" <td>90</td>\n",
" <td>300</td>\n",
" <td>{'max_depth': 90, 'n_estimators': 300}</td>\n",
" <td>0.976682</td>\n",
" <td>0.975741</td>\n",
" <td>0.973944</td>\n",
" <td>...</td>\n",
" <td>0.972517</td>\n",
" <td>0.003718</td>\n",
" <td>3</td>\n",
" <td>0.999102</td>\n",
" <td>0.998877</td>\n",
" <td>0.998877</td>\n",
" <td>0.999326</td>\n",
" <td>0.999326</td>\n",
" <td>0.999102</td>\n",
" <td>0.000201</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>34.176885</td>\n",
" <td>1.407967</td>\n",
" <td>0.505711</td>\n",
" <td>0.076573</td>\n",
" <td>None</td>\n",
" <td>150</td>\n",
" <td>{'max_depth': None, 'n_estimators': 150}</td>\n",
" <td>0.977578</td>\n",
" <td>0.973046</td>\n",
" <td>0.974843</td>\n",
" <td>...</td>\n",
" <td>0.972337</td>\n",
" <td>0.003840</td>\n",
" <td>4</td>\n",
" <td>1.000000</td>\n",
" <td>1.000000</td>\n",
" <td>1.000000</td>\n",
" <td>1.000000</td>\n",
" <td>1.000000</td>\n",
" <td>1.000000</td>\n",
" <td>0.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>5.971492</td>\n",
" <td>0.761027</td>\n",
" <td>0.270247</td>\n",
" <td>0.055440</td>\n",
" <td>90</td>\n",
" <td>10</td>\n",
" <td>{'max_depth': 90, 'n_estimators': 10}</td>\n",
" <td>0.973991</td>\n",
" <td>0.973944</td>\n",
" <td>0.968553</td>\n",
" <td>...</td>\n",
" <td>0.971259</td>\n",
" <td>0.003417</td>\n",
" <td>5</td>\n",
" <td>0.998428</td>\n",
" <td>0.997081</td>\n",
" <td>0.998204</td>\n",
" <td>0.997306</td>\n",
" <td>0.997081</td>\n",
" <td>0.997620</td>\n",
" <td>0.000578</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>5 rows × 22 columns</p>\n",
"</div>"
],
"text/plain": [
" mean_fit_time std_fit_time mean_score_time std_score_time \\\n",
"7 38.373489 0.509486 0.522701 0.057746 \n",
"11 55.351193 8.387999 0.621246 0.184220 \n",
"8 63.410393 1.602087 0.739778 0.078957 \n",
"10 34.176885 1.407967 0.505711 0.076573 \n",
"6 5.971492 0.761027 0.270247 0.055440 \n",
"\n",
" param_max_depth param_n_estimators \\\n",
"7 90 150 \n",
"11 None 300 \n",
"8 90 300 \n",
"10 None 150 \n",
"6 90 10 \n",
"\n",
" params split0_test_score \\\n",
"7 {'max_depth': 90, 'n_estimators': 150} 0.978475 \n",
"11 {'max_depth': None, 'n_estimators': 300} 0.977578 \n",
"8 {'max_depth': 90, 'n_estimators': 300} 0.976682 \n",
"10 {'max_depth': None, 'n_estimators': 150} 0.977578 \n",
"6 {'max_depth': 90, 'n_estimators': 10} 0.973991 \n",
"\n",
" split1_test_score split2_test_score ... mean_test_score \\\n",
"7 0.976640 0.973944 ... 0.973774 \n",
"11 0.973046 0.973944 ... 0.972696 \n",
"8 0.975741 0.973944 ... 0.972517 \n",
"10 0.973046 0.974843 ... 0.972337 \n",
"6 0.973944 0.968553 ... 0.971259 \n",
"\n",
" std_test_score rank_test_score split0_train_score split1_train_score \\\n",
"7 0.003754 1 0.998877 0.999326 \n",
"11 0.003257 2 1.000000 1.000000 \n",
"8 0.003718 3 0.999102 0.998877 \n",
"10 0.003840 4 1.000000 1.000000 \n",
"6 0.003417 5 0.998428 0.997081 \n",
"\n",
" split2_train_score split3_train_score split4_train_score \\\n",
"7 0.998877 0.999326 0.998877 \n",
"11 1.000000 1.000000 1.000000 \n",
"8 0.998877 0.999326 0.999326 \n",
"10 1.000000 1.000000 1.000000 \n",
"6 0.998204 0.997306 0.997081 \n",
"\n",
" mean_train_score std_train_score \n",
"7 0.999057 0.000220 \n",
"11 1.000000 0.000000 \n",
"8 0.999102 0.000201 \n",
"10 1.000000 0.000000 \n",
"6 0.997620 0.000578 \n",
"\n",
"[5 rows x 22 columns]"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"rf = RandomForestClassifier()\n",
"param = {'n_estimators': [10, 150, 300],\n",
" 'max_depth': [30, 60, 90, None]}\n",
"\n",
"gs = GridSearchCV(rf, param, cv=5, n_jobs=-1)# n_jobs=-1 for parallelizing search\n",
"gs_fit = gs.fit(X_count_feat, data['label'])\n",
"pd.DataFrame(gs_fit.cv_results_).sort_values('mean_test_score', ascending=False).head()"
]
}
],
"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.2"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": false,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment