python训练SVM并输出混淆矩阵
时间: 2023-07-13 15:13:52 浏览: 110
好的,下面是使用Python训练SVM并输出混淆矩阵的示例代码:
```python
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn.svm import SVC
# 加载数据集
iris = datasets.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模型
svm = SVC(kernel='linear', C=1, random_state=42)
svm.fit(X_train, y_train)
# 预测测试集结果
y_pred = svm.predict(X_test)
# 输出混淆矩阵
cm = confusion_matrix(y_test, y_pred)
print(cm)
```
在示例代码中,我们使用了sklearn库中的iris数据集,并使用train_test_split函数将其划分为训练集和测试集。然后,我们使用SVC函数训练SVM模型,并使用predict函数预测测试集结果。最后,我们使用confusion_matrix函数输出混淆矩阵。
相关问题
SVM预测后分别输出训练集上的混淆矩阵和测试集上的混淆矩阵
可以使用以下代码来输出训练集上的混淆矩阵和测试集上的混淆矩阵:
```python
from sklearn.metrics import confusion_matrix
# 训练集上的混淆矩阵
train_pred = svm_clf.predict(X_train)
train_conf_mx = confusion_matrix(y_train, train_pred)
print("Train Confusion Matrix:\n", train_conf_mx)
# 测试集上的混淆矩阵
test_pred = svm_clf.predict(X_test)
test_conf_mx = confusion_matrix(y_test, test_pred)
print("Test Confusion Matrix:\n", test_conf_mx)
```
其中,`X_train` 和 `y_train` 分别是训练集的特征和标签,`X_test` 和 `y_test` 分别是测试集的特征和标签。`svm_clf` 是训练好的 SVM 分类器。`confusion_matrix` 函数可以计算混淆矩阵。输出的结果中,行表示真实类别,列表示预测类别。例如,`conf_mx[i, j]` 表示真实类别为 i,预测类别为 j 的样本数。
python训练三分类SVM
好的,下面是使用Python训练三分类SVM的示例代码:
```python
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn.svm import SVC
# 加载数据集
iris = datasets.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模型
svm = SVC(kernel='linear', C=1, random_state=42)
svm.fit(X_train, y_train)
# 预测测试集结果
y_pred = svm.predict(X_test)
# 输出混淆矩阵
cm = confusion_matrix(y_test, y_pred)
print(cm)
```
在示例代码中,我们使用了sklearn库中的iris数据集,并使用train_test_split函数将其划分为训练集和测试集。然后,我们使用SVC函数训练SVM模型,并使用predict函数预测测试集结果。最后,我们使用confusion_matrix函数输出混淆矩阵。
需要注意的是,在上述示例代码中,我们使用了线性核函数,适用于线性可分的情况。如果数据集不是线性可分的,可以使用其他核函数,例如高斯核函数。同时,在训练SVM模型时,需要设置参数C和gamma,以及选择合适的核函数和参数,以达到最好的分类效果。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)