以下是为您生成的 Python 代码示例,用于实现将样本的扰动分为 0、0.001、0.003、0.005 四类的分类器:
import numpy as np
# 第一步:导入需要的库
import numpy as np
# 第二步:加载数据
data = np.load('/public/xdw/ls/signaldefense/AS/perturbed_data.npy')
# 第三步:获取数据样本的扰动值
perturbation_values = data[:, 1] # 假设扰动值在第二列
# 第四步:判断扰动值的大小并计数
count_0 = 0
count_0_001 = 0
count_0_003 = 0
count_0_005 = 0
for value in perturbation_values:
if value == 0:
count_0 += 1
elif value == 0.001:
count_0_001 += 1
elif value == 0.003:
count_0_003 += 1
elif value == 0.005:
count_0_005 += 1
# 第五步:计算正确率
total_samples = len(perturbation_values)
accuracy_0 = count_0 / total_samples
accuracy_0_001 = count_0_001 / total_samples
accuracy_0_003 = count_0_003 / total_samples
accuracy_0_005 = count_0_005 / total_samples
print("扰动值为 0 的正确率:", accuracy_0)
print("扰动值为 0.001 的正确率:", accuracy_0_001)
print("扰动值为 0.003 的正确率:", accuracy_0_003)
print("扰动值为 0.005 的正确率:", accuracy_0_005)