{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "이 노트북의 코드에 대한 설명은 [Pipeline에서 캐싱을 사용하기](https://tensorflow.blog/2017/12/08/pipeline%ec%97%90%ec%84%9c-%ec%ba%90%ec%8b%b1%ec%9d%84-%ec%82%ac%ec%9a%a9%ed%95%98%ea%b8%b0/) 글을 참고하세요." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# 보스턴 주택 데이터셋이 1.0 버전에 deprecated 되었고 1.2 버전에서 삭제됩니다.\n", "# 경고 메시지를 피하기 위해 다음 코드를 추가합니다.\n", "import warnings\n", "warnings.filterwarnings(\"ignore\", category=FutureWarning)\n", "\n", "from sklearn.datasets import load_boston\n", "from sklearn.preprocessing import StandardScaler, PolynomialFeatures\n", "from sklearn.linear_model import Ridge\n", "from sklearn.model_selection import train_test_split, GridSearchCV\n", "from sklearn.pipeline import make_pipeline\n", "\n", "boston = load_boston()\n", "X_train, X_test, y_train, y_test = train_test_split(boston.data, boston.target, random_state=0)\n", "param_grid = {'polynomialfeatures__degree': [1, 2, 3, 4, 5],\n", " 'ridge__alpha': [0.001, 0.01, 0.1, 1, 10, 100]}" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "pipe = make_pipeline(StandardScaler(), PolynomialFeatures(), Ridge())\n", "grid = GridSearchCV(pipe, param_grid=param_grid, cv=5, n_jobs=-1)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "696 ms ± 19.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" ] } ], "source": [ "%timeit grid.fit(X_train, y_train)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "from tempfile import mkdtemp\n", "from shutil import rmtree" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "cache_dir = mkdtemp()\n", "pipe2 = make_pipeline(StandardScaler(), PolynomialFeatures(), Ridge(), memory=cache_dir)\n", "grid2 = GridSearchCV(pipe2, param_grid=param_grid, cv=5, n_jobs=-1)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "714 ms ± 9.09 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" ] } ], "source": [ "%timeit grid2.fit(X_train, y_train)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "rmtree(cache_dir)" ] } ], "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.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }