首先是在 matlab 中训练模型,代码如下
clear all;
clc;
%加载训练 测试数据
y_valid = load('shuju/valid_data_label4.mat');
y_valid = y_valid.valid_data_label4;
x_valid = load('valid_data4.mat');
x_valid = x_valid.valid_data4;
x_train = load('train_data4.mat');
x_train = x_train.train_data4;
y_train = load('train_data_label4.mat');
y_train = y_train.train_data_label4;
%建立决策树 训练得到 模型:ctree
ctree = ClassificationTree.fit(x_train,y_train)
%剪枝
ctree = prune(ctree,'level',20);
%画图
view(ctree,'mode','graph');
%测试结果
y_hat = predict(ctree,x_valid);
acc_test = sum(y_hat == y_valid)/length(y_valid);
disp(['测试准确度:' num2str(acc_test)])
y_train_hat = predict(ctree,x_train);