{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Hands-on exercise\n", "\n", "In this simple example you are required to perform a simple linear regression with scipy. Find all the information on the function in the documentation: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.linregress.html" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Assignment\n", "\n", "1) Load the provided .csv file with the used car data\n", "\n", "2) Use a linear regression to estimate the car prices from the year, kilometers or engine power. You can make a simple 1D regression from each one of the parameters independently (as an optional task you can also try a 2D or 3D regression combining multiple cues)\n", "\n", "3) Firstly perform the estimation using the scipy linregress function (or alternatively you can use the sklearn.linear_model.LinearRegression class).\n", "NB: check the documentation of the two methods!! In particular be aware of the number of outputs (in case use \"_\" to avoid the return of a specific output).\n", "\n", "4) Have a look at the correlation coefficient to see which of the 3 features works better\n", "\n", "5) Then implement the least square algorithm: you should get exactly the same solution of linregress !\n", "\n", "6) Plot the data and the lines representing the output of the linregress and least square algorithms\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import csv\n", "from scipy import stats\n", "import numpy as np\n", "import sklearn as sl\n", "from sklearn import linear_model" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Load the provided data file with the used car data (you can also have a look at it with any text editor)\n", "\n", "filename = \"data/km_year_power_price.csv\"\n", "lines = csv.reader(open(filename, newline=''), delimiter=',')\n", "\n", "# place your loading code here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use linear regression to estimate the car prices from the year, kilometers or engine power. \n", "You can make a simple 1D regression from each one of the parameters independently \n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# linear regression with linregress (estimate price from year)\n", "\n", "# your code....." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# (Optional) linear regression with linear_model.LinearRegression() (estimate price from year)\n", "# Recall that in Python a mx1 matrix is different from a 1D array -> need to reshape\n", "\n", "# your code....." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "# (Optional) perform linear regression with a manually implemented least squares (estimate price from year)\n", "# You should get exactly the same solution of linregress !\n", "\n", "# your code....." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Plot the data and the lines representing the output of the linregress and least square algorithms\n", "\n", "# your code....\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# linear regression with linregress (estimate price from power)\n", "\n", "# your code....." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# linear regression with linregress (estimate price from km)\n", "\n", "# your code..." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Have a look at the correlation coefficients to see which of the 3 features works better\n", "\n", "# your code......" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# (Optional) 2D linear regression with linear model (estimate price from year and power)\n", "\n", "\n", "# your code......\n" ] } ], "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.8.5" } }, "nbformat": 4, "nbformat_minor": 2 }