1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
from sklearn.model_selection import cross_val_score
cross_val_score(model(),X_train,y_train,cv=3,scoring="accuracy")
from sklearn.model_selection import StratifiedKFold from sklean.base import clone
skfolds = StratifiedKFold(n_splits=3,random_state=42) for train_index,test_index in skfolds.split(X_train,y_train): clone_clf = clone(model()) X_train_folds = X_train[trian_index] y_train_folds = y_train[train_index] X_test_folds = X_train[test_index] y_test_folds= y_train[test_index] clone_clf.fit(X_train_folds,y_train_folds) y_pred = clone_clf.predict(X_test_folds) n_correct = sum(y_pred == y_test_folds) print(n_correct / len(y_pred) )
|