-
Notifications
You must be signed in to change notification settings - Fork 105
/
semi_parametric.py
759 lines (595 loc) · 27.7 KB
/
semi_parametric.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
from __future__ import absolute_import
import torch
import numpy as np
import pandas as pd
import scipy
import copy
from pysurvival import HAS_GPU
from pysurvival import utils
from pysurvival.utils import neural_networks as nn
from pysurvival.utils import optimization as opt
from pysurvival.models import BaseModel
from pysurvival.models._coxph import _CoxPHModel
from pysurvival.models._coxph import _baseline_functions
class CoxPHModel(BaseModel):
""" Cox proportional hazards model:
-------------------------------
The purpose of the model is to evaluate simultaneously
the effect of several factors on survival.
In other words, it allows us to examine how specified factors
influence the rate of a particular event happening
at a particular point in time.
The Cox model is expressed by the hazard function h(t)
(the risk of dying at time t. )
It can be estimated as follow:
h(t, x)=h_0(t)*exp(<x, W>)
Then the Survival function can be calculated as follow:
H(t, x) = cumsum( h(t, x) )
S(t, x) = exp( -H(t, x) )
Reference:
* http://www.sthda.com/english/wiki/cox-proportional-hazards-model
"""
def get_summary(self, alpha = 0.95, precision=3):
""" Providing the summary of the regression results:
* standard errors
* z-score
* p-value
"""
# Flattening the coef
W_flat = self.weights.flatten()
# calculating standard error
self.std_err = np.sqrt(self.inv_Hessian.diagonal())/self.std_scale
# Confidence Intervals
alpha = scipy.stats.norm.ppf((1. + alpha) / 2.)
lower_ci = np.round( W_flat - alpha * self.std_err, precision)
upper_ci = np.round( W_flat + alpha * self.std_err, precision)
z = np.round(W_flat / self.std_err , precision)
p_values = np.round(scipy.stats.chi2.sf( np.square(z), 1), precision)
W = np.round(W_flat, precision)
std_err = np.round(self.std_err, precision)
# Creating summary
df = np.c_[self.variables, W, std_err,
lower_ci, upper_ci, z, p_values]
df = pd.DataFrame(data = df,
columns = ['variables', 'coef', 'std. err',
'lower_ci', 'upper_ci',
'z', 'p_values'])
self.summary = df
return df
def fit(self, X, T, E, init_method='glorot_normal', lr = 1e-2,
max_iter = 100, l2_reg = 1e-2, alpha = 0.95,
tol = 1e-3, verbose = True ):
"""
Fitting a proportional hazards regression model using
the Efron's approximation method to take into account tied times.
As the Hessian matrix of the log-likelihood can be
calculated without too much effort, the model parameters are
computed using the Newton_Raphson Optimization scheme:
W_new = W_old - lr*<Hessian^(-1), gradient>
Arguments:
---------
* `X` : **array-like**, *shape=(n_samples, n_features)* --
The input samples.
* `T` : **array-like** --
The target values describing when the event of interest or
censoring occurred.
* `E` : **array-like** --
The values that indicate if the event of interest occurred
i.e.: E[i]=1 corresponds to an event, and E[i] = 0 means censoring,
for all i.
* `init_method` : **str** *(default = 'glorot_uniform')* --
Initialization method to use. Here are the possible options:
* `glorot_uniform`: Glorot/Xavier uniform initializer
* `he_uniform`: He uniform variance scaling initializer
* `uniform`: Initializing tensors with uniform (-1, 1) distribution
* `glorot_normal`: Glorot normal initializer,
* `he_normal`: He normal initializer.
* `normal`: Initializing tensors with standard normal distribution
* `ones`: Initializing tensors to 1
* `zeros`: Initializing tensors to 0
* `orthogonal`: Initializing tensors with a orthogonal matrix,
* `lr`: **float** *(default=1e-4)* --
learning rate used in the optimization
* `max_iter`: **int** *(default=100)* --
The maximum number of iterations in the Newton optimization
* `l2_reg`: **float** *(default=1e-4)* --
L2 regularization parameter for the model coefficients
* `alpha`: **float** *(default=0.95)* --
Confidence interval
* `tol`: **float** *(default=1e-3)* --
Tolerance for stopping criteria
* `verbose`: **bool** *(default=True)* --
Whether or not producing detailed logging about the modeling
Example:
--------
#### 1 - Importing packages
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from sklearn.model_selection import train_test_split
from pysurvival.models.simulations import SimulationModel
from pysurvival.models.semi_parametric import CoxPHModel
from pysurvival.utils.metrics import concordance_index
from pysurvival.utils.display import integrated_brier_score
#%pylab inline # To use with Jupyter notebooks
#### 2 - Generating the dataset from a Log-Logistic parametric model
# Initializing the simulation model
sim = SimulationModel( survival_distribution = 'log-logistic',
risk_type = 'linear',
censored_parameter = 10.1,
alpha = 0.1, beta=1.2 )
# Generating N random samples
N = 1000
dataset = sim.generate_data(num_samples = N, num_features = 3)
#### 3 - Creating the modeling dataset
# Defining the features
features = sim.features
# Building training and testing sets #
index_train, index_test = train_test_split( range(N), test_size = 0.2)
data_train = dataset.loc[index_train].reset_index( drop = True )
data_test = dataset.loc[index_test].reset_index( drop = True )
# Creating the X, T and E input
X_train, X_test = data_train[features], data_test[features]
T_train, T_test = data_train['time'].values, data_test['time'].values
E_train, E_test = data_train['event'].values, data_test['event'].values
#### 4 - Creating an instance of the Cox PH model and fitting the data.
# Building the model
coxph = CoxPHModel()
coxph.fit(X_train, T_train, E_train, lr=0.5, l2_reg=1e-2,
init_method='zeros')
#### 5 - Cross Validation / Model Performances
c_index = concordance_index(coxph, X_test, T_test, E_test) #0.92
print('C-index: {:.2f}'.format(c_index))
ibs = integrated_brier_score(coxph, X_test, T_test, E_test, t_max=10,
figure_size=(20, 6.5) )
References:
-----------
* https://en.wikipedia.org/wiki/Proportional_hazards_model#Tied_times
* Efron, Bradley (1974). "The Efficiency of Cox's Likelihood
Function for Censored Data". Journal of the American Statistical
Association. 72 (359): 557-565.
"""
# Collecting features names
N, self.num_vars = X.shape
if isinstance(X, pd.DataFrame):
self.variables = X.columns.tolist()
else:
self.variables = ['x_{}'.format(i) for i in range(self.num_vars)]
# Checking the format of the data
X, T, E = utils.check_data(X, T, E)
order = np.argsort(-T)
T = T[order]
E = E[order]
X = self.scaler.fit_transform( X[order, :] )
self.std_scale = np.sqrt( self.scaler.var_ )
# Initializing the model
self.model = _CoxPHModel()
# Creating the time axis
self.model.get_times(T, E)
# Initializing the parameters
W = np.zeros(self.num_vars)
W = opt.initialization(init_method, W, False).flatten()
W = W.astype(np.float64)
# Optimizing to find best parameters
epsilon=1e-9
self.model.newton_optimization(X, T, E, W, lr, l2_reg, tol, epsilon,
max_iter, verbose)
# Saving the Cython attributes in the Python object
self.weights = np.array( self.model.W )
self.loss = self.model.loss
self.times = np.array( self.model.times)
self.gradient = np.array( self.model.gradient )
self.Hessian = np.array( self.model.Hessian )
self.inv_Hessian = np.array( self.model.inv_Hessian )
self.loss_values = np.array( self.model.loss_values )
self.grad2_values = np.array( self.model.grad2_values )
# Computing baseline functions
score = np.exp( np.dot(X, self.weights) )
baselines = _baseline_functions(score, T, E)
# Saving the Cython attributes in the Python object
self.baseline_hazard = np.array( baselines[1] )
self.baseline_survival = np.array( baselines[2] )
del self.model
self.get_time_buckets()
# Calculating summary
self.get_summary(alpha)
return self
def predict(self, x, t = None):
"""
Predicting the hazard, density and survival functions
Arguments:
* x: pd.Dataframe or np.ndarray or list
x is the testing dataset containing the features
x should not be standardized before, the model
will take care of it
* t: float (default=None)
Time at which hazard, density and survival functions
should be calculated. If None, the method returns
the functions for all times t.
"""
# Convert x into the right format
x = utils.check_data(x)
# Sacling the dataset
if x.ndim == 1:
x = self.scaler.transform( x.reshape(1, -1) )
elif x.ndim == 2:
x = self.scaler.transform( x )
# Calculating risk_score, hazard, density and survival
phi = np.exp( np.dot(x, self.weights) )
hazard = self.baseline_hazard*phi.reshape(-1, 1)
survival = np.power(self.baseline_survival, phi.reshape(-1, 1))
density = hazard*survival
if t is None:
return hazard, density, survival
else:
min_index = [ abs(a_j_1-t) for (a_j_1, a_j) in self.time_buckets ]
index = np.argmin(min_index)
return hazard[:, index], density[:, index], survival[:, index]
def predict_risk(self, x, use_log = False):
"""
Predicting the risk score functions
Arguments:
* x: pd.Dataframe or np.ndarray or list
x is the testing dataset containing the features
x should not be standardized before, the model
will take care of it
"""
# Convert x into the right format
x = utils.check_data(x)
# Scaling the dataset
if x.ndim == 1:
x = self.scaler.transform( x.reshape(1, -1) )
elif x.ndim == 2:
x = self.scaler.transform( x )
# Calculating risk_score
risk_score = np.exp( np.dot(x, self.weights) )
if not use_log:
risk_score = np.exp(risk_score)
return risk_score
class NonLinearCoxPHModel(BaseModel):
""" NonLinear Cox Proportional Hazard model (NeuralCoxPH)
The original Cox Proportional Hazard model, was first introduced
by David R Cox in `Regression models and life-tables`.
The NonLinear CoxPH model was popularized by Katzman et al.
in `DeepSurv: Personalized Treatment Recommender System Using
A Cox Proportional Hazards Deep Neural Network` by allowing the use of
Neural Networks within the original design.
This current adaptation of the model differs from DeepSurv
as it uses the Efron's method to take ties into account.
Parameters
----------
* structure: None or list of dictionaries
Provides an MLP structure within the CoxPH
If None, then the model becomes the Linear CoxPH
ex: structure = [ {'activation': 'relu', 'num_units': 128},
{'activation': 'tanh', 'num_units': 128}, ]
Here are the possible activation functions:
* Atan
* BentIdentity
* BipolarSigmoid
* CosReLU
* ELU
* Gaussian
* Hardtanh
* Identity
* InverseSqrt
* LeakyReLU
* LeCunTanh
* LogLog
* LogSigmoid
* ReLU
* SELU
* Sigmoid
* Sinc
* SinReLU
* Softmax
* Softplus
* Softsign
* Swish
* Tanh
* auto_scaler: boolean (default=True)
Determines whether a sklearn scaler should be automatically
applied
"""
def __init__(self, structure=None, auto_scaler = True):
# Saving attributes
self.structure = structure
self.loss_values = []
# Initializing the elements from BaseModel
super(NonLinearCoxPHModel, self).__init__(auto_scaler)
def risk_fail_matrix(self, T, E):
""" Calculating the Risk, Fail matrices to calculate the loss
function by vectorizing all the quantities at stake
"""
N = T.shape[0]
Risk = np.zeros( (self.nb_times, N) )
Fail = np.zeros( (self.nb_times, N) )
for i in range(N):
# At risk
index_risk = np.argwhere( self.times <= T[i] ).flatten()
Risk[ index_risk, i] = 1.
# Failed
if E[i] == 1 :
index_fail = np.argwhere( self.times == T[i] )[0]
Fail[index_fail, i] = 1.
self.nb_fail_per_time = np.sum( Fail, axis = 1 ).astype(int)
return torch.FloatTensor(Risk), torch.FloatTensor(Fail)
def efron_matrix(self):
""" Computing the Efron Coefficient matrices to calculate the loss
function by vectorizing all the quantities at stake
"""
max_nb_fails = int(max(self.nb_fail_per_time))
Efron_coef = np.zeros( (self.nb_times, max_nb_fails ) )
Efron_one = np.zeros( (self.nb_times, max_nb_fails ) )
Efron_anti_one = np.ones( (self.nb_times, max_nb_fails ) )
for i, d in enumerate(self.nb_fail_per_time) :
if d > 0:
Efron_coef[i, :d] = [ h*1.0/d for h in range( d )]
Efron_one [i, :d] = 1.
Efron_anti_one[i, :d] = 0.
Efron_coef = torch.FloatTensor(Efron_coef)
Efron_one = torch.FloatTensor(Efron_one)
Efron_anti_one = torch.FloatTensor(Efron_anti_one)
return Efron_coef, Efron_one, Efron_anti_one
def loss_function(self, model, X, Risk, Fail,
Efron_coef, Efron_one, Efron_anti_one, l2_reg):
""" Efron's approximation loss function by vectorizing
all the quantities at stake
"""
# Calculating the score
pre_score = model(X)
score = torch.reshape( torch.exp(pre_score), (-1, 1) )
max_nb_fails = Efron_coef.shape[1]
# Numerator calculation
log_score = torch.log( score )
log_fail = torch.mm(Fail, log_score)
numerator = torch.sum(log_fail)
# Denominator calculation
risk_score = torch.reshape( torch.mm(Risk, score), (-1,1) )
risk_score = risk_score.repeat(1, max_nb_fails)
fail_score = torch.reshape( torch.mm(Fail, score), (-1,1) )
fail_score = fail_score.repeat(1, max_nb_fails)
Efron_Fail = fail_score*Efron_coef
Efron_Risk = risk_score*Efron_one
log_efron = torch.log( Efron_Risk - Efron_Fail + Efron_anti_one)
denominator = torch.sum( torch.sum(log_efron, dim=1) )
# Adding regularization
loss = - (numerator - denominator)
for w in model.parameters():
loss += l2_reg*torch.sum(w*w)/2.
return loss
def fit(self, X, T, E, init_method = 'glorot_uniform',
optimizer ='adam', lr = 1e-4, num_epochs = 1000,
dropout = 0.2, batch_normalization=False, bn_and_dropout=False,
l2_reg=1e-5, verbose=True):
"""
Fit the estimator based on the given parameters.
Parameters:
-----------
* `X` : **array-like**, *shape=(n_samples, n_features)* --
The input samples.
* `T` : **array-like** --
The target values describing when the event of interest or censoring
occurred.
* `E` : **array-like** --
The values that indicate if the event of interest occurred i.e.:
E[i]=1 corresponds to an event, and E[i] = 0 means censoring,
for all i.
* `init_method` : **str** *(default = 'glorot_uniform')* --
Initialization method to use. Here are the possible options:
* `glorot_uniform`: Glorot/Xavier uniform initializer
* `he_uniform`: He uniform variance scaling initializer
* `uniform`: Initializing tensors with uniform (-1, 1) distribution
* `glorot_normal`: Glorot normal initializer,
* `he_normal`: He normal initializer.
* `normal`: Initializing tensors with standard normal distribution
* `ones`: Initializing tensors to 1
* `zeros`: Initializing tensors to 0
* `orthogonal`: Initializing tensors with a orthogonal matrix,
* `optimizer`: **str** *(default = 'adam')* --
iterative method for optimizing a differentiable objective function.
Here are the possible options:
- `adadelta`
- `adagrad`
- `adam`
- `adamax`
- `rmsprop`
- `sparseadam`
- `sgd`
* `lr`: **float** *(default=1e-4)* --
learning rate used in the optimization
* `num_epochs`: **int** *(default=1000)* --
The number of iterations in the optimization
* `dropout`: **float** *(default=0.5)* --
Randomly sets a fraction rate of input units to 0
at each update during training time, which helps prevent overfitting.
* `l2_reg`: **float** *(default=1e-4)* --
L2 regularization parameter for the model coefficients
* `batch_normalization`: **bool** *(default=True)* --
Applying Batch Normalization or not
* `bn_and_dropout`: **bool** *(default=False)* --
Applying Batch Normalization and Dropout at the same time
* `verbose`: **bool** *(default=True)* --
Whether or not producing detailed logging about the modeling
Example:
--------
#### 1 - Importing packages
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from sklearn.model_selection import train_test_split
from pysurvival.models.simulations import SimulationModel
from pysurvival.models.semi_parametric import NonLinearCoxPHModel
from pysurvival.utils.metrics import concordance_index
from pysurvival.utils.display import integrated_brier_score
#%matplotlib inline # To use with Jupyter notebooks
#### 2 - Generating the dataset from a nonlinear Weibull parametric model
# Initializing the simulation model
sim = SimulationModel( survival_distribution = 'weibull',
risk_type = 'Gaussian',
censored_parameter = 2.1,
alpha = 0.1, beta=3.2 )
# Generating N random samples
N = 1000
dataset = sim.generate_data(num_samples = N, num_features=3)
# Showing a few data-points
dataset.head(2)
#### 3 - Creating the modeling dataset
# Defining the features
features = sim.features
# Building training and testing sets #
index_train, index_test = train_test_split( range(N), test_size = 0.2)
data_train = dataset.loc[index_train].reset_index( drop = True )
data_test = dataset.loc[index_test].reset_index( drop = True )
# Creating the X, T and E input
X_train, X_test = data_train[features], data_test[features]
T_train, T_test = data_train['time'].values, data_test['time'].values
E_train, E_test = data_train['event'].values, data_test['event'].values
#### 4 - Creating an instance of the NonLinear CoxPH model and fitting
# the data.
# Defining the MLP structure. Here we will build a 1-hidden layer
# with 150 units and `BentIdentity` as its activation function
structure = [ {'activation': 'BentIdentity', 'num_units': 150}, ]
# Building the model
nonlinear_coxph = NonLinearCoxPHModel(structure=structure)
nonlinear_coxph.fit(X_train, T_train, E_train, lr=1e-3,
init_method='xav_uniform')
#### 5 - Cross Validation / Model Performances
c_index = concordance_index(nonlinear_coxph, X_test, T_test, E_test)
print('C-index: {:.2f}'.format(c_index))
ibs = integrated_brier_score(nonlinear_coxph, X_test, T_test, E_test,
t_max=10, figure_size=(20, 6.5) )
"""
# Checking data format (i.e.: transforming into numpy array)
X, T, E = utils.check_data(X, T, E)
# Extracting data parameters
N, self.num_vars = X.shape
input_shape = self.num_vars
# Scaling data
if self.auto_scaler:
X_original = self.scaler.fit_transform( X )
# Sorting X, T, E in descending order according to T
order = np.argsort(-T)
T = T[order]
E = E[order]
X_original = X_original[order, :]
self.times = np.unique(T[E.astype(bool)])
self.nb_times = len(self.times)
self.get_time_buckets()
# Initializing the model
model = nn.NeuralNet(input_shape, 1, self.structure,
init_method, dropout, batch_normalization,
bn_and_dropout )
# Looping through the data to calculate the loss
X = torch.FloatTensor(X_original)
# Computing the Risk and Fail tensors
Risk, Fail = self.risk_fail_matrix(T, E)
Risk = torch.FloatTensor(Risk)
Fail = torch.FloatTensor(Fail)
# Computing Efron's matrices
Efron_coef, Efron_one, Efron_anti_one = self.efron_matrix()
Efron_coef = torch.FloatTensor(Efron_coef)
Efron_one = torch.FloatTensor(Efron_one)
Efron_anti_one = torch.FloatTensor(Efron_anti_one)
# Performing order 1 optimization
model, loss_values = opt.optimize(self.loss_function, model, optimizer,
lr, num_epochs, verbose, X=X, Risk=Risk, Fail=Fail,
Efron_coef=Efron_coef, Efron_one=Efron_one,
Efron_anti_one=Efron_anti_one, l2_reg=l2_reg)
# Saving attributes
self.model = model.eval()
self.loss_values = loss_values
# Computing baseline functions
x = X_original
x = torch.FloatTensor(x)
# Calculating risk_score
score = np.exp(self.model(torch.FloatTensor(x)).data.numpy().flatten())
baselines = _baseline_functions(score, T, E)
# Saving the Cython attributes in the Python object
self.times = np.array( baselines[0] )
self.baseline_hazard = np.array( baselines[1] )
self.baseline_survival = np.array( baselines[2] )
return self
def predict(self, x, t = None):
"""
Predicting the hazard, density and survival functions
Arguments:
* x: pd.Dataframe or np.ndarray or list
x is the testing dataset containing the features
x should not be standardized before, the model
will take care of it
* t: float (default=None)
Time at which hazard, density and survival functions
should be calculated. If None, the method returns
the functions for all times t.
"""
# Convert x into the right format
x = utils.check_data(x)
# Scaling the dataset
if x.ndim == 1:
x = self.scaler.transform( x.reshape(1, -1) )
elif x.ndim == 2:
x = self.scaler.transform( x )
# Calculating risk_score, hazard, density and survival
score = self.model(torch.FloatTensor(x)).data.numpy().flatten()
phi = np.exp( score )
hazard = self.baseline_hazard*phi.reshape(-1, 1)
survival = np.power(self.baseline_survival, phi.reshape(-1, 1))
density = hazard*survival
if t is None:
return hazard, density, survival
else:
min_index = [ abs(a_j_1-t) for (a_j_1, a_j) in self.time_buckets ]
index = np.argmin(min_index)
return hazard[:, index], density[:, index], survival[:, index]
def predict_risk(self, x, use_log = False):
"""
Predicting the risk score functions
Arguments:
* x: pd.Dataframe or np.ndarray or list
x is the testing dataset containing the features
x should not be standardized before, the model
will take care of it
"""
# Convert x into the right format
x = utils.check_data(x)
# Scaling the data
if self.auto_scaler:
if x.ndim == 1:
x = self.scaler.transform( x.reshape(1, -1) )
elif x.ndim == 2:
x = self.scaler.transform( x )
else:
# Ensuring x has 2 dimensions
if x.ndim == 1:
x = np.reshape(x, (1, -1))
# Transforming into pytorch objects
x = torch.FloatTensor(x)
# Calculating risk_score
score = self.model(x).data.numpy().flatten()
if not use_log:
score = np.exp(score)
return score
def __repr__(self):
""" Representing the class object """
if self.structure is None:
super(NonLinearCoxPHModel, self).__repr__()
return self.name
else:
S = len(self.structure)
self.name = self.__class__.__name__
empty = len(self.name)
self.name += '( '
for i, s in enumerate(self.structure):
n = 'Layer({}): '.format(i+1)
activation = nn.activation_function(s['activation'],
return_text=True)
n += 'activation = {}, '.format( s['activation'] )
n += 'num_units = {} '.format( s['num_units'] )
if i != S-1:
self.name += n + '; \n'
self.name += empty*' ' + ' '
else:
self.name += n
self.name += ')'
return self.name