Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "### Cross-validation using sklearn"
  8. ]
  9. },
  10. {
  11. "cell_type": "code",
  12. "execution_count": 29,
  13. "metadata": {},
  14. "outputs": [
  15. {
  16. "data": {
  17. "text/plain": [
  18. "array([0.9853431 , 0.98533333, 0.974 , 0.96533333, 0.96 ,\n",
  19. " 0.97933333, 0.99 , 0.99333333, 1. , 1. ])"
  20. ]
  21. },
  22. "execution_count": 29,
  23. "metadata": {},
  24. "output_type": "execute_result"
  25. }
  26. ],
  27. "source": [
  28. "# Import the function for implementing cross validation\n",
  29. "from sklearn.model_selection import cross_val_score\n",
  30. "\n",
  31. "# Use that function to print the cross validation score for 10 folds\n",
  32. "cross_val_score(model,features,target,cv=10)"
  33. ]
  34. },
  35. {
  36. "cell_type": "markdown",
  37. "metadata": {},
  38. "source": [
  39. "### Setting up GridSearch parameters"
  40. ]
  41. },
  42. {
  43. "cell_type": "code",
  44. "execution_count": 30,
  45. "metadata": {},
  46. "outputs": [],
  47. "source": [
  48. "# Generate values for maximum depth\n",
  49. "depth = [i for i in range(5,21,1)]\n",
  50. "\n",
  51. "# Generate values for minimum sample size\n",
  52. "samples = [i for i in range(50,500,50)]\n",
  53. "\n",
  54. "# Create the dictionary with parameters to be checked\n",
  55. "parameters = dict(max_depth=depth, min_samples_leaf=samples)"
  56. ]
  57. },
  58. {
  59. "cell_type": "markdown",
  60. "metadata": {},
  61. "source": [
  62. "### Implementing GridSearch"
  63. ]
  64. },
  65. {
  66. "cell_type": "code",
  67. "execution_count": 32,
  68. "metadata": {},
  69. "outputs": [
  70. {
  71. "name": "stdout",
  72. "output_type": "stream",
  73. "text": [
  74. "{'max_depth': 5, 'min_samples_leaf': 50}\n"
  75. ]
  76. }
  77. ],
  78. "source": [
  79. "# import the GridSearchCV function\n",
  80. "from sklearn.model_selection import GridSearchCV\n",
  81. "\n",
  82. "# set up parameters: done\n",
  83. "parameters = dict(max_depth=depth, min_samples_leaf=samples)\n",
  84. "\n",
  85. "# initialize the param_search function using the GridSearchCV function, initial model and parameters above\n",
  86. "param_search = GridSearchCV(model, parameters, cv = 3)\n",
  87. "\n",
  88. "# fit the param_search to the training dataset\n",
  89. "param_search.fit(features_train, target_train)\n",
  90. "\n",
  91. "# print the best parameters found\n",
  92. "print(param_search.best_params_)"
  93. ]
  94. }
  95. ],
  96. "metadata": {
  97. "kernelspec": {
  98. "display_name": "Python 3",
  99. "language": "python",
  100. "name": "python3"
  101. },
  102. "language_info": {
  103. "codemirror_mode": {
  104. "name": "ipython",
  105. "version": 3
  106. },
  107. "file_extension": ".py",
  108. "mimetype": "text/x-python",
  109. "name": "python",
  110. "nbconvert_exporter": "python",
  111. "pygments_lexer": "ipython3",
  112. "version": "3.7.3"
  113. }
  114. },
  115. "nbformat": 4,
  116. "nbformat_minor": 2
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement