slisemap.local_models
Module that contains the built-in alternatives for local white box models.
These functions can also be used as templates for implementing your own.
local_predict(X, B, local_model)
Get individual predictions when every data item has a separate model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
Tensor
|
Data matrix [n, m]. |
required |
B |
Tensor
|
Coefficient matrix [n, q]. |
required |
local_model |
Callable[[Tensor, Tensor], Tensor]
|
Prediction function: [1, m], [1, q] -> [1, 1, o]. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Matrix of local predictions [n, o]. |
Source code in slisemap/local_models.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
ALocalModel
Bases: ABC
Abstract class for gathering all the functions needed for a local model (predict, loss, coefficients).
Source code in slisemap/local_models.py
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 |
|
predict(X, B)
abstractmethod
staticmethod
Prediction function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
Tensor
|
Data matrix. |
required |
B |
Tensor
|
Coefficient matrix. |
required |
Returns:
Name | Type | Description |
---|---|---|
Y |
Tensor
|
Prediction matrix. |
Source code in slisemap/local_models.py
44 45 46 47 48 49 50 51 52 53 54 55 56 |
|
loss(Ytilde, Y)
abstractmethod
staticmethod
Loss function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
Ytilde |
Tensor
|
Prediction matrix. |
required |
Y |
Tensor
|
Target matrix |
required |
Returns:
Name | Type | Description |
---|---|---|
L |
Tensor
|
Loss matrix. |
Source code in slisemap/local_models.py
58 59 60 61 62 63 64 65 66 67 68 69 70 |
|
coefficients(X, Y, intercept)
abstractmethod
staticmethod
Get for the number of columns of B.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
Union[Tensor, ndarray]
|
Data matrix. |
required |
Y |
Union[Tensor, ndarray]
|
Target matrix. |
required |
intercept |
bool
|
Add intercept. |
required |
Returns:
Type | Description |
---|---|
int
|
Number of columns. |
Source code in slisemap/local_models.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
|
regularisation(X, Y, B, Z, Ytilde)
staticmethod
Regularisation function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
Tensor
|
Data matrix. |
required |
Y |
Tensor
|
Target matrix. |
required |
B |
Tensor
|
Coefficient matrix. |
required |
Z |
Tensor
|
Embedding matrix. |
required |
Ytilde |
Tensor
|
Prediction matrix. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Additional loss term. |
Source code in slisemap/local_models.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
|
linear_regression(X, B)
Prediction function for (multiple) linear regression.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
Tensor
|
Data matrix [n_x, m]. |
required |
B |
Tensor
|
Coefficient Matrix [n_b, m * p]. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Prediction tensor [n_b, n_x, p] |
Source code in slisemap/local_models.py
114 115 116 117 118 119 120 121 122 123 124 125 |
|
multiple_linear_regression(X, B)
Prediction function for multiple linear regression. DEPRECATED.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
Tensor
|
Data matrix [n_x, m]. |
required |
B |
Tensor
|
Coefficient Matrix [n_b, m*p]. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Prediction tensor [n_b, n_x, p] |
Deprecated
1.4: In favour of a combined linear_regression
Source code in slisemap/local_models.py
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
|
linear_regression_loss(Ytilde, Y, B=None)
Least squares loss function for (multiple) linear regresson.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
Ytilde |
Tensor
|
Predicted values [n_b, n_x, p]. |
required |
Y |
Tensor
|
Ground truth values [n_x, p]. |
required |
B |
Optional[Tensor]
|
Coefficient matrix. Deprecated. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
Tensor
|
Loss values [n_b, n_x]. |
Deprecated
1.6: B
Source code in slisemap/local_models.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
|
linear_regression_coefficients(X, Y, intercept=False)
Get the number of coefficients for a (multiple) linear regression.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
Union[Tensor, ndarray]
|
Data matrix. |
required |
Y |
Union[Tensor, ndarray]
|
Target matrix. |
required |
intercept |
bool
|
Add an (additional) intercept to X. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
int
|
Number of coefficients (columns of B). |
Source code in slisemap/local_models.py
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
|
LinearRegression
Bases: ALocalModel
A class that contains all the functions needed for linear regression.
Source code in slisemap/local_models.py
182 183 184 185 186 187 |
|
absolute_error(Ytilde, Y, B=None)
Absolute error function for (multiple) linear regresson.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
Ytilde |
Tensor
|
Predicted values [n_b, n_x, p]. |
required |
Y |
Tensor
|
Ground truth values [n_x, p]. |
required |
B |
Optional[Tensor]
|
Coefficient matrix. Deprecated. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
Tensor
|
Loss values [n_b, n_x]. |
Deprecated
1.6: B
Source code in slisemap/local_models.py
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
|
LinearAbsoluteRegression
Bases: ALocalModel
A class that contains all the functions needed for linear regression with absolute errors.
Source code in slisemap/local_models.py
209 210 211 212 213 214 |
|
logistic_regression(X, B)
Prediction function for (multinomial) logistic regression.
Note that the number of coefficients is m * (p-1)
due to the normalisation of softmax.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
Tensor
|
Data matrix [n_x, m]. |
required |
B |
Tensor
|
Coefficient Matrix [n_b, m*(p-1)]. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Prediction tensor [n_b, n_x, p] |
Source code in slisemap/local_models.py
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
|
logistic_regression_loss(Ytilde, Y, B=None)
Squared Hellinger distance function for (multinomial) logistic regression.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
Ytilde |
Tensor
|
Predicted values [n_b, n_x, p]. |
required |
Y |
Tensor
|
Ground truth values [n_x, p]. |
required |
B |
Optional[Tensor]
|
Coefficient matrix. Deprecated. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
Tensor
|
Loss values [n_b, n_x]. |
Deprecated
1.6: B
Source code in slisemap/local_models.py
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
|
logistic_regression_coefficients(X, Y, intercept=False)
Get the number of coefficients for a (multinomial) logistic regression.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
Union[Tensor, ndarray]
|
Data matrix. |
required |
Y |
Union[Tensor, ndarray]
|
Target matrix. |
required |
intercept |
bool
|
Add an (additional) intercept to X. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
int
|
Number of coefficients (columns of B). |
Source code in slisemap/local_models.py
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
|
LogisticRegression
Bases: ALocalModel
A class that contains all the functions needed for logistic regression.
Source code in slisemap/local_models.py
282 283 284 285 286 287 |
|
logistic_regression_log(X, B)
Prediction function for (multinomial) logistic regression that returns the log of the prediction.
Note that the number of coefficients is m * (p-1)
due to the normalisation of softmax.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
Tensor
|
Data matrix [n_x, m]. |
required |
B |
Tensor
|
Coefficient Matrix [n_b, m*(p-1)]. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Prediction tensor [n_b, n_x, p] |
Source code in slisemap/local_models.py
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
|
logistic_regression_log_loss(Ytilde, Y, B=None)
Cross entropy loss function for (multinomial) logistic regression.
Note that this loss function expects Ytilde
to be the log of the predicted probabilities.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
Ytilde |
Tensor
|
Predicted logits [n_b, n_x, p]. |
required |
Y |
Tensor
|
Ground truth values [n_x, p]. |
required |
B |
Optional[Tensor]
|
Coefficient matrix. Deprecated. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
Tensor
|
Loss values [n_b, n_x]. |
Deprecated
1.6: B
Source code in slisemap/local_models.py
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 |
|
LogisticLogRegression
Bases: ALocalModel
A class that contains all the functions needed for logistic regression.
The predictions are in log-space rather than probabilities for numerical stability.
Source code in slisemap/local_models.py
339 340 341 342 343 344 345 346 347 |
|
identify_local_model(local_model, local_loss=None, coefficients=None, regularisation=None)
Identify the "predict", "loss", and "coefficients" functions for a local model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
local_model |
Union[LocalModelCollection, CallableLike[predict]]
|
A instance/subclass of |
required |
local_loss |
Optional[CallableLike[loss]]
|
A loss function or None if it is part of |
None
|
coefficients |
Union[None, int, CallableLike[coefficients]]
|
The number of coefficients, or a function giving that number, or None if it is part of |
None
|
regularisation |
Union[None, CallableLike[regularisation]]
|
Additional regularisation function. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
predict |
Callable
|
"prediction" function (takes X and B and returns predicted Y for every X and B combination). |
loss |
Callable
|
"loss" function (takes predicted Y and real Y and returns the loss). |
coefficients |
Callable
|
"coefficients" function (takes X and Y and returns the number of coefficients for B). |
regularisation |
Callable
|
"regularisation" function (takes X, Y, B, Z, and, Ytilde and returns an additional loss scalar). |
Source code in slisemap/local_models.py
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 |
|