{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "i-6BexFnjcsL", "outputId": "d6644117-ed23-4928-e6b2-c127aaeb17ef" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[107.2087328]\n" ] } ], "source": [ "import pandas\n", "from sklearn import linear_model\n", "\n", "df = pandas.read_csv(\"cars.csv\")\n", "\n", "X = df[['Weight', 'Volume']]\n", "y = df['CO2']\n", "\n", "regr = linear_model.LinearRegression()\n", "regr.fit(X, y)\n", "\n", "#predict the CO2 emission of a car where the weight is 2300kg, and the volume is 1300cm3:\n", "predictedCO2 = regr.predict([[2300, 1300]])\n", "\n", "print(predictedCO2)" ] }, { "cell_type": "markdown", "metadata": { "id": "-lYsAj9gjcsM" }, "source": [ "# Coefficient" ] }, { "cell_type": "markdown", "metadata": { "id": "1HLzpAf_jcsM" }, "source": [ "The coefficient is a factor that describes the relationship with an unknown variable.\n", "In this case, we can ask for the coefficient value of weight against CO2, and for volume against CO2. The answer(s) we get tells us what would happen if we increase, or decrease, one of the independent values." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "vgQVwIY-jcsM", "outputId": "36cb2e3c-2917-472c-8c2e-aa964fc2544b" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0.00755095 0.00780526]\n" ] } ], "source": [ "print(regr.coef_)" ] }, { "cell_type": "markdown", "metadata": { "id": "v8S6hGcgjcsN" }, "source": [ "The result array represents the coefficient values of weight and volume.\n", "\n", "Weight: 0.00755095\n", "Volume: 0.00780526\n", "\n", "These values tell us that if the weight increase by 1kg, the CO2 emission increases by 0.00755095g.\n", "\n", "And if the engine size (Volume) increases by 1 cm3, the CO2 emission increases by 0.00780526 g.\n", "\n", "I think that is a fair guess, but let test it!\n", "\n", "We have already predicted that if a car with a 1300cm3 engine weighs 2300kg, the CO2 emission will be approximately 107g.\n", "\n", "What if we increase the weight with 1000kg (from 2300 to 3300) what will be the CO2 emission?\n", "\n", "Ans: 107.2087328 + (1000 * 0.00755095) = 114.75968" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "zque8bbpjcsN", "outputId": "0ab435a2-e384-459f-dcca-f3486ce717d5" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[114.75968007]\n" ] } ], "source": [ "predictedCO2 = regr.predict([[3300, 1300]])\n", "\n", "print(predictedCO2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "_pp929ZVjcsN" }, "outputs": [], "source": [] } ], "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.6" }, "colab": { "provenance": [], "include_colab_link": true } }, "nbformat": 4, "nbformat_minor": 0 }