from sklearn.datasets import load_iris |
from sklearn.model_selection import train_test_split |
from sklearn.svm import SVC |
from sklearn.metrics import accuracy_score |
# 加载数据集 |
iris = load_iris() |
X = iris.data |
y = iris.target |
# 划分训练集和测试集 |
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3 , random_state = 42 ) |
# 训练SVM模型 |
clf = SVC(kernel = 'linear' , C = 1 , random_state = 42 ) |
clf.fit(X_train, y_train) |
# 评估模型准确性 |
y_pred = clf.predict(X_test) |
accuracy = accuracy_score(y_test, y_pred) |
print ( "Accuracy:" , accuracy) |