{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "이 노트북의 코드에 대한 설명은 [New SAGA solver](https://tensorflow.blog/2017/12/15/new-saga-solver/) 글을 참고하세요." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from sklearn.datasets import load_breast_cancer, load_boston\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.linear_model import LogisticRegression, Ridge" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "for cancer dataset" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "cancer = load_breast_cancer()\n", "X_train, X_test, y_train, y_test = train_test_split(\n", " cancer.data, cancer.target, stratify=cancer.target, random_state=42)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "훈련 세트 점수: 0.927\n", "테스트 세트 점수: 0.930\n" ] } ], "source": [ "logreg_sag = LogisticRegression(solver='sag', max_iter=10000).fit(X_train, y_train)\n", "print(\"훈련 세트 점수: {:.3f}\".format(logreg_sag.score(X_train, y_train)))\n", "print(\"테스트 세트 점수: {:.3f}\".format(logreg_sag.score(X_test, y_test)))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "훈련 세트 점수: 0.920\n", "테스트 세트 점수: 0.937\n" ] } ], "source": [ "logreg_saga = LogisticRegression(solver='saga', max_iter=10000).fit(X_train, y_train)\n", "print(\"훈련 세트 점수: {:.3f}\".format(logreg_saga.score(X_train, y_train)))\n", "print(\"테스트 세트 점수: {:.3f}\".format(logreg_saga.score(X_test, y_test)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "for california housing dataset" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "from sklearn.datasets import fetch_california_housing\n", "housing = fetch_california_housing()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "(array([[ 8.32520000e+00, 4.10000000e+01, 6.98412698e+00,\n", " 1.02380952e+00, 3.22000000e+02, 2.55555556e+00,\n", " 3.78800000e+01, -1.22230000e+02],\n", " [ 8.30140000e+00, 2.10000000e+01, 6.23813708e+00,\n", " 9.71880492e-01, 2.40100000e+03, 2.10984183e+00,\n", " 3.78600000e+01, -1.22220000e+02],\n", " [ 7.25740000e+00, 5.20000000e+01, 8.28813559e+00,\n", " 1.07344633e+00, 4.96000000e+02, 2.80225989e+00,\n", " 3.78500000e+01, -1.22240000e+02],\n", " [ 5.64310000e+00, 5.20000000e+01, 5.81735160e+00,\n", " 1.07305936e+00, 5.58000000e+02, 2.54794521e+00,\n", " 3.78500000e+01, -1.22250000e+02],\n", " [ 3.84620000e+00, 5.20000000e+01, 6.28185328e+00,\n", " 1.08108108e+00, 5.65000000e+02, 2.18146718e+00,\n", " 3.78500000e+01, -1.22250000e+02]]),\n", " array([4.526, 3.585, 3.521, 3.413, 3.422]))" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "housing.data[:5], housing.target[:5]" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['MedInc',\n", " 'HouseAge',\n", " 'AveRooms',\n", " 'AveBedrms',\n", " 'Population',\n", " 'AveOccup',\n", " 'Latitude',\n", " 'Longitude']" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "housing.feature_names" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ ".. _california_housing_dataset:\n", "\n", "California Housing dataset\n", "--------------------------\n", "\n", "**Data Set Characteristics:**\n", "\n", " :Number of Instances: 20640\n", "\n", " :Number of Attributes: 8 numeric, predictive attributes and the target\n", "\n", " :Attribute Information:\n", " - MedInc median income in block group\n", " - HouseAge median house age in block group\n", " - AveRooms average number of rooms per household\n", " - AveBedrms average number of bedrooms per household\n", " - Population block group population\n", " - AveOccup average number of household members\n", " - Latitude block group latitude\n", " - Longitude block group longitude\n", "\n", " :Missing Attribute Values: None\n", "\n", "This dataset was obtained from the StatLib repository.\n", "https://www.dcc.fc.up.pt/~ltorgo/Regression/cal_housing.html\n", "\n", "The target variable is the median house value for California districts,\n", "expressed in hundreds of thousands of dollars ($100,000).\n", "\n", "This dataset was derived from the 1990 U.S. census, using one row per census\n", "block group. A block group is the smallest geographical unit for which the U.S.\n", "Census Bureau publishes sample data (a block group typically has a population\n", "of 600 to 3,000 people).\n", "\n", "An household is a group of people residing within a home. Since the average\n", "number of rooms and bedrooms in this dataset are provided per household, these\n", "columns may take surpinsingly large values for block groups with few households\n", "and many empty houses, such as vacation resorts.\n", "\n", "It can be downloaded/loaded using the\n", ":func:`sklearn.datasets.fetch_california_housing` function.\n", "\n", ".. topic:: References\n", "\n", " - Pace, R. Kelley and Ronald Barry, Sparse Spatial Autoregressions,\n", " Statistics and Probability Letters, 33 (1997) 291-297\n", "\n" ] } ], "source": [ "print(housing.DESCR)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "X_train, X_test, y_train, y_test = train_test_split(\n", " housing.data, housing.target, random_state=42)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "훈련 세트 점수: 0.061\n", "테스트 세트 점수: 0.062\n" ] } ], "source": [ "ridge = Ridge(solver='sag').fit(X_train, y_train)\n", "print(\"훈련 세트 점수: {:.3f}\".format(ridge.score(X_train, y_train)))\n", "print(\"테스트 세트 점수: {:.3f}\".format(ridge.score(X_test, y_test)))" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "훈련 세트 점수: 0.035\n", "테스트 세트 점수: 0.036\n" ] } ], "source": [ "ridge_saga = Ridge(solver='saga').fit(X_train, y_train)\n", "print(\"훈련 세트 점수: {:.3f}\".format(ridge_saga.score(X_train, y_train)))\n", "print(\"테스트 세트 점수: {:.3f}\".format(ridge_saga.score(X_test, y_test)))" ] } ], "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 }