Matlab:SOM和kmeans可以应用于二值化时间序列数据吗?

时间:2016-12-04 07:43:23

标签: matlab neural-network cluster-analysis k-means self-organizing-maps

我在这里找到了一个类似的问题Determining cluster membership in SOM (Self Organizing Map) for time series data

我想学习如何在二值化中应用自组织映射或为数据分配2种以上的符号。

例如,让data = rand(100,1)一般情况下,我会做data_quantized = 2*(data>=0.5)-1来获得二进制值变换序列,其中假设并修复了阈值0.5。可能使用多于2个符号来量化数据。可以使用kmeans或SOM来完成这项任务吗?如果我在量化数据时使用SOM,输入和输出应该是什么?

X = {x_i(t)}对于i = 1:N和t = 1:T个时间序列,N表示组件/变量的数量。要获得任何向量x_i的量化值,请使用最接近的BMU的值。量化误差将是输入矢量和最佳匹配模型之差的欧几里德范数。然后使用时间序列的符号表示来比较/匹配新的时间序列。 WOuld BMU是标量值数还是浮点数的向量?很难想象SOM正在做什么。

Matlab实施https://www.mathworks.com/matlabcentral/fileexchange/39930-self-organizing-map-simple-demonstration

我无法理解如何在量化中使用时间序列。假设N = 1是从白噪声过程中获得的一维元素/矢量元素,我如何使用自组织映射对这些数据进行量化/分区?

http://www.mathworks.com/help/nnet/ug/cluster-with-self-organizing-map-neural-network.html

由Matlab提供,但它适用于N维数据,但我有一个包含1000个数据点的一维数据(t = 1,...,1000)。

如果提供一个解释如何将时间序列量化为多个级别的玩具示例,那将是非常有帮助的。设,trainingData = x_i;

T = 1000;
N = 1;
x_i = rand(T,N)  ;

如何应用SOM下面的代码,以便数值数据可以用1,2,3这样的符号表示,即使用3个符号聚类?数据点(标量值)可以用符号1或2或3表示。

function som = SOMSimple(nfeatures, ndim, nepochs, ntrainingvectors, eta0, etadecay, sgm0, sgmdecay, showMode)
%SOMSimple Simple demonstration of a Self-Organizing Map that was proposed by Kohonen.
%   sommap = SOMSimple(nfeatures, ndim, nepochs, ntrainingvectors, eta0, neta, sgm0, nsgm, showMode) 
%   trains a self-organizing map with the following parameters
%       nfeatures        - dimension size of the training feature vectors
%       ndim             - width of a square SOM map
%       nepochs          - number of epochs used for training
%       ntrainingvectors - number of training vectors that are randomly generated
%       eta0             - initial learning rate
%       etadecay         - exponential decay rate of the learning rate
%       sgm0             - initial variance of a Gaussian function that
%                          is used to determine the neighbours of the best 
%                          matching unit (BMU)
%       sgmdecay         - exponential decay rate of the Gaussian variance 
%       showMode         - 0: do not show output, 
%                          1: show the initially randomly generated SOM map 
%                             and the trained SOM map,
%                          2: show the trained SOM map after each update
%
%   For example: A demonstration of an SOM map that is trained by RGB values
%           
%       som = SOMSimple(1,60,10,100,0.1,0.05,20,0.05,2);
%       % It uses:
%       %   1    : dimensions for training vectors
%       %   60x60: neurons
%       %   10   : epochs
%       %   100  : training vectors
%       %   0.1  : initial learning rate
%       %   0.05 : exponential decay rate of the learning rate
%       %   20   : initial Gaussian variance
%       %   0.05 : exponential decay rate of the Gaussian variance
%       %   2    : Display the som map after every update

nrows = ndim;
ncols = ndim;
nfeatures = 1;
som = rand(nrows,ncols,nfeatures);


% Generate random training data
    x_i = trainingData;

% Generate coordinate system
[x y] = meshgrid(1:ncols,1:nrows);

for t = 1:nepochs    
    % Compute the learning rate for the current epoch
    eta = eta0 * exp(-t*etadecay);        

    % Compute the variance of the Gaussian (Neighbourhood) function for the ucrrent epoch
    sgm = sgm0 * exp(-t*sgmdecay);

    % Consider the width of the Gaussian function as 3 sigma
    width = ceil(sgm*3);        

    for ntraining = 1:ntrainingvectors
        % Get current training vector
        trainingVector = trainingData(ntraining,:);

        % Compute the Euclidean distance between the training vector and
        % each neuron in the SOM map
        dist = getEuclideanDistance(trainingVector, som, nrows, ncols, nfeatures);

        % Find the best matching unit (bmu)
        [~, bmuindex] = min(dist);

        % transform the bmu index into 2D
        [bmurow bmucol] = ind2sub([nrows ncols],bmuindex);        

        % Generate a Gaussian function centered on the location of the bmu
        g = exp(-(((x - bmucol).^2) + ((y - bmurow).^2)) / (2*sgm*sgm));

        % Determine the boundary of the local neighbourhood
        fromrow = max(1,bmurow - width);
        torow   = min(bmurow + width,nrows);
        fromcol = max(1,bmucol - width);
        tocol   = min(bmucol + width,ncols);

        % Get the neighbouring neurons and determine the size of the neighbourhood
        neighbourNeurons = som(fromrow:torow,fromcol:tocol,:);
        sz = size(neighbourNeurons);

        % Transform the training vector and the Gaussian function into 
        % multi-dimensional to facilitate the computation of the neuron weights update
        T = reshape(repmat(trainingVector,sz(1)*sz(2),1),sz(1),sz(2),nfeatures);                   
        G = repmat(g(fromrow:torow,fromcol:tocol),[1 1 nfeatures]);

        % Update the weights of the neurons that are in the neighbourhood of the bmu
        neighbourNeurons = neighbourNeurons + eta .* G .* (T - neighbourNeurons);

        % Put the new weights of the BMU neighbouring neurons back to the
        % entire SOM map
        som(fromrow:torow,fromcol:tocol,:) = neighbourNeurons;


    end
end


function ed = getEuclideanDistance(trainingVector, sommap, nrows, ncols, nfeatures)

% Transform the 3D representation of neurons into 2D
neuronList = reshape(sommap,nrows*ncols,nfeatures);               

% Initialize Euclidean Distance
ed = 0;
for n = 1:size(neuronList,2)
    ed = ed + (trainingVector(n)-neuronList(:,n)).^2;
end
ed = sqrt(ed);

1 个答案:

答案 0 :(得分:2)

我不知道我可能会误解你的问题,但从我的理解来看,它非常简单,包括kmeans和Matlab自己的selforgmap。您为SOMSimple发布的实现我无法评论。

我们来看你最初的例子:

rng(1337);
T = 1000;
x_i = rand(1,T); %rowvector for convenience

假设您想要量化为三个符号,您的手动版本可能是:

nsyms = 3;
symsthresh = [1:-1/nsyms:1/nsyms];
x_i_q = zeros(size(x_i));

for i=1:nsyms
    x_i_q(x_i<=symsthresh(i)) = i;
end

使用Matlab自己的selforgmap,您可以获得类似的结果:

net = selforgmap(nsyms);
net.trainParam.showWindow = false;
net = train(net,x_i);
net(x_i);
y = net(x_i);
classes = vec2ind(y);

最后,使用kmeans

可以直接完成相同的操作
clusters = kmeans(x_i',nsyms)';