{ "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": "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": 2, "metadata": { "id": "pfhFCcUUKj3w", "outputId": "77bdbbd3-d427-42b6-bd74-581bd25cf459", "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 }