% 导入数据集(特征矩阵X和标签向量y),并进行数据预处理
load(‘dataset.mat’); % 替换为实际的数据集文件
% 数据归一化(假设使用MinMax归一化)
minX = min(X);
maxX = max(X);
X_normalized = (X - minX) ./ (maxX - minX);
% 将标签向量转换为独热编码表示
numClasses = max(y);
y_onehot = zeros(length(y), numClasses);
for i = 1:length(y)
y_onehot(i, y(i)) = 1;
end
% 将数据集划分为训练集和测试集
trainRatio = 0.8; % 训练集比例
numTrainSamples = round(trainRatio * size(X_normalized, 1));
X_train = X_normalized(1:numTrainSamples, 😃;
y_train = y_onehot(1:numTrainSamples, 😃;
X_test = X_normalized(numTrainSamples+1:end, 😃;
y_test = y_onehot(numTrainSamples+1:end, 😃;
% 设置PLS的参数
numComponents = 5; % 主成分数量
% 构建PLS模型
plsModel = plsregress(X_train, y_train, numComponents);
% 在训练集上进行预测
y_train_pred = [ones(size(X_train, 1), 1) X_train] * plsModel;
% 在测试集上进行预测
y_test_pred = [ones(size(X_test, 1), 1) X_test] * plsModel;
% 将预测结果转换为分类标签
[~, y_train_pred_labels] = max(y_train_pred, [], 2);
[~, y_test_pred_labels] = max(y_test_pred, [], 2);
% 计算分类准确率
trainAccuracy = sum(y_train_pred_labels == find(y_train’)) / numel(y_train);
testAccuracy = sum(y_test_pred_labels == find(y_test’)) / numel(y_test);
disp([‘训练集分类准确率:’, num2str(trainAccuracy)]);
disp([‘测试集分类准确率:’, num2str(testAccuracy)]);