Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### Cross-validation using sklearn"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 29,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([0.9853431 , 0.98533333, 0.974 , 0.96533333, 0.96 ,\n",
- " 0.97933333, 0.99 , 0.99333333, 1. , 1. ])"
- ]
- },
- "execution_count": 29,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "# Import the function for implementing cross validation\n",
- "from sklearn.model_selection import cross_val_score\n",
- "\n",
- "# Use that function to print the cross validation score for 10 folds\n",
- "cross_val_score(model,features,target,cv=10)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### Setting up GridSearch parameters"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 30,
- "metadata": {},
- "outputs": [],
- "source": [
- "# Generate values for maximum depth\n",
- "depth = [i for i in range(5,21,1)]\n",
- "\n",
- "# Generate values for minimum sample size\n",
- "samples = [i for i in range(50,500,50)]\n",
- "\n",
- "# Create the dictionary with parameters to be checked\n",
- "parameters = dict(max_depth=depth, min_samples_leaf=samples)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### Implementing GridSearch"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 32,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "{'max_depth': 5, 'min_samples_leaf': 50}\n"
- ]
- }
- ],
- "source": [
- "# import the GridSearchCV function\n",
- "from sklearn.model_selection import GridSearchCV\n",
- "\n",
- "# set up parameters: done\n",
- "parameters = dict(max_depth=depth, min_samples_leaf=samples)\n",
- "\n",
- "# initialize the param_search function using the GridSearchCV function, initial model and parameters above\n",
- "param_search = GridSearchCV(model, parameters, cv = 3)\n",
- "\n",
- "# fit the param_search to the training dataset\n",
- "param_search.fit(features_train, target_train)\n",
- "\n",
- "# print the best parameters found\n",
- "print(param_search.best_params_)"
- ]
- }
- ],
- "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.3"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement