Visual demo for survival analysis (regression) with Accelerated Failure Time (AFT) model.
def
plot_censored_labels
(
X
:
,
y_lower
:
,
y_upper
:
)
->
None
:
def
replace_inf
(
x
:
,
target_value
:
float
)
->
:
x
[
(
x
)]
=
target_value
return
x
plt
.
plot
(
X
,
y_lower
,
"o"
,
label
=
"y_lower"
,
color
=
"blue"
)
plt
.
plot
(
X
,
y_upper
,
"o"
,
label
=
"y_upper"
,
color
=
"fuchsia"
)
plt
.
vlines
(
X
,
ymin
=
replace_inf
(
y_lower
,
0.01
),
ymax
=
replace_inf
(
y_upper
,
1000.0
),
label
=
"Range for y"
,
color
=
"gray"
,
)
X
=
([
1
,
2
,
3
,
4
,
5
])
.
reshape
((
-
1
,
1
))
INF
=
y_lower
=
([
10
,
15
,
-
INF
,
30
,
100
])
y_upper
=
([
INF
,
INF
,
20
,
50
,
INF
])
plt
.
figure
(
figsize
=
(
5
,
4
))
plot_censored_labels
(
X
,
y_lower
,
y_upper
)
plt
.
ylim
((
6
,
200
))
plt
.
legend
(
loc
=
"lower right"
)
plt
.
title
(
"Toy data"
)
plt
.
xlabel
(
"Input feature"
)
plt
.
ylabel
(
"Label"
)
plt
.
yscale
(
"log"
)
plt
.
tight_layout
()
plt
.
show
(
block
=
True
)
grid_pts
=
(
0.8
,
5.2
,
1000
)
.
reshape
((
-
1
,
1
))
dmat
=
xgb
.
DMatrix
(
X
)
dmat
.
set_float_info
(
"label_lower_bound"
,
y_lower
)
dmat
.
set_float_info
(
"label_upper_bound"
,
y_upper
)
params
=
{
"max_depth"
:
3
,
"objective"
:
"survival:aft"
,
"min_child_weight"
:
0
}
accuracy_history
=
[]
class
PlotIntermediateModel
(
xgb
.
callback
.
TrainingCallback
):
"""Custom callback to plot intermediate models."""
def
__init__
(
self
)
->
None
:
super
()
.
__init__
()
def
after_iteration
(
self
,
model
:
xgb
.
Booster
,
epoch
:
int
,
evals_log
:
xgb
.
callback
.
TrainingCallback
.
EvalsLog
,
)
->
bool
:
"""Run after training is finished."""
y_pred
=
model
.
predict
(
dmat
)
acc
=
(
(
y_pred
>=
y_lower
,
y_pred
<=
y_upper
)
/
len
(
X
)
*
100
)
accuracy_history
.
append
(
acc
)
plt
.
subplot
(
5
,
3
,
epoch
+
1
)
plot_censored_labels
(
X
,
y_lower
,
y_upper
)
y_pred_grid_pts
=
model
.
predict
(
xgb
.
DMatrix
(
grid_pts
))
plt
.
plot
(
grid_pts
,
y_pred_grid_pts
,
"r-"
,
label
=
"XGBoost AFT model"
,
linewidth
=
4
)
plt
.
title
(
"Iteration
{}
"
.
format
(
epoch
),
x
=
0.5
,
y
=
0.8
)
plt
.
xlim
((
0.8
,
5.2
))
plt
.
ylim
((
1
if
(
y_pred
)
<
6
else
6
,
200
))
plt
.
yscale
(
"log"
)
return
False
res
:
xgb
.
callback
.
TrainingCallback
.
EvalsLog
=
{}
plt
.
figure
(
figsize
=
(
12
,
13
))
bst
=
xgb
.
train
(
params
,
dmat
,
num_boost_round
=
15
,
evals
=
[(
dmat
,
"train"
)],
evals_result
=
res
,
callbacks
=
[
PlotIntermediateModel
()],
)
plt
.
tight_layout
()
plt
.
legend
(
loc
=
"lower center"
,
ncol
=
4
,
bbox_to_anchor
=
(
0.5
,
0
),
bbox_transform
=
plt
.
gcf
()
.
transFigure
,
)
plt
.
tight_layout
()
plt
.
figure
(
figsize
=
(
8
,
3
))
plt
.
subplot
(
1
,
2
,
1
)
plt
.
plot
(
res
[
"train"
][
"aft-nloglik"
],
"b-o"
,
label
=
"aft-nloglik"
)
plt
.
xlabel
(
"# Boosting Iterations"
)
plt
.
legend
(
loc
=
"best"
)
plt
.
subplot
(
1
,
2
,
2
)
plt
.
plot
(
accuracy_history
,
"r-o"
,
label
=
"Accuracy (%)"
)
plt
.
xlabel
(
"# Boosting Iterations"
)
plt
.
legend
(
loc
=
"best"
)
plt
.
tight_layout
()
plt
.
show
()