In [1]:
# Configure Jupyter so figures appear in the notebook
%matplotlib inline
# Configure Jupyter to display the assigned value after an assignment
%config InteractiveShell.ast_node_interactivity='last_expr_or_assign'
# import functions from the modsim.py module
from modsim import *
In [2]:
data = pd.read_csv('data/glucose_insulin.csv', index_col='time')
Here's what the glucose time series looks like.
In [3]:
plot(data.glucose, 'bo', label='glucose')
decorate(xlabel='Time (min)',
ylabel='Concentration (mg/dL)')
And the insulin time series.
In [4]:
plot(data.insulin, 'go', label='insulin')
decorate(xlabel='Time (min)',
ylabel='Concentration ($\mu$U/mL)')
For the book, I put them in a single figure, using subplot
In [5]:
subplot(2, 1, 1)
plot(data.glucose, 'bo', label='glucose')
decorate(ylabel='Concentration (mg/dL)')
subplot(2, 1, 2)
plot(data.insulin, 'go', label='insulin')
decorate(xlabel='Time (min)',
ylabel='Concentration ($\mu$U/mL)')
savefig('figs/chap17-fig01.pdf')
The return value from interpolate
is a function.
In [6]:
I = interpolate(data.insulin)
We can use the result, I
, to estimate the insulin level at any point in time.
In [7]:
I(7)
I
can also take an array of time and return an array of estimates:
In [8]:
t_0 = get_first_label(data)
t_end = get_last_label(data)
ts = linrange(t_0, t_end, endpoint=True)
I(ts)
type(ts)
Here's what the interpolated values look like.
In [9]:
plot(data.insulin, 'go', label='insulin data')
plot(ts, I(ts), color='green', label='interpolated')
decorate(xlabel='Time (min)',
ylabel='Concentration ($\mu$U/mL)')
savefig('figs/chap17-fig02.pdf')
Exercise: Read the documentation of scipy.interpolate.interp1d
. Pass a keyword argument to interpolate
to specify one of the other kinds of interpolation, and run the code again to see what it looks like.
In [10]:
# Solution goes here
Exercise: Interpolate the glucose data and generate a plot, similar to the previous one, that shows the data points and the interpolated curve evaluated at the time values in ts
.
In [11]:
# Solution goes here
In [12]:
source_code(interpolate)
In [ ]: