Close Menu
    Trending
    • Inheritance: A Software Engineering Concept Data Scientists Must Know To Succeed
    • Why OPENAI CODEX Might Be the Technological COVID We Missed | by Abay Serkebayev | May, 2025
    • Cloudera Releases AI-Powered Unified Data Visualization for On-Prem Environments
    • How to Get Your First 1,000 Email Subscribers
    • What Statistics Can Tell Us About NBA Coaches
    • Anthropic’s new hybrid AI model can work on tasks autonomously for hours at a time
    • Model Context Protocol (MCP): The Universal Connector for AI Applications | by AJG | May, 2025
    • How Brands Can Master Bluesky and Capitalize on Its Growing Audience
    Finance StarGate
    • Home
    • Artificial Intelligence
    • AI Technology
    • Data Science
    • Machine Learning
    • Finance
    • Passive Income
    Finance StarGate
    Home»Machine Learning»ML Threshold Tuning with 5-Fold Stratified Cross-Validation! | by Hourglassdatalab | Apr, 2025
    Machine Learning

    ML Threshold Tuning with 5-Fold Stratified Cross-Validation! | by Hourglassdatalab | Apr, 2025

    FinanceStarGateBy FinanceStarGateApril 5, 2025No Comments2 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Share
    Facebook Twitter LinkedIn Pinterest Email


    Scikit-learn 1.5+ launched a brilliant handy solution to tune choice thresholds immediately utilizing TunedThresholdClassifierCV.

    Let’s stroll via an instance utilizing the favored breast most cancers dataset.

    In medical purposes like breast most cancers detection, false negatives (missed constructive instances) are much more harmful than false positives. That is why we’ll use the F2 rating, which weights recall larger than precision, making it ideally suited for this situation.

    Let’s dive into the code:

    import pandas as pd
    from sklearn.datasets import load_breast_cancer
    from sklearn.model_selection import TunedThresholdClassifierCV, train_test_split
    from sklearn.metrics import make_scorer, fbeta_score
    from sklearn.preprocessing import StandardScaler
    from sklearn.linear_model import LogisticRegression

    import warnings
    warnings.filterwarnings('ignore')

    RANDOM_STATE = 000

    knowledge = load_breast_cancer()
    X, y = knowledge.knowledge, knowledge.goal

    # Outline an F2 scorer to emphasise recall (beta=2)
    f2_scorer = make_scorer(fbeta_score, beta=2)

    X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.3, stratify=y, random_state=RANDOM_STATE
    )

    # Standardize the function knowledge
    scaler = StandardScaler()
    X_train = scaler.fit_transform(X_train)
    X_test = scaler.remodel(X_test)

    # Arrange a base classifier
    base_clf = LogisticRegression()
    base_clf.match(X_train, y_train)
    base_pred = base_clf.predict(X_test)

    print("F2 rating on take a look at (unseen) set:", fbeta_score(y_test, base_pred, beta=2)

    F2 rating on take a look at (unseen) set: 0.9644194756554307

    # Configure the TunedThresholdClassifierCV to optimize the F2 rating
    tuned_clf = TunedThresholdClassifierCV(
    estimator=base_clf,
    scoring=f2_scorer,
    cv=5,
    refit=True,
    store_cv_results=True,
    random_state=RANDOM_STATE
    )

    tuned_clf.match(X_train, y_train)
    tuned_preds = tuned_clf.predict(X_test)

    # Print the optimum threshold decided by cross-validation
    print("Optimum threshold:", tuned_clf.best_threshold_)
    print("F2 rating on take a look at (unseen) set:", fbeta_score(y_test, tuned_preds, beta=2))

    Optimum threshold: 0.40404039630244054
    F2 rating on take a look at (unseen) set: 0.9776536312849162

    Given the scientific implications, lacking a most cancers analysis carries larger penalties than false positives, making the F2 rating a perfect analysis metric as a consequence of its give attention to recall. Our experimentation with transferring the brink efficiently boosted the F2 rating from 0.9644 to 0.9777 on the take a look at set, validating the worth of threshold tuning.

    For extra detailed details about API utilization, check with scikit-learn’s docs.



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleHow To Make Money Fast Real Ways To Make Money Quickly
    Next Article How One Simple Question Altered the Trajectory of This Entrepreneur’s Life
    FinanceStarGate

    Related Posts

    Machine Learning

    Why OPENAI CODEX Might Be the Technological COVID We Missed | by Abay Serkebayev | May, 2025

    May 22, 2025
    Machine Learning

    Model Context Protocol (MCP): The Universal Connector for AI Applications | by AJG | May, 2025

    May 22, 2025
    Machine Learning

    🤖 HATERS? NO PROBLEM. NO LIKEY ROBOT? YOU DON’T GET ONE. EVER. You heard me. – NickyCammarata

    May 22, 2025
    Add A Comment

    Comments are closed.

    Top Posts

    15 DIY SEO Strategies That Boosted My Startup’s Visibility

    April 12, 2025

    Smarter Ancestry AI. How MIT’s Breakthrough Could Transform… | by RizesGen | May, 2025

    May 6, 2025

    Gjfhh

    February 6, 2025

    The Good-Enough Truth | Towards Data Science

    April 17, 2025

    Get a ChatGPT + Automation E-Degree for Just $20

    March 30, 2025
    Categories
    • AI Technology
    • Artificial Intelligence
    • Data Science
    • Finance
    • Machine Learning
    • Passive Income
    Most Popular

    Circuit Tracing: A Step Closer to Understanding Large Language Models

    April 9, 2025

    Compression Power is Intelligence | by Andy Yu

    April 7, 2025

    How to Build a Team That Thinks and Executes Like a Founder

    May 2, 2025
    Our Picks

    Your Business Needs AI—But Not 50 Different Apps

    March 22, 2025

    InFlux Technologies Debuts AI-Based Document Intelligence

    February 15, 2025

    Tax Cuts for the Middle Class and Retirees: A Win for Your Wallet

    February 7, 2025
    Categories
    • AI Technology
    • Artificial Intelligence
    • Data Science
    • Finance
    • Machine Learning
    • Passive Income
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
    • About us
    • Contact us
    Copyright © 2025 Financestargate.com All Rights Reserved.

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