desdeo_brb.brb¶
The main model class: BRBModel.
brb
¶
Main BRB model class with an sklearn-compatible interface.
Provides the BRBModel class which supports fitting, predicting, and
inspecting a Belief Rule-Based inference system.
BRBModel
¶
BRBModel(precedent_referential_values: list[ndarray], consequent_referential_values: ndarray, rule_base: RuleBase | None = None, utility_fn: Callable[[ndarray], ndarray] | None = None, initial_rule_fn: Callable[[ndarray], float] | None = None, backend: str = 'numpy')
A trainable Belief Rule-Based inference model.
Implements an sklearn-compatible interface (fit, predict, score,
get_params, set_params) for building and using BRB systems.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
precedent_referential_values
|
list[ndarray]
|
List of 1D sorted arrays, one per attribute. |
required |
consequent_referential_values
|
ndarray
|
1D sorted array of consequent values. |
required |
rule_base
|
RuleBase | None
|
Optional pre-configured RuleBase. If |
None
|
utility_fn
|
Callable[[ndarray], ndarray] | None
|
Optional utility function applied to consequent values before computing the scalar output. |
None
|
initial_rule_fn
|
Callable[[ndarray], float] | None
|
Optional callable mapping a 1D array of antecedent
values to a scalar. Used to compute initial belief degrees when
|
None
|
Source code in src/desdeo_brb/brb.py
predict
¶
predict(X: ndarray) -> InferenceResult
Run the full inference pipeline on input data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Input array of shape |
required |
Returns:
| Name | Type | Description |
|---|---|---|
An |
InferenceResult
|
class: |
Source code in src/desdeo_brb/brb.py
predict_values
¶
Convenience method returning only the scalar outputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Input array of shape |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
1-D array of shape |
Source code in src/desdeo_brb/brb.py
explain
¶
explain(X: ndarray, sample_idx: int = 0, top_k: int = 3, attribute_names: list[str] | None = None, consequent_name: str | None = None, threshold: float = 0.01) -> str
Predict on X and return a human-readable explanation.
Convenience wrapper that calls predict(X) and then
InferenceResult.explain() with this model's rule base.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Input array of shape |
required |
sample_idx
|
int
|
Which sample in the batch to explain. |
0
|
top_k
|
int
|
Number of top-activated rules to show. |
3
|
attribute_names
|
list[str] | None
|
Display names for each attribute. |
None
|
consequent_name
|
str | None
|
Display name for the consequent. |
None
|
threshold
|
float
|
Minimum weight/belief to display. |
0.01
|
Source code in src/desdeo_brb/brb.py
fit
¶
fit(X: ndarray, y: ndarray, fix_endpoints: bool = True, fix_endpoint_beliefs: bool = False, normalize_rule_weights: bool = True, method: str | None = None, optimizer_options: dict | None = None, n_restarts: int = 1, verbose: bool = False, **minimize_kwargs: Any) -> BRBModel
Train the model by minimizing MSE.
For the NumPy backend, supported methods are "SLSQP" (default)
and "trust-constr". For the JAX backend, the only supported
method is "L-BFGS-B" (default), which uses exact jax.grad
gradients.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Training inputs, shape |
required |
y
|
ndarray
|
Target values, shape |
required |
fix_endpoints
|
bool
|
If |
True
|
fix_endpoint_beliefs
|
bool
|
If |
False
|
normalize_rule_weights
|
bool
|
If |
True
|
method
|
str | None
|
scipy.optimize.minimize method. NumPy backend supports
|
None
|
optimizer_options
|
dict | None
|
Options dict passed to |
None
|
n_restarts
|
int
|
Number of optimization runs (default 1). When > 1, the first run uses the unperturbed initial parameters and subsequent runs perturb the initial parameters with seeded random noise. The final model is the best of all runs as measured by training MSE. Multi-start is critical for escaping bad local minima. |
1
|
verbose
|
bool
|
If |
False
|
**minimize_kwargs
|
Any
|
Extra keyword arguments forwarded to
|
{}
|
Returns:
| Type | Description |
|---|---|
BRBModel
|
self |
Source code in src/desdeo_brb/brb.py
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 | |
update_from_pyomo
¶
Extract solved parameter values from a Pyomo model and update the rule base.
Reads the variable values for belief degrees, rule weights,
attribute weights, and referential values from the Pyomo model
and assembles them into a fresh RuleBase (with validation).
Solver-tolerance violations are projected back onto the constraint
surface (rows renormalized to sum to 1, attribute weights clipped
to be non-negative, referential values sorted).
This method is the inverse of :func:build_pyomo_brb_model for
the parameter-extraction direction. Users who want to optimize a
custom Pyomo objective on top of the BRB structure can call::
from desdeo_brb.pyomo_backend import build_pyomo_brb_model
import pyomo.environ as pyo
m = build_pyomo_brb_model(brb, X, y)
m.del_component(m.obj)
m.obj = pyo.Objective(expr=my_custom_loss(m), sense=pyo.minimize)
pyo.SolverFactory("ipopt").solve(m)
brb.update_from_pyomo(m)
Source code in src/desdeo_brb/brb.py
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 | |
fit_custom
¶
fit_custom(loss_fn: Callable[[BRBModel], float], fix_endpoints: bool = True, fix_endpoint_beliefs: bool = False, normalize_rule_weights: bool = True, method: str = 'SLSQP', optimizer_options: dict | None = None, n_restarts: int = 1, constraints: list[dict] | None = None, verbose: bool = False, **minimize_kwargs: Any) -> BRBModel
Train using a user-supplied loss function.
The loss function receives the model instance (with updated
parameters) and must return a scalar loss value. The model's
parameters are updated internally before each call so the user
can simply call model.predict_values() inside the loss.
Optimization uses scipy with finite differences regardless of the
model's backend, since the user's loss function is opaque to JAX.
The structural BRB constraints (belief degree row sums, rule weight
sum, attribute weight bounds, referential value ordering) are
always enforced; users may pass additional constraints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loss_fn
|
Callable[[BRBModel], float]
|
Callable |
required |
fix_endpoints
|
bool
|
If |
True
|
fix_endpoint_beliefs
|
bool
|
If |
False
|
normalize_rule_weights
|
bool
|
If |
True
|
method
|
str
|
scipy optimizer to use. Supported: |
'SLSQP'
|
optimizer_options
|
dict | None
|
Options dict passed to |
None
|
n_restarts
|
int
|
Number of optimization runs from perturbed initial
points. The best result by |
1
|
constraints
|
list[dict] | None
|
Additional constraints to add on top of the BRB
structural constraints. For SLSQP, list of dicts with
|
None
|
verbose
|
bool
|
If |
False
|
**minimize_kwargs
|
Any
|
Extra keyword arguments forwarded to
|
{}
|
Returns:
| Type | Description |
|---|---|
BRBModel
|
self |
Source code in src/desdeo_brb/brb.py
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 | |
get_params
¶
Get model parameters (sklearn-compatible).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
deep
|
bool
|
If |
True
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary of parameter names to values. |
Source code in src/desdeo_brb/brb.py
set_params
¶
set_params(**params: Any) -> BRBModel
Set model parameters (sklearn-compatible).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**params
|
Any
|
Parameter names and values. |
{}
|
Returns:
| Type | Description |
|---|---|
BRBModel
|
self |
Source code in src/desdeo_brb/brb.py
score
¶
Return negative MSE (sklearn convention: higher is better).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Input array, shape |
required |
y
|
ndarray
|
True target values, shape |
required |
Returns:
| Type | Description |
|---|---|
float
|
Negative mean squared error. |