{ "cells": [ { "attachments": {}, "cell_type": "markdown", "id": "edec260f-4232-4817-a035-fe3533030be1", "metadata": {}, "source": [ "# __AutoML을 사용하여 예측 모델 만들기__" ] }, { "attachments": {}, "cell_type": "markdown", "id": "1663f276", "metadata": {}, "source": [ "- 튜토리얼 난이도: ★☆☆☆☆\n", "- 읽는 시간: 5분\n", "- 사용 언어: [SQL](https://ko.wikipedia.org/wiki/SQL) (100%)\n", "- 실행 파일 위치: tutorial/thanosql_ml/regression/automl_regression.ipynb\n", "- 참고 문서: [(캐글) Bike Sharing Demand](https://www.kaggle.com/competitions/bike-sharing-demand/overview)" ] }, { "attachments": {}, "cell_type": "markdown", "id": "ee7afe15-fd61-4239-ac84-1e03043de750", "metadata": {}, "source": [ "## 튜토리얼 소개\n", "\n", "
\n", "

회귀 작업 이해하기

\n", "

회귀 작업은 목푯값(Target)이 연속성을 지닌 숫자를 예측하기 위해 사용하는 머신러닝(기계학습/Machine Learning)의 한 형태입니다. 예를 들어, 기상 데이터가 주어졌을 때 내일 기온을 예측하거나, 특정지역의 집값을 예측할 때 사용할 수 있습니다.

\n", "
\n", "\n", "기업이 특정 금액을 광고에 사용할 경우 과거의 유사한 사례의 판매 성과 데이터를 활용하여 광고의 성과를 예측할 수 있습니다. 광고하고자 하는 제품에 대한 특성부터 제품을 판매하는 시기, 주변 시장 정보, 경쟁사의 판매량 정보, 대상 고객군에 대한 정의, 산업군의 시장 트렌드 등 데이터화 할 수 있는 모든 특성(Feature)이 입력자료가 될 수 있습니다. 입력 데이터에서 통제 가능한 정보를 바꿔가면서 최적의 판매성과를 예측해 볼 수 있고 예측성과에 따라 광고에 소비할 비용을 조정할 수도 있습니다. 이러한 회귀 모델을 이용해서 광고 성과를 향상시키고 판매량을 지속적으로 늘릴 수 있습니다.\n", "\n", "__아래는 ThanoSQL 회귀 모델의 활용 및 예시입니다.__\n", "\n", " - 주식의 시가, 종가, 고가, 저가, 관련주 주가, 종합주가지수, 관련뉴스 등을 활용한 주식가격 예측 (금융)\n", " - 기기/설비의 온도, 진동, 소리 등 센서데이터를 이용한 고장 확률 및 잔존 수명 예측 (제조)\n", " - 날씨, 기온, 운량, 일사량 등을 활용한 태양광 에너지 발전량 예측 (에너지)\n", " - 과거 수요량 트렌드, 유가 및 환율 변동 등을 활용한 수요예측 (원자재)\n", "\n", "
\n", "

본 튜토리얼에서는

\n", "

👉 대표적인 머신러닝 경진대회 플랫폼인 캐글의 입문자를 위한 Bike Sharing Demand 데이터 세트를 사용하여 자전거 수요 예측 회귀 모델을 만듭니다. 이 대회의 목표는 아래와 같습니다. (참고로, 해당 대회의 데이터는 2011년부터 2012년까지 날짜와 시간, 기온, 습도, 풍속 등의 정보를 기반으로 자전거 대여 횟수가 기재되어 있습니다.)

\n", "
\n", "\n", "__특정 날짜에서 시간대별 자전거 대여 수량 예측하기__\n", "\n", "ThanoSQL에서는 자동화된 머신러닝(__AutoML__)을 제공합니다. 본 튜토리얼에서는 AutoML을 사용하여 자전거 대여 수량을 예측합니다. ThanoSQL에서 제공하는 AutoML은 모델(Model)개발을 위한 프로세스를 자동화하고, 데이터 과학(Data Science)에 대한 전문지식이 없어도 데이터의 수집 및 저장, 머신러닝 모델의 개발 및 배포(엔드투엔드 머신러닝 파이프라인)를 하나의 언어(ThanoSQL)만으로도 가능하도록 지원합니다.\n", "\n", "__ThanoSQL의 자동화 된 머신러닝을 사용하면 다음과 같은 장점이 있습니다.__\n", "\n", "1. 광범위한 프로그래밍 또는 데이터과학에 대한 지식이 없어도 머신러닝 솔루션의 구현 및 배포가 가능\n", "2. 개발모델의 배포에 들어가는 시간 및 리소스를 절약\n", "3. 의사결정을 위해 보유하고 있는 데이터를 이용한 신속한 문제해결이 가능\n", "\n", "이제부터 ThanoSQL을 사용하여 간단하게 자전거 대여 수량을 예측하는 회귀 모델을 만들어 봅니다." ] }, { "attachments": {}, "cell_type": "markdown", "id": "a68baaf7-ca66-4c0d-bd36-d6066ed22127", "metadata": {}, "source": [ "## __0. 데이터 세트 준비__\n", "\n", "ThanoSQL의 쿼리 구문을 사용하기 위해서는 [ThanoSQL 워크스페이스](https://docs.thanosql.ai/1.4/ko/getting_started/paas/workspace/lab/)에서 언급된 것처럼 API 토큰을 생성하고 아래의 쿼리를 실행해야 합니다." ] }, { "cell_type": "code", "execution_count": null, "id": "f543db2d-a192-41e9-878b-bfc3b67d0d6e", "metadata": {}, "outputs": [], "source": [ "%load_ext thanosql\n", "%thanosql API_TOKEN=<발급받은_API_TOKEN>" ] }, { "attachments": {}, "cell_type": "markdown", "id": "a3fc89e5-600a-4c5f-bba2-289a425db405", "metadata": {}, "source": [ "### __데이터 세트 준비__" ] }, { "cell_type": "code", "execution_count": 2, "id": "ad07a885-ee2e-4eeb-a2f0-634af9b3b843", "metadata": { "execution": { "iopub.execute_input": "2023-01-19T10:33:51.955088Z", "iopub.status.busy": "2023-01-19T10:33:51.954840Z", "iopub.status.idle": "2023-01-19T10:33:53.214656Z", "shell.execute_reply": "2023-01-19T10:33:53.213757Z", "shell.execute_reply.started": "2023-01-19T10:33:51.955070Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Success\n" ] } ], "source": [ "%%thanosql\n", "GET THANOSQL DATASET bike_sharing_data\n", "OPTIONS (overwrite=True)" ] }, { "attachments": {}, "cell_type": "markdown", "id": "77319ae0", "metadata": {}, "source": [ "
\n", "

쿼리 세부 정보

\n", " \n", "
" ] }, { "cell_type": "code", "execution_count": 3, "id": "5e344e20-54ab-4c3c-948f-e17fa95ed54a", "metadata": { "execution": { "iopub.execute_input": "2023-01-19T10:33:53.215716Z", "iopub.status.busy": "2023-01-19T10:33:53.215526Z", "iopub.status.idle": "2023-01-19T10:33:54.278885Z", "shell.execute_reply": "2023-01-19T10:33:54.278234Z", "shell.execute_reply.started": "2023-01-19T10:33:53.215699Z" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Success\n" ] } ], "source": [ "%%thanosql\n", "COPY bike_sharing_train\n", "OPTIONS (if_exists='replace')\n", "FROM 'thanosql-dataset/bike_sharing_data/bike_sharing_train.csv'" ] }, { "cell_type": "code", "execution_count": 4, "id": "c262d2e6-d486-4128-92f6-4c998653bee8", "metadata": { "execution": { "iopub.execute_input": "2023-01-19T10:33:54.279793Z", "iopub.status.busy": "2023-01-19T10:33:54.279622Z", "iopub.status.idle": "2023-01-19T10:33:55.350531Z", "shell.execute_reply": "2023-01-19T10:33:55.349799Z", "shell.execute_reply.started": "2023-01-19T10:33:54.279777Z" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Success\n" ] } ], "source": [ "%%thanosql\n", "COPY bike_sharing_test\n", "OPTIONS (if_exists='replace')\n", "FROM 'thanosql-dataset/bike_sharing_data/bike_sharing_test.csv'" ] }, { "attachments": {}, "cell_type": "markdown", "id": "ca05c715", "metadata": {}, "source": [ "
\n", "

쿼리 세부 정보

\n", " \n", "
" ] }, { "attachments": {}, "cell_type": "markdown", "id": "6a0f8d6e-a01f-4d17-bbcf-e1aac3509e12", "metadata": {}, "source": [ "## __1. 데이터 세트 확인__\n", "\n", "회귀 모델을 만들기 위해 ThanoSQL 워크스페이스 데이터베이스에 저장되어 있는 __bike_sharing_train__ 테이블을 사용합니다. 아래의 쿼리 구문을 실행하고 테이블의 내용을 확인합니다." ] }, { "cell_type": "code", "execution_count": 5, "id": "2727e422-7a05-4a4e-91cb-6036c22cea51", "metadata": { "execution": { "iopub.execute_input": "2023-01-19T10:33:55.352113Z", "iopub.status.busy": "2023-01-19T10:33:55.351925Z", "iopub.status.idle": "2023-01-19T10:33:56.327105Z", "shell.execute_reply": "2023-01-19T10:33:56.326430Z", "shell.execute_reply.started": "2023-01-19T10:33:55.352096Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
datetimeseasonholidayworkingdayweathertempatemphumiditywindspeedcount
02011-01-01 0:0010019.8414.395810.016
12011-01-01 1:0010019.0213.635800.040
22011-01-01 2:0010019.0213.635800.032
32011-01-01 3:0010019.8414.395750.013
42011-01-01 4:0010019.8414.395750.01
\n", "
" ], "text/plain": [ " datetime season holiday workingday weather temp atemp \\\n", "0 2011-01-01 0:00 1 0 0 1 9.84 14.395 \n", "1 2011-01-01 1:00 1 0 0 1 9.02 13.635 \n", "2 2011-01-01 2:00 1 0 0 1 9.02 13.635 \n", "3 2011-01-01 3:00 1 0 0 1 9.84 14.395 \n", "4 2011-01-01 4:00 1 0 0 1 9.84 14.395 \n", "\n", " humidity windspeed count \n", "0 81 0.0 16 \n", "1 80 0.0 40 \n", "2 80 0.0 32 \n", "3 75 0.0 13 \n", "4 75 0.0 1 " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%thanosql\n", "SELECT *\n", "FROM bike_sharing_train\n", "LIMIT 5" ] }, { "attachments": {}, "cell_type": "markdown", "id": "c477e2d6", "metadata": {}, "source": [ "
\n", "

데이터 테이블 이해하기

\n", "

bike_sharing_train 테이블은 아래와 같은 정보를 담고 있습니다. 2011년 1월부터 2012년 12월까지 날짜와 시간, 기온, 습도, 풍속 등의 정보를 기반으로 1시간 간격 동안의 자전거 대여 횟수에 대한 정보를 담고 있습니다.

\n", " \n", "
" ] }, { "attachments": {}, "cell_type": "markdown", "id": "77cd13e0-9820-45d3-8d81-7f84d6789942", "metadata": {}, "source": [ "## __2. 회귀 모델 생성__\n", "\n", "이전 단계에서 확인한 __bike_sharing_train__ 테이블을 사용하여 자전거 수요 예측 회귀 모델을 만듭니다. 아래의 쿼리 구문을 실행하여 bike_regression이라는 이름의 모델을 만듭니다. \n", "(쿼리 실행 시 예상 소요 시간: 8 min)" ] }, { "cell_type": "code", "execution_count": 6, "id": "a019d5be-b11a-43e3-8213-c02d3c1ec49e", "metadata": { "execution": { "iopub.execute_input": "2023-01-19T10:33:56.327894Z", "iopub.status.busy": "2023-01-19T10:33:56.327723Z", "iopub.status.idle": "2023-01-19T10:39:03.639957Z", "shell.execute_reply": "2023-01-19T10:39:03.639169Z", "shell.execute_reply.started": "2023-01-19T10:33:56.327877Z" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Success\n" ] } ], "source": [ "%%thanosql\n", "BUILD MODEL bike_regression\n", "USING AutomlRegressor\n", "OPTIONS (\n", " target_col='count',\n", " impute_type='simple',\n", " datetime_attribs=['datetime'],\n", " time_left_for_this_task=300,\n", " overwrite=True\n", " )\n", "AS\n", "SELECT *\n", "FROM bike_sharing_train" ] }, { "attachments": {}, "cell_type": "markdown", "id": "14b85130", "metadata": {}, "source": [ "
\n", "

쿼리 세부 정보

\n", " \n", "
" ] }, { "attachments": {}, "cell_type": "markdown", "id": "bbb0db7b-2c2a-4c8e-b475-7c908a16b7ba", "metadata": {}, "source": [ "## __3. 생성된 모델 평가__\n", "\n", "아래의 쿼리문을 실행하여 이전 단계에서 만든 예측 모델의 성능을 평가합니다." ] }, { "cell_type": "code", "execution_count": 7, "id": "16f3413b-c5b0-419b-a09e-da2874b15d32", "metadata": { "execution": { "iopub.execute_input": "2023-01-19T10:39:03.641175Z", "iopub.status.busy": "2023-01-19T10:39:03.640982Z", "iopub.status.idle": "2023-01-19T10:39:05.614528Z", "shell.execute_reply": "2023-01-19T10:39:05.613836Z", "shell.execute_reply.started": "2023-01-19T10:39:03.641158Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
metricscore
0MAE78.6563
1MSE10986.4542
2R20.2292
3RMSLE1.3861
4MAPE0.5028
\n", "
" ], "text/plain": [ " metric score\n", "0 MAE 78.6563\n", "1 MSE 10986.4542\n", "2 R2 0.2292\n", "3 RMSLE 1.3861\n", "4 MAPE 0.5028" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%thanosql\n", "EVALUATE USING bike_regression\n", "OPTIONS (\n", " target_col='count'\n", " )\n", "AS\n", "SELECT *\n", "FROM bike_sharing_train" ] }, { "attachments": {}, "cell_type": "markdown", "id": "48d3f16d", "metadata": {}, "source": [ "
\n", "

쿼리 세부 정보

\n", " \n", "
\n", "\n", "
\n", "

평가용 데이터 세트

\n", "

평가용 데이터 세트는 학습 데이터 세트의 일부를 분리하여 학습에 사용되지 않아야 하나 튜토리얼에서는 편의상 학습 데이터를 사용합니다.

\n", "
" ] }, { "attachments": {}, "cell_type": "markdown", "id": "8b266f94-363e-46bc-a6fd-77898bbc0804", "metadata": {}, "source": [ "## __4. 생성된 모델을 사용하여 자전거 대여 수량 예측__\n", "\n", "이전 단계에서 생성한 수요 예측 모델을 사용해 10개의 데이터에 대한 자전거 대여 수량을 예측해 봅니다. 테스트용 데이터 세트(학습에 이용되지 않은 데이터 테이블, bike_sharing_test)를 사용합니다." ] }, { "cell_type": "code", "execution_count": 8, "id": "6769745c-45bd-42d9-ac50-b551c11a96b1", "metadata": { "execution": { "iopub.execute_input": "2023-01-19T10:39:05.615539Z", "iopub.status.busy": "2023-01-19T10:39:05.615340Z", "iopub.status.idle": "2023-01-19T10:39:07.094578Z", "shell.execute_reply": "2023-01-19T10:39:07.093897Z", "shell.execute_reply.started": "2023-01-19T10:39:05.615522Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
datetimeseasonholidayworkingdayweathertempatemphumiditywindspeedpredict_result
02011-01-20 0:00101110.6611.3655626.0027102.836334
12011-01-20 1:00101110.6613.635560.000092.060480
22011-01-20 2:00101110.6613.635560.000092.060480
32011-01-20 3:00101110.6612.8805611.001495.181085
42011-01-20 4:00101110.6612.8805611.001495.181085
52011-01-20 5:0010119.8411.3656015.001391.816701
62011-01-20 6:0010119.0210.6056015.001387.213365
72011-01-20 7:0010119.0210.6055515.001387.054590
82011-01-20 8:0010119.0210.6055519.001288.568595
92011-01-20 9:0010129.8411.3655215.0013103.445460
\n", "
" ], "text/plain": [ " datetime season holiday workingday weather temp atemp \\\n", "0 2011-01-20 0:00 1 0 1 1 10.66 11.365 \n", "1 2011-01-20 1:00 1 0 1 1 10.66 13.635 \n", "2 2011-01-20 2:00 1 0 1 1 10.66 13.635 \n", "3 2011-01-20 3:00 1 0 1 1 10.66 12.880 \n", "4 2011-01-20 4:00 1 0 1 1 10.66 12.880 \n", "5 2011-01-20 5:00 1 0 1 1 9.84 11.365 \n", "6 2011-01-20 6:00 1 0 1 1 9.02 10.605 \n", "7 2011-01-20 7:00 1 0 1 1 9.02 10.605 \n", "8 2011-01-20 8:00 1 0 1 1 9.02 10.605 \n", "9 2011-01-20 9:00 1 0 1 2 9.84 11.365 \n", "\n", " humidity windspeed predict_result \n", "0 56 26.0027 102.836334 \n", "1 56 0.0000 92.060480 \n", "2 56 0.0000 92.060480 \n", "3 56 11.0014 95.181085 \n", "4 56 11.0014 95.181085 \n", "5 60 15.0013 91.816701 \n", "6 60 15.0013 87.213365 \n", "7 55 15.0013 87.054590 \n", "8 55 19.0012 88.568595 \n", "9 52 15.0013 103.445460 " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%thanosql\n", "PREDICT USING bike_regression\n", "OPTIONS (\n", " result_col='predict_result'\n", " )\n", "AS\n", "SELECT *\n", "FROM bike_sharing_test\n", "LIMIT 10" ] }, { "attachments": {}, "cell_type": "markdown", "id": "b2ccc6b9", "metadata": {}, "source": [ "
\n", "

쿼리 세부 정보

\n", " \n", "
" ] }, { "attachments": {}, "cell_type": "markdown", "id": "d1269716", "metadata": {}, "source": [ "## __5. 튜토리얼을 마치며__\n", "\n", "이번 튜토리얼에서는 [캐글](https://www.kaggle.com)의 Bike Sharing Demand 데이터 세트를 사용하여 자전거 수요 예측 회귀 모델을 만들어 보았습니다. 초급 단계 튜토리얼인만큼 정확도 향상을 위한 과정보다는 전반적인 프로세스 위주의 설명으로 진행 하였습니다.\n", "\n", "* [나만의 데이터 업로드하기](https://docs.thanosql.ai/1.4/ko/getting_started/data_upload/)\n", "* [나만의 데이터 테이블 생성하기](https://docs.thanosql.ai/1.4/ko/how-to_guides/ThanoSQL_query/COPY_SYNTAX/)\n", "* [나만의 모델 업로드하기](https://docs.thanosql.ai/1.4/ko/how-to_guides/ThanoSQL_query/UPLOAD_MODEL_SYNTAX/)\n", "\n", "
\n", "

나만의 서비스를 위한 모델 배포 관련 문의

\n", "

ThanoSQL을 활용해 나만의 모델을 만들거나, 나의 서비스에 적용하는데 어려움이 있다면 언제든 아래로 문의주세요😊

\n", "

회귀 모델 구축 관련 문의: contact@smartmind.team

\n", "
" ] } ], "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.8.13" }, "vscode": { "interpreter": { "hash": "54a1ec72395a4a5a649013bb47cb6c1a711fb4b3d33a07524a09f31d6d2ee0ec" } } }, "nbformat": 4, "nbformat_minor": 5 }