Close Menu
    Trending
    • Entrepreneur+ Subscriber-Only Event | May 28: How This Founder Sold 3 Million Units of His Toy Ball Idea
    • Google’s AlphaEvolve: Getting Started with Evolutionary Coding Agents
    • The Logic Gap: AI Insights vs. Policy Actions | by Sheedeh Rahimi | May, 2025
    • Klarna Uses an AI Clone of Its CEO to Summarize Earnings
    • 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
    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

    The Logic Gap: AI Insights vs. Policy Actions | by Sheedeh Rahimi | May, 2025

    May 22, 2025
    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
    Add A Comment

    Comments are closed.

    Top Posts

    Are You Ready to Go Viral? 4 Ways to Navigate Overnight Growth

    April 21, 2025

    Accepting A Preemptive Offer vs. Listing On The Open Market

    May 21, 2025

    Protecting Digital Assets with Advanced Technology

    February 17, 2025

    DeepSeek-R1 İnceleme. Geçtiğimiz haftalarda OpenAI’nın o1–127… | by Ümit | Feb, 2025

    February 2, 2025

    A Simple Implementation of the Attention Mechanism from Scratch

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

    Exploring the Slope of Straight Lines in Differential Calculus | by Yokeswaran | Mar, 2025

    March 17, 2025

    Smash Your Way to Success with an iSmash Rage Room Franchise

    April 9, 2025

    Newton’s Method in Focus: How a Machine Learning Lesson Sparked AI Crypto Market Shifts on March 13, 2025 | by ButerinBard | Mar, 2025

    March 14, 2025
    Our Picks

    How to Solve Machine Learning Case Studies: Cracking Fraud Detection in Data Science Interviews | by Ancienthorse | Feb, 2025

    February 27, 2025

    Fiveonefour Unveils Aurora AI Agents for Data Engineering

    April 3, 2025

    The Easy Way to Make Managing Your Rental Property Stress Free is Just $39

    February 1, 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.