将后面代码绘制的图保存的代码from sklearn.metrics import confusion_matrix import seaborn as sns import matplotlib.pyplot as plt # 预测结果和真实结果 y_true = [0, 1, 2, 0, 1, 2, 0, 1, 2] y_pred = [0, 1, 1, 0, 2, 1, 0, 2, 2] # 计算混淆矩阵 cm = confusion_matrix(y_true, y_pred) # 可视化混淆矩阵 sns.set() sns.heatmap(cm, annot=True, cmap='Blues') plt.xlabel('Predicted label') plt.ylabel('True label') plt.show()
时间: 2024-01-13 08:04:37 浏览: 105
以下是将代码中绘制的图保存到本地的代码:
```python
from sklearn.metrics import confusion_matrix
import seaborn as sns
import matplotlib.pyplot as plt
# 预测结果和真实结果
y_true = [0, 1, 2, 0, 1, 2, 0, 1, 2]
y_pred = [0, 1, 1, 0, 2, 1, 0, 2, 2]
# 计算混淆矩阵
cm = confusion_matrix(y_true, y_pred)
# 可视化混淆矩阵
sns.set()
sns.heatmap(cm, annot=True, cmap='Blues')
plt.xlabel('Predicted label')
plt.ylabel('True label')
plt.savefig('confusion_matrix.png')
plt.show()
```
这段代码会将绘制的混淆矩阵保存为当前工作路径下的 `confusion_matrix.png` 文件。您可以根据需要修改文件名和路径。
相关问题
import pandas as pd import numpy as np import scipy.stats as stats import seaborn as sns from sklearn.metrics import RocCurveDisplay from sklearn.model_selection import train_test_split from sklearn.metrics import confusion_matrix,accuracy_score from sklearn.preprocessing import StandardScaler from sklearn.linear_model import LogisticRegression from sklearn import tree from sklearn.decomposition import PCA import matplotlib.pyplot as plt from sklearn.tree import DecisionTreeClassifier请在此基础上续写代码块,要求是(1) 读入数据后,选取自变量"sysBP", "diaBP","age","totChol","BMI", "heartRate", "glucose"记为X,因变量"TenYearCHD"记为y,组成新的数据集。¶
import pandas as pd
import numpy as np
import scipy.stats as stats
import seaborn as sns
from sklearn.metrics import RocCurveDisplay
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix,accuracy_score
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn import tree
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
from sklearn.tree import DecisionTreeClassifier
# 读入数据
data = pd.read_csv("heart.csv")
# 选取自变量
X = data[["sysBP", "diaBP", "age", "totChol", "BMI", "heartRate", "glucose"]]
# 因变量
y = data["TenYearCHD"]
# 组成新的数据集
new_data = pd.concat([X, y], axis=1)
# 打印新的数据集
print(new_data.head())
import numpy as np import paddle as paddle import paddle.dataset.mnist as mnist import paddle.fluid as fluid from PIL import Image import matplotlib.pyplot as plt from pathlib import Path import os from paddle.vision.datasets import DatasetFolder,ImageFolder from paddle.vision.transforms import Compose,Resize,Transpose import paddle.nn.functional as F from sklearn.metrics import confusion_matrix,f1_score,classification_report import seaborn as sns
这段代码看起来是在导入需要用到的Python库,包括numpy、paddle、PIL、matplotlib、pathlib、os、sklearn等。其中paddle库是深度学习框架PaddlePaddle的Python API,paddle.fluid是PaddlePaddle的核心模块,paddle.dataset.mnist是PaddlePaddle内置的手写数字MNIST数据集,paddle.vision.datasets是PaddlePaddle内置的视觉数据集,paddle.vision.transforms是PaddlePaddle内置的图像变换函数,paddle.nn.functional是PaddlePaddle内置的函数式API模块。而sklearn.metrics则是sklearn库的metrics模块,包含了一些常用的度量指标函数,如混淆矩阵、F1-Score等。sns则是seaborn库,用于绘制统计图表。
阅读全文