Last active
January 7, 2020 07:41
-
-
Save lol97/4e4eff1bd77bc79ddd65f686e65c159f to your computer and use it in GitHub Desktop.
Cross-Validation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from sklearn import datasets | |
| from sklearn.model_selection import StratifiedKFold | |
| from sklearn.svm import LinearSVC | |
| iris = datasets.load_iris() | |
| features = iris.data | |
| labels = iris.target | |
| skf = StratifiedKFold(n_splits=5) | |
| fold = 1 | |
| for train_index, test_index in skf.split(features, labels): | |
| X_train, X_test = features[train_index], features[test_index] | |
| y_train, y_test = labels[train_index], labels[test_index] | |
| print("fold {}".format(fold), end="\t") | |
| print("banyak data latih {}".format(len(y_train)), end="\t") | |
| print("banyak data uji {}".format(len(y_test)), end="\t") | |
| #contoh hitung akurasi model linear svm | |
| clf = LinearSVC(C=1, max_iter=10000) | |
| clf.fit(X_train, y_train) | |
| print("akurasi {}".format(clf.score(X_test, y_test))) | |
| fold+=1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment