{ "cells": [ { "cell_type": "code", "execution_count": 16, "id": "initial_id", "metadata": { "collapsed": true, "ExecuteTime": { "end_time": "2023-11-07T00:07:54.400654Z", "start_time": "2023-11-07T00:07:54.375821Z" } }, "outputs": [], "source": [ "import pandas as pd\n", "import folium\n", "import utils" ] }, { "cell_type": "code", "execution_count": 17, "outputs": [], "source": [ "# Load the data\n", "ListA = pd.read_csv('List A.csv')\n", "ListB = pd.read_csv('List B.csv')\n", "ListC = pd.read_csv('List C.csv')\n", "ListD = pd.read_csv('List D.csv')" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2023-11-07T00:07:54.430515Z", "start_time": "2023-11-07T00:07:54.381537Z" } }, "id": "73b780e762c9de37" }, { "cell_type": "code", "execution_count": 18, "outputs": [], "source": [ "# Create two centroids, one in the North End and one in the Financial District\n", "centroids = [[42.364506, -71.054733], [42.358894, -71.056742]]\n", "\n", "northeastern_coordinate = \"-71.09033,42.33976\"" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2023-11-07T00:07:54.431407Z", "start_time": "2023-11-07T00:07:54.392677Z" } }, "id": "65e208650eb43b4" }, { "cell_type": "code", "execution_count": 19, "outputs": [], "source": [ "# Combine the two lists and add a column to indicate the list\n", "ListA['list'] = 'A'\n", "ListB['list'] = 'B'\n", "ListC['list'] = 'C'\n", "ListD['list'] = 'D'\n", "\n", "TotalList = pd.concat([ListA, ListB, ListC])" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2023-11-07T00:07:54.431829Z", "start_time": "2023-11-07T00:07:54.397279Z" } }, "id": "ffe4025e97a6c6b9" }, { "cell_type": "code", "execution_count": 20, "outputs": [], "source": [ "# Remove all columns but name and gps\n", "TotalList = TotalList[['name', 'gps', 'list']]" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2023-11-07T00:07:54.432180Z", "start_time": "2023-11-07T00:07:54.401907Z" } }, "id": "72657779b4484aae" }, { "cell_type": "code", "execution_count": 21, "outputs": [], "source": [ "# Convert the gps column to a list of lists for k-means\n", "TotalList['gps'] = TotalList['gps'].apply(lambda x: x.strip('[]').split(','))\n", "TotalList['gps'] = TotalList['gps'].apply(lambda x: [float(i) for i in x])" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2023-11-07T00:07:54.432238Z", "start_time": "2023-11-07T00:07:54.405216Z" } }, "id": "a157ffaec020a29a" }, { "cell_type": "code", "execution_count": 22, "outputs": [ { "data": { "text/plain": " name gps list \\\n0 521 Commercial Street #525 [42.3688272, -71.0553792] A \n1 Acorn St [42.3576234, -71.0688746] A \n2 Arlington's Great Meadows [42.4299758, -71.2038948] A \n3 Arthur Fiedler Statue [42.3565057, -71.0754527] A \n4 BU Beach [42.3511927, -71.1060828] A \n.. ... ... ... \n28 The Clam Box [42.2763168, -71.0092883] C \n29 The Partisans [42.3478375, -71.0404428] C \n30 Union Oyster House [42.361288, -71.056908] C \n31 Victoria's Diner [42.3270498, -71.0667744] C \n32 Wollaston Beach [42.2806539, -71.0119933] C \n\n normalized_gps \n0 [0.7251058917247415, 0.8141430878559053] \n1 [0.6747391031099019, 0.778052752104061] \n2 [1.0, 0.41697235794883575] \n3 [0.6697144722136962, 0.7604611403245493] \n4 [0.6458298305822171, 0.6785480000609988] \n.. ... \n28 [0.30922451563130937, 0.9374025730216268] \n29 [0.6307464973238023, 0.8540870458656248] \n30 [0.6912133469876947, 0.8100546647415456] \n31 [0.5372951958288665, 0.7836692527743693] \n32 [0.32872198960456106, 0.9301686741961767] \n\n[131 rows x 4 columns]", "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 \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
namegpslistnormalized_gps
0521 Commercial Street #525[42.3688272, -71.0553792]A[0.7251058917247415, 0.8141430878559053]
1Acorn St[42.3576234, -71.0688746]A[0.6747391031099019, 0.778052752104061]
2Arlington's Great Meadows[42.4299758, -71.2038948]A[1.0, 0.41697235794883575]
3Arthur Fiedler Statue[42.3565057, -71.0754527]A[0.6697144722136962, 0.7604611403245493]
4BU Beach[42.3511927, -71.1060828]A[0.6458298305822171, 0.6785480000609988]
...............
28The Clam Box[42.2763168, -71.0092883]C[0.30922451563130937, 0.9374025730216268]
29The Partisans[42.3478375, -71.0404428]C[0.6307464973238023, 0.8540870458656248]
30Union Oyster House[42.361288, -71.056908]C[0.6912133469876947, 0.8100546647415456]
31Victoria's Diner[42.3270498, -71.0667744]C[0.5372951958288665, 0.7836692527743693]
32Wollaston Beach[42.2806539, -71.0119933]C[0.32872198960456106, 0.9301686741961767]
\n

131 rows × 4 columns

\n
" }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Create a new column with normalized gps coordinates and centroids\n", "TotalList['normalized_gps'], norm_centroids = utils.normalize_gps(TotalList['gps'].values.tolist(), centroids)\n", "display(TotalList)" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2023-11-07T00:07:54.432731Z", "start_time": "2023-11-07T00:07:54.412279Z" } }, "id": "a03ebde91b87fa3b" }, { "cell_type": "markdown", "source": [ "# Cluster and Minimize" ], "metadata": { "collapsed": false }, "id": "4bd41be9aca5094b" }, { "cell_type": "code", "execution_count": 23, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/garrinshieh/anaconda3/lib/python3.11/site-packages/sklearn/cluster/_kmeans.py:1412: FutureWarning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress the warning\n", " super()._check_params_vs_input(X, default_n_init=10)\n", "/Users/garrinshieh/anaconda3/lib/python3.11/site-packages/sklearn/cluster/_kmeans.py:1412: RuntimeWarning: Explicit initial center position passed: performing only one init in KMeans instead of n_init=10.\n", " super()._check_params_vs_input(X, default_n_init=10)\n" ] } ], "source": [ "# Cluster and minimize the data\n", "df, route_1_coordinates, route_2_coordinates = utils.cluster_and_minimize(TotalList, centroids, norm_centroids,\n", " northeastern_coordinate, 0.5)" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2023-11-07T00:08:20.577006Z", "start_time": "2023-11-07T00:07:54.416349Z" } }, "id": "ee9b3c1ecb360976" }, { "cell_type": "code", "execution_count": 24, "outputs": [], "source": [ "# Create a JSON request for the API\n", "# This is the data we want to get from the API\n", "route_1 = utils.list_to_string(route_1_coordinates)\n", "route_2 = utils.list_to_string(route_2_coordinates)" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2023-11-07T00:08:20.591584Z", "start_time": "2023-11-07T00:08:20.577492Z" } }, "id": "aa618161182b5b07" }, { "cell_type": "code", "execution_count": 25, "outputs": [], "source": [ "# Create a dataframe from the JSON\n", "df1 = utils.create_json_df(route_1, utils.list_to_string([centroids[0]]), northeastern_coordinate)\n", "df2 = utils.create_json_df(route_2, utils.list_to_string([centroids[1]]), northeastern_coordinate)" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2023-11-07T00:08:22.409355Z", "start_time": "2023-11-07T00:08:20.579890Z" } }, "id": "32c485788eedd94" }, { "cell_type": "code", "execution_count": 26, "outputs": [], "source": [ "# Add columns for the route number\n", "df1['route'] = 1\n", "df2['route'] = 2\n", "\n", "# Concatenate the two dataframes\n", "df = pd.concat([df1, df2], ignore_index=True)" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2023-11-07T00:08:22.425179Z", "start_time": "2023-11-07T00:08:22.412707Z" } }, "id": "49dba1f17ca8337e" }, { "cell_type": "code", "execution_count": 27, "outputs": [ { "data": { "text/plain": " waypoint_index trips_index \\\n0 0 0 \n13 1 0 \n68 2 0 \n40 3 0 \n22 4 0 \n.. ... ... \n14 67 0 \n11 68 0 \n69 69 0 \n19 70 0 \n71 71 0 \n\n hint distance \\\n0 dMQAgDTDAIAuAAAAEgAAAAAAAAAAAAAAiaamQKk960AAAA... 1.113855 \n13 oLwsgCS9LIBHAAAA2AAAAAAAAABgAQAAkQwAQdo1v0EAAA... 2.532529 \n68 CL0sgBS9LIAhAAAAagAAAAAAAAAAAAAAfoF0QPCwOkEAAA... 7.608103 \n40 YbwsgEO9LIBbAAAAEgAAAAAAAAAPAAAA5ua1QcswjkAAAA... 0.468602 \n22 UkAEgFxABIB8AAAAAAAAAAAAAAAYAgAAVjBdQQAAAAAAAA... 6.397300 \n.. ... ... \n14 -mUsgHZmLIATAAAAYgEAAL0AAADpAAAALf8HQHZ8HUK-9a... 55.355565 \n11 43YhgPN2IYA1AAAAJAAAAAAAAAA5AAAAEha0QWgpbEEAAA... 18.896385 \n69 CdQhgB0OA4AYAAAAHgAAADkAAAAAAAAALdMlQdSMQ0Fd0r... 10.970598 \n19 XAAigHIAIoBKAAAASwAAAFUAAABDAQAARGUEQURlBEG2ZR... 11.054154 \n71 DoUhgBeFIYCcAAAAJgAAAAAAAAARAAAAm0CKQdkZiEAAAA... 0.236958 \n\n name location lat lon \\\n0 State Street [-71.056741, 42.358884] -71.056741 42.358884 \n13 [-71.056995, 42.36049] -71.056995 42.360490 \n68 [-71.056994, 42.361263] -71.056994 42.361263 \n40 Creek Square [-71.056819, 42.361534] -71.056819 42.361534 \n22 [-71.059255, 42.359295] -71.059255 42.359295 \n.. ... ... ... ... \n14 [-71.049204, 42.325624] -71.049204 42.325624 \n11 Lucy Street [-71.06221, 42.324934] -71.062210 42.324934 \n69 [-71.066844, 42.327134] -71.066844 42.327134 \n19 [-71.071196, 42.34085] -71.071196 42.340850 \n71 Northeastern (Inbound) [-71.090331, 42.339762] -71.090331 42.339762 \n\n route \n0 2 \n13 2 \n68 2 \n40 2 \n22 2 \n.. ... \n14 2 \n11 2 \n69 2 \n19 2 \n71 2 \n\n[72 rows x 9 columns]", "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 \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 \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 \n \n \n \n
waypoint_indextrips_indexhintdistancenamelocationlatlonroute
000dMQAgDTDAIAuAAAAEgAAAAAAAAAAAAAAiaamQKk960AAAA...1.113855State Street[-71.056741, 42.358884]-71.05674142.3588842
1310oLwsgCS9LIBHAAAA2AAAAAAAAABgAQAAkQwAQdo1v0EAAA...2.532529[-71.056995, 42.36049]-71.05699542.3604902
6820CL0sgBS9LIAhAAAAagAAAAAAAAAAAAAAfoF0QPCwOkEAAA...7.608103[-71.056994, 42.361263]-71.05699442.3612632
4030YbwsgEO9LIBbAAAAEgAAAAAAAAAPAAAA5ua1QcswjkAAAA...0.468602Creek Square[-71.056819, 42.361534]-71.05681942.3615342
2240UkAEgFxABIB8AAAAAAAAAAAAAAAYAgAAVjBdQQAAAAAAAA...6.397300[-71.059255, 42.359295]-71.05925542.3592952
..............................
14670-mUsgHZmLIATAAAAYgEAAL0AAADpAAAALf8HQHZ8HUK-9a...55.355565[-71.049204, 42.325624]-71.04920442.3256242
1168043YhgPN2IYA1AAAAJAAAAAAAAAA5AAAAEha0QWgpbEEAAA...18.896385Lucy Street[-71.06221, 42.324934]-71.06221042.3249342
69690CdQhgB0OA4AYAAAAHgAAADkAAAAAAAAALdMlQdSMQ0Fd0r...10.970598[-71.066844, 42.327134]-71.06684442.3271342
19700XAAigHIAIoBKAAAASwAAAFUAAABDAQAARGUEQURlBEG2ZR...11.054154[-71.071196, 42.34085]-71.07119642.3408502
71710DoUhgBeFIYCcAAAAJgAAAAAAAAARAAAAm0CKQdkZiEAAAA...0.236958Northeastern (Inbound)[-71.090331, 42.339762]-71.09033142.3397622
\n

72 rows × 9 columns

\n
" }, "metadata": {}, "output_type": "display_data" } ], "source": [ "display(df2)" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2023-11-07T00:08:22.440853Z", "start_time": "2023-11-07T00:08:22.424158Z" } }, "id": "f231d9a35358988c" }, { "cell_type": "code", "execution_count": 28, "outputs": [ { "data": { "text/plain": "", "text/html": "
Make this Notebook Trusted to load map: File -> Trust Notebook
" }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Create a map\n", "m = folium.Map(location=[df['lon'].mean(), df['lat'].mean()], zoom_start=11)\n", "\n", "# Add the points and lines for the two routes with different colors\n", "colors = ['red', 'blue']\n", "\n", "for route in df['route'].unique():\n", " df_route = df[df['route'] == route]\n", " folium.PolyLine(df_route[['lon', 'lat']].values.tolist(), color=colors[route - 1]).add_to(m)\n", " for i in range(len(df_route)):\n", " folium.CircleMarker(df_route[['lon', 'lat']].iloc[i].values.tolist(), radius=3, color=colors[route - 1]).add_to(\n", " m)\n", "\n", "# Display the map\n", "m" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2023-11-07T00:08:22.513542Z", "start_time": "2023-11-07T00:08:22.430363Z" } }, "id": "80fd847da2833913" }, { "cell_type": "code", "execution_count": 29, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Route 1 has 61 waypoints\n", "Route 2 has 70 waypoints\n" ] } ], "source": [ "# Get the number of waypoints for each route\n", "route_1_waypoints = len(route_1_coordinates)\n", "route_2_waypoints = len(route_2_coordinates)\n", "print(\"Route 1 has {} waypoints\".format(route_1_waypoints))\n", "print(\"Route 2 has {} waypoints\".format(route_2_waypoints))" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2023-11-07T00:08:22.513689Z", "start_time": "2023-11-07T00:08:22.488854Z" } }, "id": "f53c97acec1c2fc4" }, { "cell_type": "code", "execution_count": 30, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The trip will take 10.36111111111111 hours\n", "The trip will take 10.586666666666666 hours\n" ] } ], "source": [ "trip_hrs_1 = utils.get_trip_time(route_1, route_1_waypoints, utils.list_to_string([centroids[0]]),\n", " northeastern_coordinate)\n", "print(\"The trip will take {} hours\".format(trip_hrs_1))\n", "trip_hrs_2 = utils.get_trip_time(route_2, route_2_waypoints, utils.list_to_string([centroids[1]]),\n", " northeastern_coordinate)\n", "print(\"The trip will take {} hours\".format(trip_hrs_2))" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2023-11-07T00:08:24.460727Z", "start_time": "2023-11-07T00:08:22.491469Z" } }, "id": "a3ec09dfb5cbb5b3" }, { "cell_type": "code", "execution_count": 30, "outputs": [], "source": [], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2023-11-07T00:08:24.471189Z", "start_time": "2023-11-07T00:08:24.460431Z" } }, "id": "eafe5678c44e94fd" } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 5 }