{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "1mrH6zALKj2u" }, "source": [ "# 5.1.4 반복 교차 검증" ] }, { "cell_type": "markdown", "metadata": { "id": "SuEyxu7ZKj2-" }, "source": [ "*아래 링크를 통해 이 노트북을 주피터 노트북 뷰어(nbviewer.org)로 보거나 구글 코랩(colab.research.google.com)에서 실행할 수 있습니다.*\n", "\n", "\n", " \n", " \n", "
\n", " 주피터 노트북 뷰어로 보기\n", " \n", " 구글 코랩(Colab)에서 실행하기\n", "
" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "id": "z7FgfKaxKj3A", "outputId": "8cea5f79-e3a0-434a-e2f4-f98ace00633d", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\u001b[?25l \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/10.8 MB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K \u001b[91m╸\u001b[0m\u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.2/10.8 MB\u001b[0m \u001b[31m7.0 MB/s\u001b[0m eta \u001b[36m0:00:02\u001b[0m\r\u001b[2K \u001b[91m━━━━━━━\u001b[0m\u001b[91m╸\u001b[0m\u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.1/10.8 MB\u001b[0m \u001b[31m30.6 MB/s\u001b[0m eta \u001b[36m0:00:01\u001b[0m\r\u001b[2K \u001b[91m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[91m╸\u001b[0m\u001b[90m━━━━\u001b[0m \u001b[32m9.6/10.8 MB\u001b[0m \u001b[31m93.4 MB/s\u001b[0m eta \u001b[36m0:00:01\u001b[0m\r\u001b[2K \u001b[91m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[91m╸\u001b[0m \u001b[32m10.8/10.8 MB\u001b[0m \u001b[31m165.3 MB/s\u001b[0m eta \u001b[36m0:00:01\u001b[0m\r\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m10.8/10.8 MB\u001b[0m \u001b[31m110.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25h" ] } ], "source": [ "# 노트북이 코랩에서 실행 중인지 체크합니다.\n", "import os\n", "import sys\n", "if 'google.colab' in sys.modules:\n", " # 사이킷런 최신 버전을 설치합니다.\n", " !pip install -q --upgrade scikit-learn\n", "\n", "import sklearn" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "id": "m_9Mve_qKj3v" }, "outputs": [], "source": [ "from sklearn.model_selection import cross_val_score, KFold, StratifiedKFold\n", "from sklearn.datasets import load_iris\n", "from sklearn.linear_model import LogisticRegression\n", "\n", "iris = load_iris()\n", "logreg = LogisticRegression(max_iter=1000)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "id": "pfhFCcUUKj3w", "outputId": "b124a79f-a23d-4020-d45d-bf6917238398", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "교차 검증 점수:\n", " [1. 0.96666667 0.93333333 1. 0.93333333 0.96666667\n", " 0.96666667 0.93333333 1. 0.96666667 0.93333333 1.\n", " 1. 0.96666667 0.96666667 0.9 1. 1.\n", " 0.93333333 0.96666667 0.93333333 0.96666667 0.96666667 1.\n", " 0.96666667 1. 0.96666667 0.96666667 0.9 1.\n", " 0.96666667 0.96666667 0.96666667 0.96666667 0.93333333 0.96666667\n", " 0.96666667 1. 1. 0.9 0.96666667 1.\n", " 0.9 0.96666667 0.96666667 0.9 0.96666667 0.96666667\n", " 1. 0.96666667]\n", "교차 검증 평균 점수: 0.965\n" ] } ], "source": [ "from sklearn.model_selection import RepeatedStratifiedKFold\n", "\n", "rskfold = RepeatedStratifiedKFold(random_state=42)\n", "scores = cross_val_score(logreg, iris.data, iris.target, cv=rskfold)\n", "\n", "print(\"교차 검증 점수:\\n\", scores)\n", "print(\"교차 검증 평균 점수: {:.3f}\".format(scores.mean()))" ] } ], "metadata": { "environment": { "kernel": "python3", "name": "common-cpu.m102", "type": "gcloud", "uri": "gcr.io/deeplearning-platform-release/base-cpu:m102" }, "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.12" }, "colab": { "provenance": [] } }, "nbformat": 4, "nbformat_minor": 0 }