Close Menu
    Trending
    • How to Access NASA’s Climate Data — And How It’s Powering the Fight Against Climate Change Pt. 1
    • From Training to Drift Monitoring: End-to-End Fraud Detection in Python | by Aakash Chavan Ravindranath, Ph.D | Jul, 2025
    • Using Graph Databases to Model Patient Journeys and Clinical Relationships
    • Cuba’s Energy Crisis: A Systemic Breakdown
    • AI Startup TML From Ex-OpenAI Exec Mira Murati Pays $500,000
    • STOP Building Useless ML Projects – What Actually Works
    • Credit Risk Scoring for BNPL Customers at Bati Bank | by Sumeya sirmula | Jul, 2025
    • The New Career Crisis: AI Is Breaking the Entry-Level Path for Gen Z
    AIBS News
    • Home
    • Artificial Intelligence
    • Machine Learning
    • AI Technology
    • Data Science
    • More
      • Technology
      • Business
    AIBS News
    Home»Artificial Intelligence»Uncertainty Quantification in Machine Learning with an Easy Python Interface
    Artificial Intelligence

    Uncertainty Quantification in Machine Learning with an Easy Python Interface

    Team_AIBS NewsBy Team_AIBS NewsMarch 27, 2025No Comments16 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Share
    Facebook Twitter LinkedIn Pinterest Email


    (UQ) in a Machine Learning (ML) mannequin permits one to estimate the precision of its predictions. That is extraordinarily vital for using its predictions in real-world duties. For example, if a machine studying mannequin is educated to foretell a property of a fabric, a predicted worth with a 20% uncertainty (error) is probably going for use very otherwise from a predicted worth with a 5% uncertainty (error) within the total decision-making course of. Regardless of its significance, UQ capabilities aren’t out there with well-liked ML software program in Python, comparable to scikit-learn, Tensorflow, and Pytorch.

    Enter ML Uncertainty: a Python package deal designed to handle this drawback. Constructed on high of well-liked Python libraries comparable to SciPy and scikit-learn, ML Uncertainty gives a really intuitive interface to estimate uncertainties in ML predictions and, the place attainable, mannequin parameters. Requiring solely about 4 strains of code to carry out these estimations, the package deal leverages highly effective and theoretically rigorous mathematical strategies within the background. It exploits the underlying statistical properties of the ML mannequin in query, making the package deal computationally cheap. Furthermore, this strategy extends its applicability to real-world use instances the place typically, solely small quantities of information can be found.

    Motivation

    I’ve been an avid Python consumer for the final 10 years. I really like the massive variety of highly effective libraries which were created and maintained, and the group, which may be very lively. The thought for ML Uncertainty got here to me after I was engaged on a hybrid ML drawback. I had constructed an ML mannequin to foretell stress-strain curves of some polymers. Stress-strain curves–an vital property of polymers–obey sure physics-based guidelines; for example, they’ve a linear area at low pressure values, and the tensile modulus decreases with temperature.

    I discovered from literature some non-linear fashions to explain the curves and these behaviors, thereby lowering the stress-strain curves to a set of parameters, every with some bodily that means. Then, I educated an ML mannequin to foretell these parameters from some simply measurable polymer attributes. Notably, I solely had just a few hundred information factors, as is sort of widespread in scientific purposes. Having educated the mannequin, finetuned the hyperparameters, and carried out the outlier evaluation, one of many stakeholders requested me: “That is all good, however what are the error estimates in your predictions?” And I spotted that there wasn’t a sublime strategy to estimate this with Python. I additionally realized that this wasn’t going to be the final time that this drawback was going to come up. And that led me down the trail that culminated on this package deal. 

    Having spent a while learning Statistics, I suspected that the maths for this wasn’t not possible and even that onerous. I started researching and studying up books like Introduction to Statistical Studying and Parts of Statistical Studying1,2 and located some solutions there. ML Uncertainty is my try at implementing a few of these strategies in Python to combine statistics extra tightly into machine studying. I imagine that the way forward for machine studying is determined by our capability to extend the reliability of predictions and the interpretability of fashions, and this can be a small step in direction of that aim. Having developed this package deal, I’ve continuously used it in my work, and it has benefited me drastically.

    That is an introduction to ML Uncertainty with an outline of the theories underpinning it. I’ve included some equations to elucidate the speculation, but when these are overwhelming, be at liberty to gloss over them. For each equation, I’ve acknowledged the important thing concept it represents.

    Getting began: An instance

    We regularly study finest by doing. So, earlier than diving deeper, let’s think about an instance. Say we’re engaged on a very good old style linear regression drawback the place the mannequin is educated with scikit-learn. We predict that the mannequin has been educated properly, however we wish extra info. For example, what are the prediction intervals for the outputs? With ML Uncertainty, this may be accomplished in 4 strains as proven beneath and mentioned on this example.

    Illustrating ML uncertainty code (a) and plot (b) for linear regression. Picture by creator.

    All examples for this package deal may be discovered right here: https://github.com/architdatar/ml_uncertainty/tree/main/examples.

    Delving deeper: A peek below the hood

    ML Uncertainty performs these computations by having the ParametricModelInference class wrap across the LinearRegression estimator from scikit-learn to extract all the knowledge it must carry out the uncertainty calculations. It follows the usual process for uncertainty estimation, which is detailed in lots of a statistics textbook,2 of which an outline is proven beneath.

    Since this can be a linear mannequin that may be expressed by way of parameters (( beta )) as ( y = Xbeta ), ML Uncertainty first computes the levels of freedom for the mannequin (( p )), the error levels of freedom (( n – p – 1 )), and the residual sum of squares (( hat{sigma}^2 )). Then, it computes the uncertainty within the mannequin parameters; i.e., the variance-covariance matrix.3

    ( textual content{Var}(hat{beta}) = hat{sigma}^2 (J^T J)^{-1} )

    The place ( J ) is the Jacobian matrix for the parameters. For linear regression, this interprets to:

    ( textual content{Var}(hat{beta}) = hat{sigma}^2 (X^T X)^{-1} )

    Lastly, the get_intervals operate computes the prediction intervals by propagating the uncertainties in each inputs in addition to the parameters. Thus, for information ( X^* ) the place predictions and uncertainties are to be estimated, predictions ( hat{y^*} ) together with the ( (1 – alpha) instances 100% ) prediction interval are:

    ( hat{y^*} pm t_{1 – alpha/2, n – p – 1} , hat{sigma} sqrt{textual content{Var}(hat{y^*})} )

    The place,

    ( textual content{Var}(hat{y^*}) = (nabla_X f)(delta X^*)^2(nabla_X f)^T + (nabla_beta f)(delta hat{beta})^2(nabla_beta f)^T + hat{sigma}^2 )

    In English, which means the uncertainty within the output is determined by the uncertainty within the inputs, uncertainty within the parameters, and the residual uncertainty. Simplified for a a number of linear mannequin and assuming no uncertainty in inputs, this interprets to:

    ( textual content{Var}(hat{y^*}) = hat{sigma}^2 left(1 + X^* (X^T X)^{-1} X^{*T} proper) )

    Extensions to linear regression

    So, that is what goes on below the hood when these 4 strains of code are executed for linear regression. However this isn’t all. ML Uncertainty comes geared up with two extra highly effective capabilities:

    1. Regularization: ML Uncertainty helps L1, L2, and L1+L2 regularization. Mixed with linear regression, which means it may cater to LASSO, ridge, and elastic internet regressions. Take a look at this example.
    2. Weighted least squares regression: Typically, not all observations are equal. We would wish to give extra weight to some observations and fewer weight to others. Generally, this occurs in science when some observations have a excessive quantity of uncertainty whereas some are extra exact. We would like our regression to replicate the extra exact ones, however can’t absolutely discard those with excessive uncertainty. For such instances, the weighted least squares regression is used.

    Most significantly, a key assumption of linear regression is one thing often called homoscedasticity; i.e., that the samples of the response variables are drawn from populations with related variances. If this isn’t the case, it’s dealt with by assigning weights to responses relying on the inverse of their variance. This may be simply dealt with in ML Uncertainty by merely specifying the pattern weights for use throughout coaching within the y_train_weights parameter of the ParametricModelInference class, and the remainder might be dealt with. An software of that is proven on this example, albeit for a nonlinear regression case.

    Foundation expansions

    I’m all the time fascinated by how a lot ML we are able to get accomplished by simply doing linear regression correctly. Many sorts of information comparable to tendencies, time sequence, audio, and pictures, may be represented by foundation expansions. These representations behave like linear fashions with many superb properties. ML Uncertainty can be utilized to compute uncertainties for these fashions simply. Take a look at these examples known as spline_synthetic_data, spline_wage_data, and fourier_basis.

    Outcomes of ML Uncertainty used for weighted least squares regression, B-Spline foundation with artificial information, B-Spline foundation with wage information, and Fourier foundation. Picture by creator.

    Past linear regression

    We regularly encounter conditions the place the underlying mannequin can’t be expressed as a linear mannequin. This generally happens in science, for example, when advanced response kinetics, transport phenomena, course of management issues, are modeled. Customary Python packages like scikit-learn, and so forth., don’t permit one to instantly match these non-linear fashions and carry out uncertainty estimation on them. ML Uncertainty ships with a category known as NonLinearRegression able to dealing with non-linear fashions. The consumer can specify the mannequin to be match and the category handles becoming with a scikit-learn-like interface which makes use of a SciPy least_squares operate within the background. This may be simply built-in with the ParametericModelInference class for seamless uncertainty estimation. Like linear regression, we are able to deal with weighted least squares and regularization for non-linear regression. Right here is an example.

    Random Forests

    Random Forests have gained vital reputation within the area. They function by averaging the predictions of choice bushes. Determination bushes, in flip, establish a algorithm to divide the predictor variable house (enter house) and assign a response worth to every terminal node (leaf). The predictions from choice bushes are averaged to supply a prediction for the random forest.1 They’re significantly helpful as a result of they’ll establish advanced relationships in information, are correct, and make fewer assumptions in regards to the information than regressions do.

    Whereas it’s applied in well-liked ML libraries like scikit-learn, there isn’t any easy strategy to estimate prediction intervals. That is significantly vital for regression as random forests, given their excessive flexibility, are inclined to overfit their coaching information. Since random forests doesn’t have parameters like conventional regression fashions do, uncertainty quantification must be carried out otherwise. 

    We use the essential concept of estimating prediction intervals utilizing bootstrapping as described by Hastie et al. in Chapter 7 of their e-book Parts of Statistical Studying.2 The central concept we are able to exploit is that the variance of the predictions ( S(Z) ) for some information ( Z ) may be estimated through predictions of its bootstrap samples as follows:

    ( widehat{textual content{Var}}[S(Z)] = frac{1}{B – 1} sum_{b=1}^{B} left( S(Z^{*b}) – bar{S}^{*} proper)^2 )

    The place ( bar{S}^{*} = sum_b S(Z^{*b}) / B ). Bootstrap samples are samples drawn from the unique dataset repeatedly and independently, thereby permitting repetitions. Fortunate for us, random forests are educated utilizing one bootstrap pattern for every choice tree inside it. So, the prediction from every tree leads to a distribution whose variance offers us the variance of the prediction. However there’s nonetheless one drawback. Let’s say we wish to receive the variance in prediction for the ( i^{textual content{th}} ) coaching pattern. If we merely use the system above, some predictions might be from bushes that embody the ( i^{textual content{th}} ) pattern within the bootstrap pattern on which they’re educated. This might result in an unrealistically smaller variance estimate.

    To unravel this drawback, the algorithm applied in ML Uncertainty solely considers predictions from bushes which didn’t use the ( i^{textual content{th}} ) pattern for coaching. This leads to an unbiased estimate of the variance.

    The gorgeous factor about this strategy is that we don’t want any extra re-training steps. As a substitute, the EnsembleModelInference class elegantly wraps across the RandomForestRegressor estimator in scikit-learn and obtains all the mandatory info from it.

    This technique is benchmarked utilizing the tactic described in Zhang et al.,4 which states {that a} right ( (1 – alpha) instances 100% ) prediction interval is one for which the chance of it containing the noticed response is ( (1 – alpha) instances 100% ). Mathematically,

    ( P(Y in I_{alpha}) approx 1 – alpha )

    Right here is an example to see ML Uncertainty in motion for random forest fashions.

    Uncertainty propagation (Error propagation)

    How a lot does a certain quantity of uncertainty in enter variables and/or mannequin parameters have an effect on the uncertainty within the response variable? How does this uncertainty (epistemic) evaluate to the inherent uncertainty within the response variables (aleatoric uncertainty)? Usually, you will need to reply these inquiries to resolve on the plan of action. For example, if one finds that the uncertainty in mannequin parameters contributes extremely to the uncertainty in predictions, one may gather extra information or examine various fashions to cut back this uncertainty. Conversely, if the epistemic uncertainty is smaller than the aleatoric uncertainty, making an attempt to cut back it additional may be pointless. With ML uncertainty, these questions may be answered simply.

    Given a mannequin relating the predictor variables to the response variable, the ErrorPropagation class can simply compute the uncertainty in responses. Say the responses (( y )) are associated to the predictor variables (( X )) through some operate (( f )) and a few parameters (( beta )), expressed as:

    ( y = f(X, beta) ).

    We want to receive prediction intervals for responses (( hat{y^*} )) for some predictor information (( X^* )) with mannequin parameters estimated as ( hat{beta} ). The uncertainty in ( X^* ) and ( hat{beta} ) are given by ( delta X^* ) and ( delta hat{beta} ), respectively. Then, the ( (1 – alpha) instances 100% ) prediction interval of the response variables might be given as:

    ( hat{y^*} pm t_{1 – alpha/2, n – p – 1} , hat{sigma} sqrt{textual content{Var}(hat{y^*})} )

    The place,

    ( textual content{Var}(hat{y^*}) = (nabla_X f)(delta X^*)^2(nabla_X f)^T + (nabla_beta f)(delta hat{beta})^2(nabla_beta f)^T + hat{sigma}^2 )

    The vital factor right here is to note how the uncertainty in predictions contains contributions from the inputs, parameters, in addition to the inherent uncertainty of the response.

    The flexibility of the ML Uncertainty package deal to propagate each enter and parameter uncertainties makes it very helpful, significantly in science, the place we strongly care in regards to the error (uncertainty) in every worth being predicted. Think about the usually talked about idea of hybrid machine studying. Right here, we mannequin identified relationships in information by way of first rules and unknown ones utilizing black-box fashions. Utilizing ML Uncertainty, the uncertainties obtained from these totally different strategies may be simply propagated by way of the computation graph.

    A quite simple instance is that of the Arrhenius mannequin for predicting response charge constants. The system ( ok = Ae^{-E_a / RT} ) may be very well-known. Say, the parameters ( A, E_a ) have been predicted from some ML mannequin and have an uncertainty of 5%. We want to know the way a lot error that interprets to within the response charge fixed.

    This may be very simply achieved with ML Uncertainty as proven on this example.

    Illustration of uncertainty propagation by way of computational graph. Picture by creator.

    Limitations

    As of v0.1.1, ML Uncertainty solely works for ML fashions educated with scikit-learn. It helps the next ML fashions natively: random forest, linear regression, LASSO regression, ridge regression, elastic internet, and regression splines. For every other fashions, the consumer can create the mannequin, the residual, loss operate, and so forth., as proven for the non-linear regression instance. The package deal has not been examined for neural networks, transformers, and different deep studying fashions.

    Contributions from the open-source ML group are welcome and extremely appreciated. Whereas there’s a lot to be accomplished, some key areas of effort are adapting ML Uncertainty to different frameworks comparable to PyTorch and Tensorflow, including assist for different ML fashions, highlighting points, and enhancing documentation.

    Benchmarking

    The ML Uncertainty code has been benchmarked towards the statsmodels package deal in Python. Particular instances may be discovered here.

    Background

    Uncertainty quantification in machine studying has been studied within the ML group and there’s rising curiosity on this area. Nonetheless, as of now, the prevailing options are relevant to very particular use instances and have key limitations.

    For linear fashions, the statsmodels library can present UQ capabilities. Whereas theoretically rigorous, it can’t deal with non-linear fashions. Furthermore, the mannequin must be expressed in a format particular to the package deal. Which means the consumer can’t benefit from the highly effective preprocessing, coaching, visualization, and different capabilities supplied by ML packages like scikit-learn. Whereas it may present confidence intervals based mostly on uncertainty within the mannequin parameters, it can’t propagate uncertainty in predictor variables (enter variables).

    One other household of options is model-agnostic UQ. These options make the most of subsamples of coaching information, prepare the mannequin repeatedly based mostly on it, and use these outcomes to estimate prediction intervals. Whereas generally helpful within the restrict of huge information, these strategies could not present correct estimates for small coaching datasets the place the samples chosen may result in considerably totally different estimates. Furthermore, it’s a computationally costly train for the reason that mannequin must be retrained a number of instances. Some packages utilizing this strategy are MAPIE, PUNCC, UQPy, and ml_uncertainty by NIST (similar title, totally different package deal), amongst many others.5–8

    With ML Uncertainty, the targets have been to maintain the coaching of the mannequin and its UQ separate, cater to extra generic fashions past linear regression, exploit the underlying statistics of the fashions, and keep away from retraining the mannequin a number of instances to make it computationally cheap.

    Abstract and future work

    This was an introduction to ML Uncertainty—a Python software program package deal to simply compute uncertainties in machine studying. The principle options of this package deal have been launched right here and a number of the philosophy of its improvement has been mentioned. Extra detailed documentation and idea may be discovered within the docs. Whereas that is solely a begin, there’s immense scope to broaden this. Questions, discussions, and contributions are all the time welcome. The code may be discovered on GitHub and the package deal may be put in from PyPi. Give it a attempt with pip set up ml-uncertainty.

    References

    (1) James, G.; Witten, D.; Hastie, T.; Tibshirani, R. An Introduction to Statistical Studying; Springer US: New York, NY, 2021. https://doi.org/10.1007/978-1-0716-1418-1.

    (2) Hastie, T.; Tibshirani, R.; Friedman, J. The Parts of Statistical Studying; Springer New York: New York, NY, 2009. https://doi.org/10.1007/978-0-387-84858-7.

    (3) Börlin, N. Nonlinear Optimization. https://www8.cs.umu.se/kurser/5DA001/HT07/lectures/lsq-handouts.pdf.

    (4) Zhang, H.; Zimmerman, J.; Nettleton, D.; Nordman, D. J. Random Forest Prediction Intervals. Am Stat 2020, 74 (4), 392–406. https://doi.org/10.1080/00031305.2019.1585288.

    (5) Cordier, T.; Blot, V.; Lacombe, L.; Morzadec, T.; Capitaine, A.; Brunel, N. Versatile and Systematic Uncertainty Estimation with Conformal Prediction through the MAPIE Library. In Conformal and Probabilistic Prediction with Functions; 2023.

    (6) Mendil, M.; Mossina, L.; Vigouroux, D. PUNCC: A Python Library for Predictive Uncertainty and Conformalization. In Proceedings of the Twelfth Symposium on Conformal and Probabilistic Prediction with Functions; Papadopoulos, H., Nguyen, Okay. A., Boström, H., Carlsson, L., Eds.; Proceedings of Machine Studying Analysis; PMLR, 2023; Vol. 204, pp 582–601.

    (7) Tsapetis, D.; Shields, M. D.; Giovanis, D. G.; Olivier, A.; Novak, L.; Chakroborty, P.; Sharma, H.; Chauhan, M.; Kontolati, Okay.; Vandanapu, L.; Loukrezis, D.; Gardner, M. UQpy v4.1: Uncertainty Quantification with Python. SoftwareX 2023, 24, 101561. https://doi.org/10.1016/j.softx.2023.101561.

    (8) Sheen, D. Machine Studying Uncertainty Estimation Toolbox. https://github.com/usnistgov/ml_uncertainty_py.

    []



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous Articlewkksx
    Next Article Nvidia Rival FuriosaAI Rejected Meta’s $800 Million Offer
    Team_AIBS News
    • Website

    Related Posts

    Artificial Intelligence

    How to Access NASA’s Climate Data — And How It’s Powering the Fight Against Climate Change Pt. 1

    July 1, 2025
    Artificial Intelligence

    STOP Building Useless ML Projects – What Actually Works

    July 1, 2025
    Artificial Intelligence

    Implementing IBCS rules in Power BI

    July 1, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Top Posts

    How to Access NASA’s Climate Data — And How It’s Powering the Fight Against Climate Change Pt. 1

    July 1, 2025

    I Tried Buying a Car Through Amazon: Here Are the Pros, Cons

    December 10, 2024

    Amazon and eBay to pay ‘fair share’ for e-waste recycling

    December 10, 2024

    Artificial Intelligence Concerns & Predictions For 2025

    December 10, 2024

    Barbara Corcoran: Entrepreneurs Must ‘Embrace Change’

    December 10, 2024
    Categories
    • AI Technology
    • Artificial Intelligence
    • Business
    • Data Science
    • Machine Learning
    • Technology
    Most Popular

    What is Next for Multimodal AI. Multimodal AI is evolving from static… | by M | Foundation Models Deep Dive | Jun, 2025

    June 22, 2025

    What Germany Currently Is Up To, Debt-Wise

    March 21, 2025

    BBC threatens AI firm with legal action over unauthorised content use

    June 20, 2025
    Our Picks

    How to Access NASA’s Climate Data — And How It’s Powering the Fight Against Climate Change Pt. 1

    July 1, 2025

    From Training to Drift Monitoring: End-to-End Fraud Detection in Python | by Aakash Chavan Ravindranath, Ph.D | Jul, 2025

    July 1, 2025

    Using Graph Databases to Model Patient Journeys and Clinical Relationships

    July 1, 2025
    Categories
    • AI Technology
    • Artificial Intelligence
    • Business
    • Data Science
    • Machine Learning
    • Technology
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
    • About us
    • Contact us
    Copyright © 2024 Aibsnews.comAll Rights Reserved.

    Type above and press Enter to search. Press Esc to cancel.