1) Вот полный пример использования iris
набора данных с train-test spliting
.
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm, datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
# import data
iris = datasets.load_iris()
X = iris.data
y = iris.target
class_names = iris.target_names
#keep only 2 classes to make the problem binary
X = X[y!=2]
y = y[y!=2]
# Split the data into a training set and a test set
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
# Fit the classifier using the training data
classifier = svm.SVC(kernel='linear', C=0.01)
classifier.fit(X_train, y_train)
# Predict using the trained classifier and the test data
y_pred = classifier.predict(X_test)
print(classification_report(y_test, y_pred, target_names=class_names))
precision recall f1-score support
setosa 1.00 1.00 1.00 13
versicolor 1.00 1.00 1.00 12
avg / total 1.00 1.00 1.00 25
2) Вот полный пример использования iris
набора данных с KFold cross-validation
.
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm, datasets
from sklearn.metrics import classification_report
from sklearn.model_selection import cross_val_predict
from sklearn.model_selection import KFold
# import data
iris = datasets.load_iris()
X = iris.data
y = iris.target
class_names = iris.target_names
# keep only 2 classes to make the problem binary
X = X[y!=2]
y = y[y!=2]
# Define the classifier
classifier = svm.SVC(kernel='linear', C=0.01)
# KFold cross validation
cv = KFold(n_splits=3)
y_pred = cross_val_predict(classifier, X, y, cv = cv)
print(classification_report(y, y_pred, target_names=class_names))
precision recall f1-score support
setosa 0.98 1.00 0.99 50
versicolor 1.00 0.98 0.99 50
avg / total 0.99 0.99 0.99 100