让代码最后输出百分号形式data = readmatrix('iris.csv'); train_data = reshape(data(1:40,:), [], 4); train_label = ones(120,1); test_data = reshape(data(41:150,:), [], 4); test_label = ones(30, 1); for i = 2:3 start_index = (i-1)50+1; end_index = i50; train_data = [train_data; reshape(data(start_index:start_index+39,:), [], 4)]; train_label = [train_label; i .* ones(40,1)]; test_data = [test_data; reshape(data(start_index+40:end_index,:), [], 4)]; test_label = [test_label; i .* ones(10,1)]; end nb = fitcnb(train_data,train_label); pred_label = predict(nb,test_data); acc = sum(pred_label==test_label) / length(test_label); disp(['Accuracy = ' num2str(acc)]);
时间: 2023-11-30 09:02:32 浏览: 100
tensorflow下的图片标准化函数per_image_standardization用法
代码如下:
```
data = readmatrix('iris.csv');
train_data = reshape(data(1:40,:), [], 4);
train_label = ones(120,1);
test_data = reshape(data(41:150,:), [], 4);
test_label = ones(30, 1);
for i = 2:3
start_index = (i-1)*50+1;
end_index = i*50;
train_data = [train_data; reshape(data(start_index:start_index+39,:), [], 4)];
train_label = [train_label; i .* ones(40,1)];
test_data = [test_data; reshape(data(start_index+40:end_index,:), [], 4)];
test_label = [test_label; i .* ones(10,1)];
end
nb = fitcnb(train_data,train_label);
pred_label = predict(nb,test_data);
acc = sum(pred_label==test_label) / length(test_label);
disp(['Accuracy = ' num2str(acc*100) '%']);
```
注意,需要将准确率acc乘以100,然后输出为百分号形式,因此在disp函数中需要使用字符串拼接的方式,将准确率转换为字符串后再加上百分号符号。
阅读全文