特征选择matlab代码
时间: 2023-05-31 12:01:35 浏览: 103
以下是一个基于互信息的特征选择matlab代码:
% 导入数据集
load('data.mat');
% 将特征和标签分开
X = data(:, 1:end-1);
Y = data(:, end);
% 计算每个特征与标签的互信息
mi = zeros(1, size(X, 2));
for i = 1:size(X, 2)
mi(i) = mutualinfo(X(:, i), Y);
end
% 按互信息大小排序
[sorted_mi, idx] = sort(mi, 'descend');
% 选择前k个特征
k = 5;
selected_features = idx(1:k);
% 输出结果
disp('Selected features:');
disp(selected_features);
% 训练模型并进行预测
model = fitcsvm(X(:, selected_features), Y);
Y_pred = predict(model, X(:, selected_features));
% 计算准确率
accuracy = sum(Y_pred == Y) / length(Y);
disp('Accuracy:');
disp(accuracy);
阅读全文