\n",
" WARNING: Do NOT add any cells to this Jupyter Notebook, because that will crash the autograder.\n",
"
\n",
"\n",
"
\n",
" WARNING: Do NOT implement any additional libraries into this workbook.\n",
"
\n",
"\n",
"All instructions, code comments, etc. in this notebook **are part of the assignment instructions**. That is, if there is instructions about completing a task in this notebook, that task is not optional. \n",
"\n",
"
\n",
" You must implement the following functions in this notebook to receive credit.\n",
"
\n",
"\n",
"`user()`\n",
"\n",
"`long_trips()`\n",
"\n",
"`manhattan_trips()`\n",
"\n",
"`weighted_profit()`\n",
"\n",
"`final_output()`\n",
"\n",
"Each method will be auto-graded using different sets of parameters or data, to ensure that values are not hard-coded. You may assume we will only use your code to work with data from the NYC-TLC dataset during auto-grading.\n",
"\n",
"
\n",
" WARNING: Do NOT remove or modify the following utility functions:\n",
"
\n",
" Do not change the below cell. Run it to initialize your PySpark instance. If you don't get any output, make sure your Notebook's Kernel is set to \"PySpark\" in the top right corner.\n",
"
\n",
" WARNING: Do NOT remodify the below cell. It contains the function for loading data and all imports, and the function for running your code.\n",
"
"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#export\n",
"from pyspark.sql.functions import col\n",
"from pyspark.sql import *"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#### DO NOT CHANGE ANYTHING IN THIS CELL ####\n",
"\n",
"def load_data(size='small'):\n",
" # Loads the data for this question. Do not change this function.\n",
" # This function should only be called with the parameter 'small' or 'large'\n",
" \n",
" if size != 'small' and size != 'large':\n",
" print(\"Invalid size parameter provided. Use only 'small' or 'large'.\")\n",
" return\n",
" \n",
" input_bucket = \"s3://cse6242-fall2021\"\n",
" \n",
" # Load Trip Data\n",
" trip_path = '/'+size+'/yellow_tripdata*'\n",
" trips = spark.read.csv(input_bucket + trip_path, header=True, inferSchema=True)\n",
" print(\"Trip Count: \",trips.count()) # Prints # of trips (# of records, as each record is one trip)\n",
" \n",
" # Load Lookup Data\n",
" lookup_path = '/'+size+'/taxi*'\n",
" lookup = spark.read.csv(input_bucket + lookup_path, header=True, inferSchema=True)\n",
" \n",
" return trips, lookup\n",
"\n",
"def main(size, bucket):\n",
" # Runs your functions implemented above.\n",
" \n",
" print(user())\n",
" trips, lookup = load_data(size=size)\n",
" trips = long_trips(trips)\n",
" mtrips = manhattan_trips(trips, lookup)\n",
" wp = weighted_profit(trips, mtrips)\n",
" final = final_output(wp,lookup)\n",
" \n",
" # Outputs the results for you to visually see\n",
" final.show()\n",
" \n",
" # Writes out as a CSV to your bucket.\n",
" final.write.csv(bucket)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Implement the below functions for this assignment:\n",
"
\n",
" WARNING: Do NOT change any function inputs or outputs, and ensure that the dataframes your code returns align with the schema definitions commented in each function\n",
"
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3a. [1 pt] Update the `user()` function\n",
"This function should return your GT username, eg: gburdell3"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#export\n",
"def user():\n",
" # Returns a string consisting of your GT username.\n",
" return 'gburdell3'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3b. [2 pts] Update the `long_trips()` function\n",
"This function filters trips to keep only trips greater than or equal to 2 miles."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#export\n",
"def long_trips(trips):\n",
" # Returns a Dataframe with Schema the same as :trips:\n",
" pass"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3c. [6 pts] Update the `manhattan_trips()` function\n",
"\n",
"This function determines the top 20 locations with a `DOLocationID` in manhattan by passenger_count (pcount).\n",
"\n",
"Example output formatting:\n",
"\n",
"```\n",
"+--------------+--------+\n",
"| DOLocationID | pcount |\n",
"+--------------+--------+\n",
"| 5| 15|\n",
"| 16| 12| \n",
"+--------------+--------+\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#export\n",
"def manhattan_trips(trips, lookup):\n",
" # Returns a Dataframe with Schema: DOLocationID, pcount\n",
" pass"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3d. [6 pts] Update the `weighted_profit()` function\n",
"This function should determine the average `total_amount`, the total count of trips, and the total count of trips ending in the top 20 destinations and return the `weighted_profit` as discussed in the homework document.\n",
"\n",
"Example output formatting:\n",
"```\n",
"+--------------+-------------------+\n",
"| PULocationID | weighted_profit |\n",
"+--------------+-------------------+\n",
"| 18| 33.784444421924436| \n",
"| 12| 21.124577637149223| \n",
"+--------------+-------------------+\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#export\n",
"def weighted_profit(trips, mtrips): \n",
" # Returns a Dataframe with Schema: PULocationID, weighted_profit\n",
" # Note: Use decimal datatype for weighted profit (NOTE: DON'T USE FLOAT)\n",
" # Our grader will be only be checking the first 8 characters for each value in the dataframe\n",
" pass"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3e. [5 pts] Update the `final_output()` function\n",
"This function will take the results of `weighted_profit`, links it to the `borough` and `zone` and returns the top 20 locations with the highest `weighted_profit`.\n",
"\n",
"Example output formatting:\n",
"```\n",
"+------------+---------+-------------------+\n",
"| Zone | Borough | weighted_profit |\n",
"+----------------------+-------------------+\n",
"| JFK Airport| Queens| 16.95897820117925|\n",
"| Jamaica| Queens| 14.879835188762488|\n",
"+------------+---------+-------------------+\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#export\n",
"def final_output(calc, lookup): \n",
" # Returns a Dataframe with Schema: Zone, Borough, weighted_profit\n",
" # Note: Use decimal datatype for weighted profit (NOTE: DON'T USE FLOAT)\n",
" # Our grader will be only be checking the first 8 characters for each value in the dataframe\n",
" pass"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
" Test your code on the small dataset first, as the large dataset will take a significantly longer time to run\n",
"
\n",
"\n",
"
\n",
" WARNING: Do NOT use the same bucket url for multiple runs of the `main()` function, as this will cause errors. Make sure to change the name of your output location every time. (ie: s3://cse6242-gburdell3/output-large2)\n",
"
\n",
"\n",
"Update the below cell with the path to your bucket, then run the below cell to run your code to store the results in S3.\n",
"\n",
"When you have tested the code with the small dataset, run it again using the large dataset. Your large output file will appear in a folder in your s3 bucket called YOUROUTPUT.csv as a csv file with a name something like part-0000-4d992f7a-0ad3-48f8-8c72-0022984e4b50-c000.csv. Download this file and rename it to q3_output.csv for submission. Do not make any other changes to the file. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# update your bucket path\n",
"bucket = 's3://cse6242-gburdell3/output-large'\n",
"main('large', bucket)\n",
"\n",
"# bucket = 's3://cse6242-gburdell3/output-small'\n",
"# main('small', bucket)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Testing\n",
"\n",
"
\n",
" You may use the below cell for any additional testing you need to do, however any code implemented below will not be run or used when grading\n",
"