32 lines
963 B
Python
32 lines
963 B
Python
|
from sklearn.datasets import load_iris
|
||
|
from sklearn.model_selection import train_test_split
|
||
|
from sklearn.svm import SVC
|
||
|
import argparse
|
||
|
|
||
|
# 加载数据集
|
||
|
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.2, random_state=42)
|
||
|
|
||
|
# 参数解析
|
||
|
parser = argparse.ArgumentParser(description='SVM Model for Iris dataset')
|
||
|
parser.add_argument('--C', type=float, default=1.0, help='Regularization parameter')
|
||
|
parser.add_argument('--kernel', type=str, default='rbf', help='Kernel type')
|
||
|
parser.add_argument('--gamma', type=float, default='scale', help='Kernel coefficient')
|
||
|
|
||
|
args = parser.parse_args()
|
||
|
|
||
|
# 定义支持向量机模型并设置参数
|
||
|
svm = SVC(C=args.C, kernel=args.kernel, gamma=args.gamma)
|
||
|
|
||
|
# 在训练集上拟合模型
|
||
|
svm.fit(X_train, y_train)
|
||
|
|
||
|
# 输出模型在测试集上的准确率
|
||
|
test_accuracy = svm.score(X_test, y_test)
|
||
|
print(f"Accuracy={test_accuracy}")
|
||
|
|