python LinearSVC代码实现三分类
时间: 2023-10-14 17:15:33 浏览: 37
下面是一个简单的示例代码,使用LinearSVC进行三分类:
```python
from sklearn.svm import LinearSVC
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
# 生成数据集
X, y = make_classification(n_samples=1000, n_features=10, n_classes=3, n_informative=5, random_state=42)
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 使用LinearSVC进行训练
clf = LinearSVC()
clf.fit(X_train, y_train)
# 在测试集上进行预测
y_pred = clf.predict(X_test)
# 输出预测准确率
print("Accuracy:", clf.score(X_test, y_test))
```
在这个示例中,我们使用了sklearn中的make_classification函数来生成一个分类问题的数据集,其中有10个特征和3个类别。然后我们使用train_test_split函数将数据集划分为训练集和测试集,并使用LinearSVC模型进行训练。最后,我们在测试集上进行预测,并输出预测准确率。