基于pytorch的一维数据的五分类混淆矩阵代码
时间: 2023-11-27 13:02:49 浏览: 150
以下是基于 PyTorch 的一维数据的五分类混淆矩阵代码示例:
```python
import torch
import numpy as np
# 假设有 100 个样本,每个样本的分类标签为 0, 1, 2, 3, 4 中的一个
y_true = torch.randint(low=0, high=5, size=(100,))
# 假设模型预测的分类为随机值,模拟混淆矩阵
y_pred = torch.randint(low=0, high=5, size=(100,))
# 计算混淆矩阵
confusion_matrix = torch.zeros(5, 5)
for i in range(len(y_true)):
confusion_matrix[y_true[i], y_pred[i]] += 1
# 输出混淆矩阵
print("Confusion Matrix:")
print(confusion_matrix)
```
该代码首先生成了 100 个随机标签的样本,以及一个随机预测的分类结果。然后,通过循环计算出混淆矩阵,并输出结果。
需要注意的是,该代码仅适用于一维数据的情况。对于多维数据,需要对每一个维度进行类似的计算。同时,该代码中假设分类标签为 0, 1, 2, 3, 4,如有需要,可以根据实际情况进行修改。
相关问题
基于pytorch的一维数据的五分类混淆矩阵以及各类别准确率代码
以下是基于 PyTorch 的一维数据的五分类混淆矩阵以及各类别准确率代码示例:
```python
import torch
import numpy as np
# 假设有 100 个样本,每个样本的分类标签为 0, 1, 2, 3, 4 中的一个
y_true = torch.randint(low=0, high=5, size=(100,))
# 假设模型预测的分类为随机值,模拟混淆矩阵
y_pred = torch.randint(low=0, high=5, size=(100,))
# 计算混淆矩阵
confusion_matrix = torch.zeros(5, 5)
for i in range(len(y_true)):
confusion_matrix[y_true[i], y_pred[i]] += 1
# 输出混淆矩阵
print("Confusion Matrix:")
print(confusion_matrix)
# 计算各类别准确率
class_accuracy = np.zeros(5)
for i in range(5):
class_accuracy[i] = confusion_matrix[i, i] / confusion_matrix[i].sum()
# 输出各类别准确率
print("Class Accuracy:")
print(class_accuracy)
```
该代码首先生成了 100 个随机标签的样本,以及一个随机预测的分类结果。然后,通过循环计算出混淆矩阵,并输出结果。接着,计算每个类别的准确率,并输出结果。
需要注意的是,该代码仅适用于一维数据的情况。对于多维数据,需要对每一个维度进行类似的计算。同时,该代码中假设分类标签为 0, 1, 2, 3, 4,如有需要,可以根据实际情况进行修改。
基于pytorch的测试一维数据的五分类混淆矩阵以及各类别准确率代码
以下是基于 PyTorch 的测试一维数据的五分类混淆矩阵以及各类别准确率代码示例:
```python
import torch
import numpy as np
# 假设有 100 个样本,每个样本的分类标签为 0, 1, 2, 3, 4 中的一个
y_true = torch.randint(low=0, high=5, size=(100,))
y_pred = torch.randint(low=0, high=5, size=(100,))
def calculate_confusion_matrix(y_true, y_pred):
# 计算混淆矩阵
confusion_matrix = torch.zeros(5, 5)
for i in range(len(y_true)):
confusion_matrix[y_true[i], y_pred[i]] += 1
return confusion_matrix
def calculate_class_accuracy(confusion_matrix):
# 计算各类别准确率
class_accuracy = np.zeros(5)
for i in range(5):
class_accuracy[i] = confusion_matrix[i, i] / confusion_matrix[i].sum()
return class_accuracy
# 计算混淆矩阵和各类别准确率
confusion_matrix = calculate_confusion_matrix(y_true, y_pred)
class_accuracy = calculate_class_accuracy(confusion_matrix)
# 输出混淆矩阵和各类别准确率
print("Confusion Matrix:")
print(confusion_matrix)
print("Class Accuracy:")
print(class_accuracy)
```
该代码首先生成了 100 个随机标签的样本,以及一个随机预测的分类结果。然后,通过调用 `calculate_confusion_matrix()` 函数计算出混淆矩阵,再调用 `calculate_class_accuracy()` 函数计算出各类别准确率,并输出结果。
需要注意的是,该代码中的 `calculate_confusion_matrix()` 和 `calculate_class_accuracy()` 函数可以复用,对于多个模型或多个测试集的结果计算都可以使用这两个函数。同时,该代码中假设分类标签为 0, 1, 2, 3, 4,如有需要,可以根据实际情况进行修改。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)