{ "cells": [ { "cell_type": "markdown", "id": "8f33a981", "metadata": {}, "source": [ "# K- fold cross validation" ] }, { "cell_type": "code", "execution_count": 27, "id": "e3d77c4a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(150, 4)\n", "(150,)\n" ] } ], "source": [ "from sklearn.datasets import load_iris\n", "iris=load_iris()\n", "x,y=iris.data,iris.target\n", "print(x.shape)\n", "print(y.shape)" ] }, { "cell_type": "code", "execution_count": 28, "id": "d3b45e19", "metadata": {}, "outputs": [], "source": [ "from sklearn.model_selection import train_test_split\n", "x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=150)" ] }, { "cell_type": "code", "execution_count": 29, "id": "6538de84", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(120, 4)\n", "(120,)\n" ] } ], "source": [ "print(x_train.shape)\n", "print(y_train.shape)" ] }, { "cell_type": "code", "execution_count": 30, "id": "1fe23a8d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "KNeighborsClassifier(n_neighbors=3)" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from sklearn.neighbors import KNeighborsClassifier\n", "model=KNeighborsClassifier(n_neighbors=3)\n", "model.fit(x,y)" ] }, { "cell_type": "code", "execution_count": 31, "id": "bd95c560", "metadata": { "scrolled": true }, "outputs": [], "source": [ "from sklearn.model_selection import cross_val_score" ] }, { "cell_type": "code", "execution_count": 32, "id": "157a9b35", "metadata": { "collapsed": true }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/azarmohammad/opt/anaconda3/lib/python3.9/site-packages/sklearn/neighbors/_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.\n", " mode, _ = stats.mode(_y[neigh_ind, k], axis=1)\n", "/Users/azarmohammad/opt/anaconda3/lib/python3.9/site-packages/sklearn/neighbors/_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.\n", " mode, _ = stats.mode(_y[neigh_ind, k], axis=1)\n", "/Users/azarmohammad/opt/anaconda3/lib/python3.9/site-packages/sklearn/neighbors/_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.\n", " mode, _ = stats.mode(_y[neigh_ind, k], axis=1)\n", "/Users/azarmohammad/opt/anaconda3/lib/python3.9/site-packages/sklearn/neighbors/_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.\n", " mode, _ = stats.mode(_y[neigh_ind, k], axis=1)\n", "/Users/azarmohammad/opt/anaconda3/lib/python3.9/site-packages/sklearn/neighbors/_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.\n", " mode, _ = stats.mode(_y[neigh_ind, k], axis=1)\n" ] } ], "source": [ "x=cross_val_score(model, x_train, y_train, cv=5)" ] }, { "cell_type": "code", "execution_count": 33, "id": "20cf793b", "metadata": { "collapsed": true }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/azarmohammad/opt/anaconda3/lib/python3.9/site-packages/sklearn/neighbors/_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.\n", " mode, _ = stats.mode(_y[neigh_ind, k], axis=1)\n", "/Users/azarmohammad/opt/anaconda3/lib/python3.9/site-packages/sklearn/neighbors/_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.\n", " mode, _ = stats.mode(_y[neigh_ind, k], axis=1)\n", "/Users/azarmohammad/opt/anaconda3/lib/python3.9/site-packages/sklearn/neighbors/_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.\n", " mode, _ = stats.mode(_y[neigh_ind, k], axis=1)\n", "/Users/azarmohammad/opt/anaconda3/lib/python3.9/site-packages/sklearn/neighbors/_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.\n", " mode, _ = stats.mode(_y[neigh_ind, k], axis=1)\n", "/Users/azarmohammad/opt/anaconda3/lib/python3.9/site-packages/sklearn/neighbors/_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.\n", " mode, _ = stats.mode(_y[neigh_ind, k], axis=1)\n", "/Users/azarmohammad/opt/anaconda3/lib/python3.9/site-packages/sklearn/neighbors/_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.\n", " mode, _ = stats.mode(_y[neigh_ind, k], axis=1)\n", "/Users/azarmohammad/opt/anaconda3/lib/python3.9/site-packages/sklearn/neighbors/_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.\n", " mode, _ = stats.mode(_y[neigh_ind, k], axis=1)\n", "/Users/azarmohammad/opt/anaconda3/lib/python3.9/site-packages/sklearn/neighbors/_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.\n", " mode, _ = stats.mode(_y[neigh_ind, k], axis=1)\n", "/Users/azarmohammad/opt/anaconda3/lib/python3.9/site-packages/sklearn/neighbors/_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.\n", " mode, _ = stats.mode(_y[neigh_ind, k], axis=1)\n", "/Users/azarmohammad/opt/anaconda3/lib/python3.9/site-packages/sklearn/neighbors/_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.\n", " mode, _ = stats.mode(_y[neigh_ind, k], axis=1)\n" ] } ], "source": [ "y=cross_val_score(model, x_train, y_train, cv=10)" ] }, { "cell_type": "code", "execution_count": 34, "id": "ed1da0a7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0.95833333 0.95833333 0.95833333 0.95833333 1. ]\n", "[0.91666667 0.91666667 1. 0.91666667 0.91666667 1.\n", " 0.91666667 1. 1. 1. ]\n" ] } ], "source": [ "#x variable is dividiing the total dataset into 5 folds for training \n", "print(x)\n", "print(y)" ] }, { "cell_type": "code", "execution_count": null, "id": "4bd7c960", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.13" } }, "nbformat": 4, "nbformat_minor": 5 }