{ "cells": [ { "cell_type": "markdown", "id": "7ccd7951", "metadata": {}, "source": [ "(chapter13_part1)=\n", "\n", "# Evaluation Metrics for Regression\n", "\n", "- This is a supplement material for the [Machine Learning Simplified](https://themlsbook.com) book. It sheds light on Python implementations of the topics discussed while all detailed explanations can be found in the book. \n", "- I also assume you know Python syntax and how it works. If you don't, I highly recommend you to take a break and get introduced to the language before going forward with my code. \n", "- This material can be downloaded as a Jupyter notebook (Download button in the upper-right corner -> `.ipynb`) to reproduce the code and play around with it.\n", "\n", "\n", "This notebook is a supplement for *Chapter 13. Model Evaluation* of **Machine Learning For Everyone** book.\n", "\n", "\n", "## 1. Required Libraries\n", "\n", "This block imports all necessary libraries." ] }, { "cell_type": "code", "execution_count": 1, "id": "34a1aebd", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from sklearn.datasets import make_regression\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.linear_model import LinearRegression\n", "from sklearn.metrics import mean_squared_error, r2_score" ] }, { "cell_type": "markdown", "id": "89646b9d", "metadata": {}, "source": [ "## 2. Univariate Regression\n", "\n", "Generate synthetic data for Univariate Regression using `sklearn.datasets.make_regression`" ] }, { "cell_type": "code", "execution_count": 2, "id": "85f3c2c8", "metadata": {}, "outputs": [], "source": [ "# Univariate Regression: 1 feature\n", "X_uni, y_uni = make_regression(n_samples=100, n_features=1, noise=0.1, random_state=42)\n", "X_train_uni, X_test_uni, y_train_uni, y_test_uni = train_test_split(X_uni, y_uni, test_size=0.2, random_state=42)" ] }, { "cell_type": "markdown", "id": "7fb0ceb8", "metadata": {}, "source": [ "Next step is to train a regression model (`LinearRegression`)" ] }, { "cell_type": "code", "execution_count": 3, "id": "acb5fd51", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
LinearRegression()In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
LinearRegression()
LinearRegression()In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
LinearRegression()