使用数据库的最近原型 Matlab 实现
Nearest Prototype Matlab implementation using a database
我是 matlab 的新手,我想做的是:我有一个数据库 http://archive.ics.uci.edu/ml/machine-learning-databases/wine/,我希望能够在 matlab 中实现 NP,最近原型算法。有没有人可以给我一些建议或一些 link NP 示例用法?
这是一个示例代码,我假设这个数据集的第一列是标签,其余是它的特征
data = load('wine.data');
% split the dataset to training and testing
data = data(randperm(end), :);
train = data(1:floor(0.7*size(data, 1)), :);
test = data(floor(0.7*size(data, 1))+1:end, :);
% training phase
% --------------------------------------------------------
% initialize the centroid, the first column is the label
centroid = [unique(data(:, 1)) zeros(size(unique(data(:, 1)), 1), size(data, 2)-1)];
for label = unique(train(:, 1))'
% collect all the data of under the label
train(train(:, 1) == label, 2:end)
% compute the centroid for the label
centroid(centroid(:, 1) == label, 2:end) = mean(train(train(:, 1) == label, 2:end));
end
% testing phase
% --------------------------------------------------------
% initialize the prediction result
pre_result = zeros(size(test, 1), 1);
for i = 1:size(test, 1)
dist = pdist2(test(i, 2:end), centroid(:, 2:end));
[~, templabel] = min(dist);
pre_result(i) = centroid(templabel, 1);
end
我是 matlab 的新手,我想做的是:我有一个数据库 http://archive.ics.uci.edu/ml/machine-learning-databases/wine/,我希望能够在 matlab 中实现 NP,最近原型算法。有没有人可以给我一些建议或一些 link NP 示例用法?
这是一个示例代码,我假设这个数据集的第一列是标签,其余是它的特征
data = load('wine.data');
% split the dataset to training and testing
data = data(randperm(end), :);
train = data(1:floor(0.7*size(data, 1)), :);
test = data(floor(0.7*size(data, 1))+1:end, :);
% training phase
% --------------------------------------------------------
% initialize the centroid, the first column is the label
centroid = [unique(data(:, 1)) zeros(size(unique(data(:, 1)), 1), size(data, 2)-1)];
for label = unique(train(:, 1))'
% collect all the data of under the label
train(train(:, 1) == label, 2:end)
% compute the centroid for the label
centroid(centroid(:, 1) == label, 2:end) = mean(train(train(:, 1) == label, 2:end));
end
% testing phase
% --------------------------------------------------------
% initialize the prediction result
pre_result = zeros(size(test, 1), 1);
for i = 1:size(test, 1)
dist = pdist2(test(i, 2:end), centroid(:, 2:end));
[~, templabel] = min(dist);
pre_result(i) = centroid(templabel, 1);
end