Commit 6c436c74 authored by Katharine Jarmul's avatar Katharine Jarmul
Browse files

adding notebooks and solutions

parent 4c4c5b91
Loading
Loading
Loading
Loading
+1383 −0

File added.

Preview size limit exceeded, changes collapsed.

+1754 −0

File added.

Preview size limit exceeded, changes collapsed.

+328 −0

File added.

Preview size limit exceeded, changes collapsed.

+211 −0
Changes for notebooks/04 - Case Study.ipynb: 211 added lines, 0 removed lines.
Original line number Diff line number Diff line
%% Cell type:markdown id: tags:

# 04. Case Study - Privacy in Practice

In this notebook, we'll explore the possibilties for data privacy on a new dataset. In this notebook, you'll be asked to work in small groups, so make friends with someone seated near you.

Your challenge is that you are working with a health care provider, who would like to do the "machine learning" on this dataset to figure out if there are preventative measures that can be taken so fewer patients are seen in the hospital for related care or so that their visits are shorter. The goal is that more potentially affected patients are given access to primary care physicians and regular medication or visits that can keep them out of the hostpital for long stays. This study is focused on blood-sugar related illnesses, but not only diabetes.

Using this dataset, we'll walk through a few possible scenarios and apply what we have learned today about data privacy to this new use case.

%% Cell type:markdown id: tags:

## Part One: Determining What's Useful and What's Sensitive

- Data completeness
- Potential sensitive columns
- Potential useful features
- What should we use (or not use)? Why?

%% Cell type:code id: tags:

``` python
%matplotlib inline
import pandas as pd

df = pd.read_csv('../data/health_data.csv')
```

%% Cell type:code id: tags:

``` python
df.head()
```

%% Cell type:code id: tags:

``` python
from datetime import datetime
df.admitted_ts = df.admitted_ts.map(lambda x: datetime.strptime(x, '%Y-%m-%d %H:%M:%S') + timedelta(days=-8*12*30) if datetime.strptime(x, '%Y-%m-%d %H:%M:%S').year > 2018 else x)
```

%% Cell type:code id: tags:

``` python
df.to_csv('../data/health_data.csv', index=False)
```

%% Cell type:code id: tags:

``` python
df.info()
```

%% Cell type:code id: tags:

``` python
df.describe()
```

%% Cell type:code id: tags:

``` python
# if you'd like, continue to look at column distributions, values or feel free to plot a few which are of interest to you.
```

%% Cell type:markdown id: tags:

## Discussion:

- What columns should we use?
- Which ones should we remove?
- Are there columns which we should protect but not remove?

For each, we need some justification or thought!

%% Cell type:code id: tags:

``` python
cols_to_drop = ['id', 'patient_name', 'ssn']

df = df.drop(columns=cols_to_drop)
```

%% Cell type:code id: tags:

``` python
df.columns
```

%% Cell type:markdown id: tags:

## Part Two: Determining the Approach for Protecting the Columns

- Scenario One: You are an employee of the company which produced this data. You have full access and can use full variables for building your model; however, you want to respect privacy and ensure your model is valuable without leaking private information.

- Scenario Two: You are the database manager at the health care provider asked to prepare the data to send to a machine learning consultant who will help give you a more detailed analysis. The consultant has signed all the necessary NDAs, but you have instructions to keep the private or potentially sensitive data to a minimum.

- Scenario Three: You suggested releasing the dataset to Kaggle. It will be uploaded so hundreds of Kagglers can participate. For the sake of avoiding a long legal argument, let's say all patients were part of a study in which they signed a waiver that their records could be released publicly. That said, you still want to avoid a PR nightmare and protect the data as much as possible.

Based on the scenario for your team above, what do you do?

%% Cell type:code id: tags:

``` python
# any investigation code to see what approach you might use
```

%% Cell type:code id: tags:

``` python
```

%% Cell type:code id: tags:

``` python
df.has_diabetes.value_counts()
```

%% Cell type:code id: tags:

``` python
df.private_insurance.value_counts()
```

%% Cell type:code id: tags:

``` python
df.no_primary_dr.value_counts()
```

%% Cell type:code id: tags:

``` python
df.corr()['no_primary_dr']
```

%% Cell type:code id: tags:

``` python
df.marital_status.value_counts()
```

%% Cell type:code id: tags:

``` python
df.age.hist(bins=70)
```

%% Cell type:markdown id: tags:

### Discussion

- What methods will be most effective in the scenario you have?
- Have you considered potential data leakage within the *non-sensitive* columns?
- Is there other sensitive or secret data we should address given the scenario?

%% Cell type:code id: tags:

``` python
```

%% Cell type:markdown id: tags:

### Step Three: Implement Data Protection for the Dataset

Now it's time to code! Feel free to utilize code from the previous notebooks to implement protection of at least two of the columns you chose as sensitive. Are there ways to make these applications more Pandas-friendly or easy to use?

%% Cell type:markdown id: tags:


## Exercise

**Implement protection for the columns you are keeping -- you may use code from previous notebooks in this workshop. If you'd like to use our API, you can check out the documentation at: https://docs.kiprotect.com **

%% Cell type:code id: tags:

``` python
# %load ../solutions/case-study/possible-solution.py
```

%% Cell type:code id: tags:

``` python
protected_df.head()
```

%% Cell type:markdown id: tags:

## Exercise

**Use a randomized response scheme to protect the values of the **no_primary_dr** and **has_diabetes** columns of the dataset.**

%% Cell type:code id: tags:

``` python
# %load "../solutions/case-study/dp.py"
```

%% Cell type:markdown id: tags:

### Discussion:

- What was difficult to decide and implement?
- How might this relate to real problems in machine learning with sensitive data?
- Does this apply to your work? How? What can you take away?

%% Cell type:code id: tags:

``` python
protected_df.to_csv('../data/health_data_protected.csv', index=False)
```
+534 −0

File added.

Preview size limit exceeded, changes collapsed.

Loading