Search code examples
matlabwekaactivity-recognition

Feature Exraction in Matlab then importing into Weka


I have captured data from an accelerometer x,y,z. I am doing activity recognition and have separated the data into sit,run,jump.

I am now windowing the data and extracting features such as the mean, variance etc and then saving it to a features.txt file,then I am going to import the data into Weka software to be classified.

I am looking for help on the file structure to be imported into Weka as I am not sure which one is correct for use with Weka.

Should the features be stored in one txt file or seperate?

Do they need their own columns: mean_x mean_y mean_z var_x var_y var_z Label? or the setup I have below?

Example of Weka:

   @relation datashimmer
    @attribute x numeric

@attribute y numeric

@attribute z numeric

}@attribute class { sitmean, jumpmean ,  runmean, sitstd, jumpstd, runstd, sitvar, jumpvar, runvar, sitrange, jumprange, runrange


@data
   9.0706440e+00   7.8577383e-01   2.4532359e+00   jumpmean
   9.6451465e+00   7.8731349e-01   3.1504403e+00   jumpmean
   8.1915240e+00   9.9549126e-01   2.4723417e+00   jumpmean
   8.6773829e+00   6.3727150e-01   3.0240603e+00   jumpmean
   8.6675553e+00   7.3380330e-01   3.0251476e+00   jumpmean
   8.6544917e+00   8.4768398e-01   2.8385212e+00   jumpmean
   9.5032030e+00   1.1324107e+00   2.1942181e+00   jumpmean
   9.5477273e+00   1.0467068e+00   2.0262543e+00   runmean
   9.5686235e+00   1.1320179e+00   1.8989209e+00   runmean
   9.2875020e+00   8.6683897e-01   1.9064545e+00   runmean
   9.3515172e+00   9.2082513e-01   2.2105460e+00   runmean
   9.6041949e+00   9.2349286e-01   2.5863699e+00   runmean
   9.6029677e+00   7.4794280e-01   2.3176337e+00   runmean
   9.1105362e+00   9.0374931e-01   2.1625089e+00   runmean
  -5.0683414e-01  -4.4845132e+00   8.8514156e+00  sitmean
  -4.9025531e-01  -4.4989652e+00   8.8622800e+00  sitmean
  -4.9871921e-01  -4.4879779e+00   8.8922710e+00  sitmean
  -5.0798669e-01  -4.4828028e+00   8.8459366e+00  sitmean
  -5.4927840e-01  -4.4699890e+00   8.8474149e+00  sitmean
  -5.3211037e-01  -4.4591018e+00   8.8433826e+00  sitmean
  -5.5223809e-01  -4.4869555e+00   8.8594771e+00  sitmean
  -4.7530904e-01  -4.4812295e+00   8.8135292e+00  sitmean
  -5.4696903e-01  -4.4788825e+00   8.8484514e+00  sitmean
  -5.5149171e-01  -4.4587802e+00   8.8630469e+00  sitmean
  -5.3465224e-01  -4.4478393e+00   8.8383810e+00  sitmean
  -5.1742062e-01  -4.4637259e+00   8.8325696e+00  sitmean
  -5.4154869e-01  -4.4833639e+00   8.8562804e+00  sitmean
  -5.7514668e-01  -4.4912760e+00   8.8727185e+00  sitmean
   9.3696539e+00   1.1837913e+00  -2.6525999e+00   jumpmean
   8.6643644e+00   1.2731911e+00  -2.4803529e+00   jumpmean
   8.0837365e+00   1.9774159e+00  -2.8276019e+00   jumpmean
   8.0305034e+00   1.2057473e+00  -1.9911895e+00   jumpmean
   7.9512264e+00   4.5797049e+00  -8.0403707e-01   jumpmean
   8.5823014e+00   3.9339561e+00  -2.6684712e+00   jumpmean
   8.2940896e+00   2.1376560e+00  -2.7870077e+00   jumpmean
   9.4819739e+00   8.1379138e-01  -3.1084057e+00   runmean
   9.0599416e+00   3.9622206e-01  -3.4679528e+00   runmean
   9.3782963e+00   9.4021813e-01  -3.3614160e+00   runmean
   9.4959636e+00   8.3451458e-01  -3.0700251e+00   runmean
   9.3596252e+00   7.6187722e-01  -3.3890264e+00   runmean
   9.4984387e+00   1.1694115e+00  -3.3733935e+00   runmean
   8.9490648e+00   1.4501386e+00  -3.1231425e+00   runmean
   8.3033044e+00  -8.7385271e-01   4.9949720e+00  sitmean
   8.2657411e+00  -9.2072139e-01   5.0284777e+00  sitmean
   8.2039554e+00  -9.1552295e-01   5.1517434e+00  sitmean
   8.2272207e+00  -8.4401430e-01   5.1535285e+00  sitmean
   8.2654827e+00  -7.9834045e-01   5.0608142e+00  sitmean
   8.2999421e+00  -8.6703733e-01   5.0902732e+00  sitmean
   8.2944043e+00  -8.0556494e-01   5.0225056e+00  sitmean
   8.5033422e+00   3.0787253e+00   3.5076829e+00   jumpstd
   7.6895407e+00   3.1243470e+00   3.0559549e+00   jumpstd
   7.5029557e+00   4.0664196e+00   4.1725267e+00   jumpstd
   7.5436436e+00   4.3278471e+00   3.8818316e+00   jumpstd
   6.1032521e+00   4.6354434e+00   3.5036980e+00   jumpstd
   7.4142339e+00   4.0956300e+00   3.7602181e+00   jumpstd
   7.9337673e+00   3.2664252e+00   4.2575543e+00   jumpstd
   5.0545343e+00   2.8901073e+00   3.5430258e+00   runstd
   5.3394208e+00   2.1666505e+00   3.7298383e+00   runstd
   4.8233755e+00   3.0923609e+00   3.4364285e+00   runstd
   5.3904459e+00   2.6258827e+00   3.3256969e+00   runstd
   4.3812033e+00   2.7708500e+00   2.5985276e+00   runstd
   4.1402607e+00   4.0070005e+00   2.7039632e+00   runstd
   4.2720294e+00   4.6312551e+00   4.4228231e+00   runstd
   2.7539715e-01   2.3209201e-01   3.2587558e-01   sitstd
   1.5065993e-01   1.7074749e-01   1.7440757e-01   sitstd
   1.6740466e-01   1.5952978e-01   1.6872027e-01   sitstd
   1.6208795e-01   1.6157018e-01   1.5045205e-01   sitstd
   1.8287180e-01   1.7500556e-01   1.9729539e-01   sitstd
   1.8928747e-01   1.9478179e-01   1.4550928e-01   sitstd
   1.7755868e-01   1.7635119e-01   1.4697588e-01   sitstd

My matlab code:

%WindowParams
Fq = 51.2;
%Sample Rate of Shimmer
windowLength = 2;
%Length for each window in seconds
startPos = 1;
startPos2 = 1;
startPos3 = 1;
%Starting Position for 1st win
endPos = startPos + (windowLength * floor(Fq));
endPos2 = startPos2 + (windowLength * floor(Fq));
endPos3= startPos3 + (windowLength * floor(Fq));
%End Position for 1st win
totalWindows = floor(length(martySit)/Fq/windowLength);
totalWindows2 = floor(length(martyJump)/Fq/windowLength);
totalWindows3 = floor(length(martyRun)/Fq/windowLength);
stats = zeros(windowLength,9);

for i = 1:totalWindows
    eprange = range(martySit(startPos:endPos,:));%calculate window range
    martySitfeatures (i,1:3) = eprange; %X, Y & Z axis values for each stat
    %Next window position
    startPos = endPos+1;
    endPos= startPos + (windowLength * floor(Fq));
    martySitfeatures (:,4) = 10;
end
for i = 1:totalWindows2
    eprange = range(martyJump(startPos2:endPos2,:));
    %calculate window range
    %X, Y & Z axis values for each stat
    martyJumpfeatures (i,1:3) = eprange;
    %Next window position
    startPos2 = endPos2+1;
    endPos2 = startPos2+ (windowLength * floor(Fq));
    martyJumpfeatures (:,4) = 11;
end

for i = 1:totalWindows3
    eprange = range(martyRun(startPos3:endPos3,:));
    %calculate window range
    %X, Y & Z axis values for each stat
    martyRunfeatures (i,1:3) = eprange;
    %Next window position
    startPos3 = endPos3+1;
    endPos3 = startPos3+ (windowLength * floor(Fq));
    martyRunfeatures (:,4) = 12;
end

Solution

  • You can either import csv files or arff files. The default format to import data in Weka being the arff format.

    The details for the arff format is described here.

    You can basically just add the following header:

    @RELATION movement
    
    @ATTRIBUTE x  NUMERIC
    @ATTRIBUTE y  NUMERIC
    @ATTRIBUTE z  NUMERIC
    @ATTRIBUTE class        {sit,run,jump}
    
    @data
    
    [x array] [y array] [z array] [class array]
    

    If you want to add the mean information as an attribute:

    @RELATION movement
    
    @ATTRIBUTE mean_x  NUMERIC
    @ATTRIBUTE mean_y  NUMERIC
    @ATTRIBUTE mean_z  NUMERIC
    @ATTRIBUTE x  NUMERIC
    @ATTRIBUTE y  NUMERIC
    @ATTRIBUTE z  NUMERIC
    @ATTRIBUTE class        {sit,run,jump}
    
    @data
    
    [mean_x array] [mean_y array] [mean_z array] [x array] [y array] [z array] [class array]
    

    Then just save it as a txt file with the .arff extension.