Skip to content
Snippets Groups Projects
Commit ccddf6d2 authored by Christoph Lange's avatar Christoph Lange
Browse files

add some cross validation data

parent 7b357ecc
No related branches found
No related tags found
1 merge request!14Predict glucose directly
Source diff could not be displayed: it is too large. Options to address this: view the blob.
Source diff could not be displayed: it is too large. Options to address this: view the blob.
Source diff could not be displayed: it is too large. Options to address this: view the blob.
%% Cell type:markdown id: tags:
## Setup
%% Cell type:code id: tags:
``` python
%load_ext autoreload
%autoreload 2
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))
import os
import datetime
import functools
import IPython
import IPython.display
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
import sklearn
import tensorflow as tf
import tensorflow_probability as tfp
import glucose_ts
# plot layouts
sns.set_style("whitegrid")
sns.set_context("notebook", font_scale=2, rc={"lines.linewidth": 3.5})
sns.color_palette("dark")
plt.subplots_adjust(wspace=1.5)
DATA_DIR = os.path.join(os.path.dirname(os.path.dirname(glucose_ts.__file__)), 'data')
```
%% Output
The autoreload extension is already loaded. To reload it, use:
%reload_ext autoreload
%% Cell type:markdown id: tags:
## Load and Normalize the data
%% Cell type:code id: tags:
``` python
glucose_series = sorted(
[
glucose_ts.data.read_glucose_ts(os.path.join(DATA_DIR, file))
for file in os.listdir(DATA_DIR)
if file.startswith('Data')
] + [
glucose_ts.data.read_glucose_ts(os.path.join(DATA_DIR, 'batch_samples', file))
for file in os.listdir(
os.path.join(
DATA_DIR,
'batch_samples'
)
)
] + [
glucose_ts.data.read_glucose_ts(os.path.join(DATA_DIR, 'calibration', file))
for file in os.listdir(
os.path.join(
DATA_DIR,
'calibration'
)
)
],
key=lambda one: one.real_concentration
)
callibration_curves = sorted(
[
glucose_ts.data.read_glucose_ts(os.path.join(DATA_DIR, 'calibration', file))
for file in os.listdir(
os.path.join(
DATA_DIR,
'calibration'
)
)
],
key=lambda one: one.real_concentration
)
```
%% Cell type:code id: tags:
``` python
import math
from sklearn import metrics
cut_off = 150
voltage_differences = np.array([
one_series.voltages[0] - one_series.voltages[cut_off]
for one_series in callibration_curves
])
concentrations = np.array([
one_series.real_concentration
for one_series in callibration_curves
])
fig, ax = plt.subplots(1, 1, figsize=(10, 10))
ax.scatter(
voltage_differences,
concentrations,
)
model = glucose_ts.models.ExpGlucose().fit(
voltage_differences[:, np.newaxis],
concentrations)
ax.plot(
np.linspace(0., 0.08, 100),
model.predict(np.linspace(0., 0.08, 100))
)
(
math.sqrt(
sklearn.metrics.mean_squared_error(
concentrations[voltage_differences < 0.06],
model.predict(voltage_differences[voltage_differences < 0.06])
)
),
math.sqrt(
sklearn.metrics.mean_squared_error(
concentrations[voltage_differences < 0.1],
model.predict(voltage_differences[voltage_differences < 0.1])
)
)
)
```
%% Output
(0.36360436836630533, 0.9870690512191114)
%% Cell type:code id: tags:
``` python
sklearn.metrics.SCORERS.keys()
```
%% Output
dict_keys(['explained_variance', 'r2', 'max_error', 'neg_median_absolute_error', 'neg_mean_absolute_error', 'neg_mean_absolute_percentage_error', 'neg_mean_squared_error', 'neg_mean_squared_log_error', 'neg_root_mean_squared_error', 'neg_mean_poisson_deviance', 'neg_mean_gamma_deviance', 'accuracy', 'top_k_accuracy', 'roc_auc', 'roc_auc_ovr', 'roc_auc_ovo', 'roc_auc_ovr_weighted', 'roc_auc_ovo_weighted', 'balanced_accuracy', 'average_precision', 'neg_log_loss', 'neg_brier_score', 'adjusted_rand_score', 'rand_score', 'homogeneity_score', 'completeness_score', 'v_measure_score', 'mutual_info_score', 'adjusted_mutual_info_score', 'normalized_mutual_info_score', 'fowlkes_mallows_score', 'precision', 'precision_macro', 'precision_micro', 'precision_samples', 'precision_weighted', 'recall', 'recall_macro', 'recall_micro', 'recall_samples', 'recall_weighted', 'f1', 'f1_macro', 'f1_micro', 'f1_samples', 'f1_weighted', 'jaccard', 'jaccard_macro', 'jaccard_micro', 'jaccard_samples', 'jaccard_weighted'])
%% Cell type:code id: tags:
``` python
from sklearn import model_selection
predictions = model_selection.cross_val_predict(
glucose_ts.models.ExpGlucose(),
voltage_differences[:, np.newaxis],
concentrations,
# scoring=['r2', 'neg_mean_squared_error'],
cv=len(concentrations)
)
math.sqrt(
sklearn.metrics.mean_squared_error(
concentrations[voltage_differences < 0.06],
predictions[voltage_differences < 0.06]
)
)
```
%% Output
0.4407224877053771
presentations/results_outline/images/sensor_behavior_over_time.png

782 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment