{ "cells": [ { "cell_type": "markdown", "metadata": { "cell_id": "8f13e2c8-3b45-4d24-a34f-34b8c38db1af", "deepnote_cell_height": 418.375, "deepnote_cell_type": "markdown", "tags": [] }, "source": [ "# Week 7, Tuesday Discussion\n", "\n", "__Today:__\n", " - Pass back Quiz #3\n", " - Review for Quiz #4\n", " - Take Quiz #4\n", "\n", "__Reminders and Announcements:__\n", "- Homework #5 due tonight 11:59pm (don't forget about the opportunity for bonus points!)\n", "- This Thursday we will spend the first half of discussion working on Homework #6 _all together_; this will give you a head start on the homework, so be sure to join!\n", "- Midterm #2 next week during Thursday discussion\n", " - There will be no quiz Tuesday of Week 8, instead we will review for the midterm\n", " - I will pass out notecards during Thursday discussion this week" ] }, { "cell_type": "markdown", "metadata": { "cell_id": "094abe9dbb2d4c4daa8f07c06bc5b2bf", "deepnote_cell_height": 161.59375, "deepnote_cell_type": "markdown", "tags": [] }, "source": [ "__Question 1:__ \n", "\n", "Suppose `df` is the DataFrame shown below. Describe in words what the result of the following code will be.\n", "\n", "`df[\"Monday\"] = 0`\n", "\n", "\n", "`df.loc[df[\"Date].dt.day_name() == \"Monday\", \"Monday\"] = 1`" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "cell_id": "7f63cef2c5f748238ed7f9486c42a395", "deepnote_cell_height": 467, "deepnote_cell_type": "code", "deepnote_to_be_reexecuted": false, "execution_millis": 26473, "execution_start": 1652143927141, "source_hash": "b01f2b07", "tags": [] }, "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", "
DateFremont Bridge TotalFremont Bridge East SidewalkFremont Bridge West Sidewalk
0Friday12.07.05.0
1Friday7.00.07.0
2Friday1.00.01.0
3Friday6.06.00.0
4Friday6.05.01.0
\n", "
" ], "text/plain": [ " Date Fremont Bridge Total Fremont Bridge East Sidewalk \\\n", "0 Friday 12.0 7.0 \n", "1 Friday 7.0 0.0 \n", "2 Friday 1.0 0.0 \n", "3 Friday 6.0 6.0 \n", "4 Friday 6.0 5.0 \n", "\n", " Fremont Bridge West Sidewalk \n", "0 5.0 \n", "1 7.0 \n", "2 1.0 \n", "3 0.0 \n", "4 1.0 " ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd \n", "\n", "df = pd.read_csv(\"../data/Fremont.csv\")\n", "df[\"Date\"] = pd.to_datetime(df[\"Date\"]).dt.day_name()\n", "df.head()" ] }, { "cell_type": "markdown", "metadata": { "cell_id": "e36569e404a74a47b0e0696f8c00fcb9", "deepnote_cell_height": 473.59375, "deepnote_cell_type": "markdown", "tags": [] }, "source": [ "__Question 2:__\n", "\n", "Imagine we have a set of data points that we want to fit to a polynomial. What would be the potential drawbacks of fitting to a polynomial of a \"large\" degree? Of a \"small\" degree?\n", "\n", "![polynomial](../images/polynomial.png)\n", "\n", "[Source](https://www.educative.io/edpresso/overfitting-and-underfitting)" ] }, { "cell_type": "markdown", "metadata": { "cell_id": "67241eb0929d425ba698ce30d5ba8e34", "deepnote_cell_height": 161.59375, "deepnote_cell_type": "markdown", "tags": [] }, "source": [ "__Question 3:__\n", "\n", "Imagine you are given the code below. What would be the result of this afterwards? \n", "\n", "`df2 = pd.DataFrame({\"X\":[1,2],\"Y\":[3,4],\"Z\":[5,6]})`\n", "\n", "`reg.predict(df2)`" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cell_id": "c3cf8ace326c4432ac9c08a74316d92b", "deepnote_cell_height": 81, "deepnote_cell_type": "code", "deepnote_to_be_reexecuted": false, "execution_millis": 2, "execution_start": 1652144742284, "source_hash": "5ab615ea", "tags": [] }, "outputs": [], "source": [ "df = pd.DataFrame({\"W\":[1,9,17,12],\"X\":[4,1,3,3],\"Y\":[1,6,7,23],\"Z\":[9,1,1,19]})" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cell_id": "4825a53753bc4253a9d75ad0be3eb545", "deepnote_cell_height": 154.1875, "deepnote_cell_type": "code", "deepnote_output_heights": [ 21.1875 ], "deepnote_to_be_reexecuted": false, "execution_millis": 18, "execution_start": 1652144743606, "source_hash": "84526317", "tags": [] }, "outputs": [ { "data": { "text/plain": [ "LinearRegression()" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from sklearn.linear_model import LinearRegression\n", "reg = LinearRegression()\n", "reg.fit(df[[\"X\",\"Y\",\"Z\"]],df[\"W\"])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cell_id": "793e3a66958f40c7826b7a1a833622a6", "deepnote_cell_height": 193.78125, "deepnote_cell_type": "code", "deepnote_output_heights": [ 78.78125 ], "deepnote_to_be_reexecuted": false, "execution_millis": 25, "execution_start": 1652144802402, "source_hash": "48527e00", "tags": [] }, "outputs": [ { "data": { "text/plain": [ "X 3.346939\n", "Y 1.306122\n", "Z -1.438776\n", "dtype: float64" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "coefs = pd.Series(reg.coef_, [\"X\",\"Y\",\"Z\"])\n", "coefs" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cell_id": "344716628cb0424d99fd90b5df538145", "deepnote_cell_height": 118.1875, "deepnote_cell_type": "code", "deepnote_output_heights": [ 21.1875 ], "deepnote_to_be_reexecuted": false, "execution_millis": 8, "execution_start": 1652144749138, "owner_user_id": "e590e996-b52f-4e48-8e79-35525bbb42f6", "source_hash": "1661cbe7", "tags": [] }, "outputs": [ { "data": { "text/plain": [ "-0.7448979591836853" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "reg.intercept_" ] } ], "metadata": { "deepnote": {}, "deepnote_execution_queue": [], "deepnote_notebook_id": "3cfdd324-0272-41c0-a702-964521ced753", "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.7.12" } }, "nbformat": 4, "nbformat_minor": 4 }