Recipe: How to check models accuracy using cross validation in Python?
MODEL SELECTION MODEL ACCURACY

How to check models accuracy using cross validation in Python?

This recipe helps you check models accuracy using cross validation in Python
In [1]:
## How to check model's accuracy using cross validation in Python
def Snippet_132():
    print()
    print(format('How to check model\'s accuracy 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="accuracy", cv = 7))
    mean_score = cross_val_score(dtree, X, y, scoring="accuracy", cv = 7).mean()
    std_score = cross_val_score(dtree, X, y, scoring="accuracy", cv = 7).std()
    print(); print(mean_score)
    print(); print(std_score)

Snippet_132()
**********How to check model's accuracy using cross validation in Python**********

[0.92517483 0.91398601 0.93697479 0.92647059 0.92927171 0.92717087
 0.92927171]

0.926103868120675

0.006990971894539273


Stuck at work?
Can't find the recipe you are looking for. Let us know and we will find an expert to create the recipe for you. Click here
Companies using this Recipe
1 developer from HvH
1 developer from Amazon
1 developer from Posidex
1 developer from American Express
1 developer from Vodafone
1 developer from ANAC
1 developer from Wipro