{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Cross Validation with KFold" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 試著print出X_train, X_test,並將shuffle改成True看看結果如何變化。" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from sklearn.model_selection import KFold\n", "X = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]])\n", "y = np.array([1, 2, 3, 4, 5])\n", "kf = KFold(n_splits=5, random_state=None, shuffle=False)\n", "for train_index, val_index in kf.split(X):\n", " X_train, X_val = X[train_index], X[val_index]\n", " y_train, y_val = y[train_index], y[val_index]\n", " print(\"TRAIN index:\", train_index, \"Validation index:\", val_index)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Print Training and Validation data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "i = 1\n", "for train_index, val_index in kf.split(X):\n", " X_train, X_val = X[train_index], X[val_index]\n", " y_train, y_val = y[train_index], y[val_index]\n", " print('Cross Validation%d==============' % i, '\\n'\n", " \"TRAIN data:\", '\\n', X_train, '\\n', \"Validation data:\", X_val)\n", " i += 1" ] }, { "cell_type": "code", "execution_count": null, "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.10.2" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "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 } }, "nbformat": 4, "nbformat_minor": 4 }