Fairness, privacy, and a first look at neural networks
Introduction to Data Science and Machine Learning · Md Mahbub E Noor · University of Barishal
MLPClassifier(hidden_layer_sizes=(64,))
A credit model trained on past approvals can repeat past unfairness.
A health model that works worse for one group puts that group at risk.
A spam or CV filter can hide real messages or good candidates without anyone noticing.
Some groups are under-represented, so the model learns less about them. Recall our 2011 UK spam data.
Labels written by people can carry their past decisions and biases.
A feature may mean different things for different groups, or be missing more often for some.
cross_val_predict gives an honest prediction for every row · Then compare accuracy group by group · Large gaps need explaining before any use · students.csv is invented: this shows the method only
import pandas as pd
from sklearn.compose import make_column_transformer
from sklearn.impute import SimpleImputer
from sklearn.model_selection import cross_val_predict
from sklearn.neighbors import KNeighborsClassifier
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import OneHotEncoder
from sklearn.preprocessing import StandardScaler
st = pd.read_csv("students.csv")
num = ["hours", "attendance", "prior_gpa"]
prep = make_column_transformer(
(make_pipeline(SimpleImputer(strategy="median"),
StandardScaler()), num),
(OneHotEncoder(), ["dept"]))
model = make_pipeline(prep, KNeighborsClassifier())
X = st[num + ["dept"]]
st["pred"] = cross_val_predict(model, X, st["passed"], cv=5)
st["correct"] = st["pred"] == st["passed"]
print(st.groupby("dept")["correct"].mean().round(3))dept CSE 0.911 EEE 0.900 Math 0.867 Name: correct, dtype: float64
If a name or phone number does not help the question, do not collect it.
Delete names, numbers and addresses before analysis. Your SMS homework already did this.
People should know and agree when their data is used. Store it safely and delete it when done.
| Check | Question to ask |
|---|---|
| Baseline | Does it clearly beat a simple guess? |
| Honest test | Was the test set used only once, at the end? |
| Right metric | Which mistake is costly, and is that metric reported? |
| Groups | Does it work similarly for different groups? |
| Data match | Does new data look like the training data (time, place, language)? |
| Privacy | Was the data collected with permission and anonymised? |
Multiplies each input by a weight, adds them up with a bias, and passes the result through a simple function.
Many neurons side by side form a layer; layers stacked together form a network.
Training adjusts the weights a little at a time to reduce the error, like the line search in Lecture 9.
Inputs: hours 6, attendance 0.9, GPA 3.2. Weights 0.5, 2.0, 1.0, bias -5. Output 1 if the total is above 0.
# total = 6×0.5 + 0.9×2.0 # + 3.2×1.0 - 5 # = 3.0 + 1.8 + 3.2 - 5 # = 3.0 # 3.0 > 0, so output 1
| input | value | weight | value × weight |
|---|---|---|---|
| hours | 6 | 0.5 | 3.0 |
| attendance | 0.9 | 2.0 | 1.8 |
| GPA | 3.2 | 1.0 | 3.2 |
| bias | -5 | ||
| total | 3.0 | ||
| output | 1 |
Next row: arrow key or the ▶ button. Predict it first!
WATCH FOR
Silent video: press play, pause any time.
MLPClassifier: a multi-layer perceptron · One hidden layer of 64 neurons · Same fit / score pattern as every model · Compare with k-NN from Practice 3 on the same split
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
X, y = load_digits(return_X_y=True)
X_tr, X_te, y_tr, y_te = train_test_split(
X, y, test_size=0.25, random_state=0, stratify=y)
knn = KNeighborsClassifier(n_neighbors=1)
net = MLPClassifier(hidden_layer_sizes=(64,),
max_iter=1000, random_state=0)
for name, m in [("kNN k=1", knn), ("neural net", net)]:
pipe = make_pipeline(StandardScaler(), m)
pipe.fit(X_tr, y_tr)
print(name, round(pipe.score(X_te, y_te), 3))kNN k=1 0.967 neural net 0.973
WATCH FOR
Silent video: press play, pause any time.
Larger networks for images, speech and text. Libraries: PyTorch, TensorFlow.
Bangla text and speech processing is an open area with real local impact.
Your project. Then public datasets and competitions, always with honest evaluation.
In the lab you can
Opens in a new tab and works offline. All labs: labs/START_HERE.html
cross_val_predict | Honest prediction for every row |
groupby(group)["correct"].mean() | Accuracy per group |
anonymise | Remove names, numbers, addresses |
neuron | Weighted sum + bias, then a function |
MLPClassifier(...) | A small neural network |
checklist | Baseline, honest test, metric, groups, data match, privacy |
Due before the project kickoff · [date]
For the neuron on the slide, find a student (hours, attendance, GPA) for whom the output is 0. Show the total.
Repeat the per-group check with a decision tree instead of k-NN. Do the groups differ more or less?
Train MLPClassifier on the digits with hidden layers of 8, 32 and 128 neurons. Report test accuracy for each.
Pick one real ML system you use. Write two ways it could treat some people unfairly.
Write one paragraph describing a project you would like to do. Bring it to the kickoff.
Watch it again at home to revise.