{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "[![Open in Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/justmarkham/scikit-learn-tips/master?filepath=notebooks%2F31_kfold_shuffle.ipynb)\n", "\n", "[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/justmarkham/scikit-learn-tips/blob/master/notebooks/31_kfold_shuffle.ipynb)\n", "\n", "# 🤖⚡ scikit-learn tip #31 ([video](https://www.youtube.com/watch?v=Ld8-_WP0G90&list=PL5-da3qGB5ID7YYAqireYEew2mWVvgmj6&index=31))\n", "\n", "If you use cross-validation and your samples are NOT in an arbitrary order, shuffling may be required to get meaningful results.\n", "\n", "Use KFold or StratifiedKFold in order to shuffle!\n", "\n", "See example 👇" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "from sklearn.datasets import load_diabetes\n", "from sklearn.linear_model import LinearRegression, LogisticRegression\n", "from sklearn.model_selection import cross_val_score" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# set up the regression problem\n", "X_reg, y_reg = load_diabetes(return_X_y=True)\n", "reg = LinearRegression()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# set up the classification problem\n", "df = pd.read_csv('http://bit.ly/kaggletrain')\n", "X_clf = df[['Pclass', 'Fare', 'SibSp']]\n", "y_clf = df['Survived']\n", "clf = LogisticRegression()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "from sklearn.model_selection import KFold, StratifiedKFold" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Use KFold with regression problems:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0.43843604, 0.38982527, 0.52792606, 0.47359858, 0.57449343])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "kf = KFold(5, shuffle=True, random_state=1)\n", "cross_val_score(reg, X_reg, y_reg, cv=kf, scoring='r2')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Use StratifiedKFold with classification problems:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0.65363128, 0.7247191 , 0.66853933, 0.68539326, 0.65730337])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "skf = StratifiedKFold(5, shuffle=True, random_state=1)\n", "cross_val_score(clf, X_clf, y_clf, cv=skf, scoring='accuracy')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Want more tips? [View all tips on GitHub](https://github.com/justmarkham/scikit-learn-tips) or [Sign up to receive 2 tips by email every week](https://scikit-learn.tips) 💌\n", "\n", "© 2020 [Data School](https://www.dataschool.io). All rights reserved." ] } ], "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.9.4" } }, "nbformat": 4, "nbformat_minor": 4 }