diff --git a/Automotive_data.ipynb b/Automotive_data.ipynb new file mode 100644 index 0000000..7e9854d --- /dev/null +++ b/Automotive_data.ipynb @@ -0,0 +1,240 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Case Study: GIS Map Visualization\n", + "\n", + "**Objective:** Visualize the speed and elevation on a map. Use Pandas to import the data and prepare the map. Geographic Information Systems (GIS) are an important technology to view spatially distributed data. The GIS maps can help to identify factors related to location.\n", + "\n", + "Programming for Engineers: [Automotive Data](https://www.apmonitor.com/pds/index.php/Main/AutomotiveData)\n", + "\n", + "- [Course Overview](https://apmonitor.com/che263)\n", + "- [Course Schedule](https://apmonitor.com/che263/index.php/Main/CourseSchedule)\n", + "\n", + "Additional data sets and case studies are available on the [Machine Learning for Engineers course](https://apmonitor.com/pds/index.php/Main/AutomotiveMonitoring).\n", + "\n", + "" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Import Packages" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "import numpy as np\n", + "import matplotlib.pyplot as plt" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Import Data and View Columns\n", + "\n", + "Import data, set time index, and print data columns" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "url = 'http://apmonitor.com/pds/uploads/Main/auto_trip.zip'\n", + "data = pd.read_csv(url)\n", + "data.head()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# reduce to every 100th row\n", + "data = data[::100]\n", + "len(data)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# reset row index\n", + "data.reset_index(inplace=True,drop=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# delete column\n", + "del data['Unnamed: 41']" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# set time index\n", + "data['time'] = pd.to_datetime(data['time'])\n", + "data = data.set_index('time')\n", + "data.sample(5)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Fill In Missing Data" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# fill in NaNs - forward fill\n", + "data.fillna(method='ffill',inplace=True)\n", + "# fill in NaNs - backward fill\n", + "data.fillna(method='bfill',inplace=True)\n", + "data.describe()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Plot Data" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "data.plot(subplots=True,figsize=(10,30))\n", + "plt.show()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### View GPS Points on Map" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Plot the longitude and latitude on a `matplotlib` plot." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x = data['Longitude'].values\n", + "y = data['Latitude'].values\n", + "\n", + "# plot data\n", + "plt.plot(x,y,'r-')\n", + "plt.show()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Display GPS with Plotly Express" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "scrolled": true + }, + "outputs": [], + "source": [ + "import plotly.express as px\n", + "\n", + "df = px.data.carshare()\n", + "fig = px.scatter_mapbox(data, lat=\"Latitude\", lon=\"Longitude\", \\\n", + " color=\"Vehicle speed (mph)\", \\\n", + " size=\"Altitude (GPS) (feet)\", \\\n", + " color_continuous_scale=px.colors.cyclical.IceFire, \\\n", + " size_max=5, zoom=6)\n", + "fig.update_layout(\n", + " mapbox_style=\"open-street-map\",\n", + " margin={\"r\": 0, \"t\": 0, \"l\": 0, \"b\": 0})\n", + "fig.show()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Challenge Problem\n", + "\n", + "Perform a similar analysis but with [new data](http://apmonitor.com/pds/uploads/Main/auto_iowa.txt) with a different OBD-II connector and vehicle.\n", + "\n", + "```python\n", + "url = http://apmonitor.com/pds/uploads/Main/auto_iowa.txt\n", + "```\n", + "\n", + "The data is taken from Iowa so elevation changes are not significant. Select a new value besides elevation to include on the map to adjust the size of the data points." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "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.8.5" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/HW13.ipynb b/HW13.ipynb index c531946..95e0057 100644 --- a/HW13.ipynb +++ b/HW13.ipynb @@ -173,7 +173,7 @@ " \n", " import yaml\n", " with open(\"thermoData.yaml\") as yfile : \n", - " yfile = yaml.load(yfile)\n", + " yfile = yaml.safe_load(yfile)\n", "\n", "\n", "* Also in ```__init__``` Make two arrays that are members of the class called a_lo, and a_hi.\n", diff --git a/HW14.ipynb b/HW14.ipynb index bb7179f..b1369f8 100644 --- a/HW14.ipynb +++ b/HW14.ipynb @@ -157,7 +157,7 @@ " self.Rgas = 8314.46 # J/kmol*K\n", " self.M = MW\n", " with open(\"thermoData.yaml\") as yfile : \n", - " yfile = yaml.load(yfile)\n", + " yfile = yaml.safe_load(yfile)\n", " self.a_lo = yfile[species][\"a_lo\"]\n", " self.a_hi = yfile[species][\"a_hi\"]\n", " self.T_lo = 300.\n", diff --git a/Import_Export_Data.ipynb b/Import_Export_Data.ipynb new file mode 100644 index 0000000..5f40d32 --- /dev/null +++ b/Import_Export_Data.ipynb @@ -0,0 +1,186 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Import and Export Data with Python\n", + "\n", + "See additional information on working with data in [Begin Python](https://github.com/APMonitor/begin_python) and [Data Science](https://github.com/APMonitor/data_science) Courses.\n", + "\n", + "#### Method 1: Pandas" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
TimeQ1Q2T1T2
000.00.019.9319.25
110.00.019.9319.29
220.00.019.9619.29
330.00.019.9319.29
440.00.019.9319.29
\n", + "
" + ], + "text/plain": [ + " Time Q1 Q2 T1 T2\n", + "0 0 0.0 0.0 19.93 19.25\n", + "1 1 0.0 0.0 19.93 19.29\n", + "2 2 0.0 0.0 19.96 19.29\n", + "3 3 0.0 0.0 19.93 19.29\n", + "4 4 0.0 0.0 19.93 19.29" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import pandas as pd\n", + "url = 'http://apmonitor.com/pdc/uploads/Main/tclab_data2.txt'\n", + "data = pd.read_csv(url)\n", + "data.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "data.to_csv('file.csv')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Method 2: Numpy" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[ 0. 0. 0. 0. 19.93 19.25]\n", + " [ 1. 1. 0. 0. 19.93 19.29]\n", + " [ 2. 2. 0. 0. 19.96 19.29]\n", + " ...\n", + " [598. 598. 50. 80. 68.82 50.64]\n", + " [599. 599. 50. 80. 68.92 50.42]\n", + " [600. 600. 50. 80. 68.75 50.51]]\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "data = np.loadtxt('file.csv',delimiter=',',skiprows=1)\n", + "print(data)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "np.savetxt('file2.csv',data,delimiter=',',\\\n", + " comments='',header='Time,Q1,Q2,T1,T2')" + ] + } + ], + "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": 4 +}