How to check models f1 score using cross validation in Python?
0

How to check models f1 score using cross validation in Python?

This recipe helps you check models f1 score using cross validation in Python
In [1]:
## How to check model's f1-score using cross validation in Python
def Snippet_133():
    print()
    print(format('How to check model\'s f1-score using cross validation in Python','*^82'))

    import warnings
    warnings.filterwarnings("ignore")

    # load libraries
    from sklearn.model_selection import cross_val_score
    from sklearn.tree import DecisionTreeClassifier
    from sklearn.datasets import make_classification

    # Generate features matrix and target vector
    X, y = make_classification(n_samples = 10000,
                               n_features = 3,
                               n_informative = 3,
                               n_redundant = 0,
                               n_classes = 2,
                               random_state = 42)

    # Create Decision Tree model
    dtree = DecisionTreeClassifier()

    # Cross-validate model using accuracy
    print(); print(cross_val_score(dtree, X, y, scoring="f1", cv = 7))
    mean_score = cross_val_score(dtree, X, y, scoring="f1", cv = 7).mean()
    std_score = cross_val_score(dtree, X, y, scoring="f1", cv = 7).std()
    print(); print(mean_score)
    print(); print(std_score)

Snippet_133()
**********How to check model's f1-score using cross validation in Python**********

[0.92522711 0.91240364 0.93596059 0.92329346 0.93239437 0.92696629
 0.92763611]

0.9265242457185211

0.006859748973343737