MSE 598-DM Spring 2021

Useful pieces in Python

Google colab

Google colab is a cloud-based Python notebook. We’re planning to use this so that you don’t have to worry about installing software on your computer.

Go to colab.research.google.com. You can use your private google account or you can activate your UIUC one here. If you can’t get to Google where you are, please let us know.

Installing packages

If a package is not installed, you can install it by doing:

!pip install [packagename]

Getting online data

You can grab data

import pandas as pd
df = pd.read_csv("https://covidtracking.com/api/v1/states/daily.csv")

Applying operations to data

def convert_to_datetime(x):
    return str(x)[0:4]+'-'+str(x)[4:6] + '-'+str(x)[6:]
df['date_conv'] = pd.to_datetime([convert_to_datetime(x) for x in df['date']])

Selecting data

df_il = df[df['state']=='X']

Plotting data

import matplotlib.pyplot as plt
plt.plot('x','y',data=df_il)
df_il

Markdown

Hit the +Text button, which creates a markdown cell. Markdown is a simple language which translates human-readable text into pretty HTML. You can use LaTeX to write math; for example

$x^2$

Plot a function

You’ll need these libraries

import numpy as np
import matplotlib.pyplot as plt

Then, to plot

x = np.linspace(0,5,300)
y = x**2
plt.plot(x,y)
plt.xlabel("x")
plt.ylabel("y")
plt.xlim(0,4) # zoom in
pass #this just prevents an ugly printout

Exercise 1

In this exercise, we will plot the total positive test results as function of time.

Sometimes the PDF export is a little fiddly. I notice that it works best in Chrome or Firefox.

Exercise 2