Last active
May 24, 2021 15:02
-
-
Save fwhigh/7745ea6f278f31f2f4e525b7a00df273 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Make some metric validation data | |
# Random predictions (AUC = 0.5) | |
awk -v n_lines=499999 'BEGIN {for (i=0; i<n_lines; i++) {print int(rand()>0.5),rand()}}' > label_pred | |
# Always correct predictions (AUC = 1) | |
awk -v n_lines=499999 'BEGIN {for (i=0; i<n_lines; i++) {label=rand(); print int(label>0.5),label}}' > label_pred | |
# Correct half the time, random and incorrect otherwise (AUC = 0.75) | |
awk -v n_lines=499999 'BEGIN {for (i=0; i<n_lines; i++) {label=rand(); r=rand(); r>0.5 ? pred=label : pred=r; print int(label>0.5),pred}}' > label_pred | |
# model output from file called "predictions" with labels "labels" | |
paste -d' ' labels predictions > label_pred | |
### One liners | |
# Accuracy | |
awk -v thresh=0.5 '$1 == ($2 > thresh) {t++} END {print t/NR}' label_pred | |
perf -ACC < label_pred | |
# Precision | |
awk -v thresh=0.5 '$2 > thresh {$1 == 1 ? tp++ : fp++} END {print tp/(tp+fp)}' label_pred | |
perf -PRE < label_pred | |
# Recall | |
awk -v thresh=0.5 '$1 == 1 {p++; $2 > thresh ? tp++ : 0} END {print tp/p}' label_pred | |
perf -REC < label_pred | |
# Root mean squared error | |
awk '{m+=($2-$1)^2} END {print sqrt(m/NR)}' label_pred | |
perf -RMS < label_pred | |
# Mean cross-entropy | |
awk '{$1 == 1 ? m-=log($2) : m-=log(1-$2)} END {print m/NR}' label_pred | |
#perf -CXE < label_pred # Doesn't give me the same answer | |
python -c 'import pandas as pd;from sklearn.metrics import log_loss;df=pd.read_csv("label_pred",sep=" ",header=None);print(log_loss(df[0],df[1]))' | |
# AUC with sort, rectangular rule | |
awk '{$1 > 0 ? p++ : n++; auc+=p*(n-prev_n); prev_n=n} END {print auc/p/n}' <(sort -g -k 2 -r label_pred) | |
perf -ROC < label_pred | |
# AUC with sort, trapezoidal rule. Invalid if prediction values repeat | |
awk '{$1 > 0 ? p++ : n++; auc+=(p+prev_p)/2*(n-prev_n); prev_p=p; prev_n=n} END {print auc/p/n}' <(sort -g -k 2 -r label_pred) | |
perf -ROC < label_pred | |
# AUC with sort, trapezoidal rule | |
awk '{if ($1 > 0) { p[$2]++ } else { n[$2]++ }; if (!($2 in a)) { a[$2]; b[s++]=$2 }} END {for (i = 0; i < s; i++) {cp+=p[b[i]]; cn+=n[b[i]]; auc+=(cp+cp_prev)/2*(cn-cn_prev); cp_prev=cp; cn_prev=cn}; print auc/cp/cn}' <(sort -g -k 2 -r label_pred) | |
perf -ROC < label_pred | |
# AUC without sort, trapezoidal rule, fixed decimals | |
awk -v decimals=6 '{if ($1 > 0) { p[int(10^decimals*$2)]++ } else { n[int(10^decimals*$2)]++ }} END {for (i = 10^decimals; i >= 0; i--) {cp+=p[i]; cn+=n[i]; auc+=(cp+cp_prev)/2*(cn-cn_prev); cp_prev=cp; cn_prev=cn}; print auc/cp/cn}' label_pred | |
perf -ROC < label_pred | |
# Train a l1 & l2 regularized linear regression model with stochastic gradient descent | |
awk -v n_lines="1e6" 'BEGIN {for (i=0; i<n_lines; i++) {print int(rand()>0.5),"|f","1:" rand()}}' > fake_vw | |
# n: eta, learning rate | |
# l1: LASSO strength | |
# l2: Ridge strength | |
awk -v n=0.1 -v l1="1e-1" -v l2="1e-1" '{r=w[-1]; for (i=3;i<=NF;i++) {split($i,f,":"); r+=w[f[1]]*f[2]}; r-=$1; w[-1]-=n*2*r; for (i=3;i<=NF;i++) {split($i,f,":"); w[f[1]]>=0?s=1:s=-1;w[f[1]]-=n*2*f[2]*r+n*s*l1+n*2*l2*w[f[1]]}} END {for (i in w) {print i, w[i]}}' fake_vw |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment