{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# K-fold cross validation - Regression Model\n", "Based on the [Ludwig regression example](https://ludwig-ai.github.io/ludwig-docs/examples/#simple-regression-fuel-efficiency-prediction) \n", "\n", "[Data set](https://archive.ics.uci.edu/ml/datasets/auto+mpg)\n", "\n", "This example demonstrates teh following:\n", "\n", "- Download a data set and create a pandas dataframe\n", "- Create a training and hold-out test data sets\n", "- Create a Ludwig config data structure from the pandas dataframe\n", "- Run a 5-fold cross validation analysis with the training data\n", "- Use Ludwig APIs to train and assess model performance on hold-out test data set" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import logging\n", "import os\n", "import os.path\n", "import shutil\n", "import tempfile\n", "\n", "import matplotlib.pyplot as plt\n", "import pandas as pd\n", "import requests\n", "import seaborn as sns\n", "from sklearn.model_selection import train_test_split\n", "\n", "from ludwig.api import kfold_cross_validate, LudwigModel" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Contstants" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "DATA_SET_URL = \"http://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data\"\n", "DATA_SET = \"auto_mpg.data\"\n", "RESULTS_DIR = \"results\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Clean out previous results" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "if os.path.isfile(DATA_SET):\n", " os.remove(DATA_SET)\n", "\n", "shutil.rmtree(RESULTS_DIR, ignore_errors=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Retrieve data from UCI Machine Learning Repository" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Download required data" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "r = requests.get(DATA_SET_URL)\n", "if r.status_code == 200:\n", " with open(DATA_SET, \"w\") as f:\n", " f.write(r.content.decode(\"utf-8\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create Pandas DataFrame from downloaded data" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(398, 8)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "raw_df = pd.read_csv(DATA_SET, header=None, na_values=\"?\", comment=\"\\t\", sep=\" \", skipinitialspace=True)\n", "\n", "\n", "raw_df.columns = [\"MPG\", \"Cylinders\", \"Displacement\", \"Horsepower\", \"Weight\", \"Acceleration\", \"ModelYear\", \"Origin\"]\n", "raw_df.shape" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | MPG | \n", "Cylinders | \n", "Displacement | \n", "Horsepower | \n", "Weight | \n", "Acceleration | \n", "ModelYear | \n", "Origin | \n", "
|---|---|---|---|---|---|---|---|---|
| 0 | \n", "18.0 | \n", "8 | \n", "307.0 | \n", "130.0 | \n", "3504.0 | \n", "12.0 | \n", "70 | \n", "1 | \n", "
| 1 | \n", "15.0 | \n", "8 | \n", "350.0 | \n", "165.0 | \n", "3693.0 | \n", "11.5 | \n", "70 | \n", "1 | \n", "
| 2 | \n", "18.0 | \n", "8 | \n", "318.0 | \n", "150.0 | \n", "3436.0 | \n", "11.0 | \n", "70 | \n", "1 | \n", "
| 3 | \n", "16.0 | \n", "8 | \n", "304.0 | \n", "150.0 | \n", "3433.0 | \n", "12.0 | \n", "70 | \n", "1 | \n", "
| 4 | \n", "17.0 | \n", "8 | \n", "302.0 | \n", "140.0 | \n", "3449.0 | \n", "10.5 | \n", "70 | \n", "1 | \n", "