DataSet Object Examples

From Eigenvector Research Documentation Wiki
Revision as of 09:05, 13 July 2009 by imported>Scott
Jump to navigation Jump to search

DataSet Object Tour

Perhaps the best way to understand how DSOs work is to examine a couple of them. Several data sets are included with PLS_Toolbox, and all of them are in the form of DSOs. We will start with the smallest one, the Wine data set. Clear the MATLAB workspace (save anything important first!) and at the command line type:

>> load wine
>> whos
  Name       Size                   Bytes  Class

  wine      10x5                     6050  dataset object

Grand total is 920 elements using 6050 bytes

We have now loaded the Wine data set. When the whos command is used, we see that there is a single variable in the workspace, wine, of Class dataset object, with a data field that is 10 by 5. We can look at the contents of wine by typing:

>> wine
 
wine = 
       name: Wine
       type: data
     author: B.M. Wise
       date: 14-May-2001 13:47:54
    moddate: 06-Jun-2001 10:27:24
       data: 10x5 [double]
      label: {2x1} [array (char)]
               Mode 1  [Country: 10x6] 
               Mode 2  [Variable: 5x6] 
  axisscale: {2x1} [vector (real)]
               Mode 1  [: ] 
               Mode 2  [: ] 
      title: {2x1} [vector (char)]
               Mode 1  [: 'Country']  
               Mode 2  [: 'Variable'] 
class: {2x1} [vector (integer)]
               Mode 1  [: ] 
               Mode 2  [: ] 
    include: {2x1} [vector (integer)]
               Mode 1  [: 1x10] 
               Mode 2  [: 1x5]  
description: Wine, beer, and liquor consumption (gal/yr), life
             life expectancy (years), and heart disease rate  
             (cases/100,000/yr) for 10 countries.            
    history: {1x1 cell} [array (char)]
   userdata: 

From this we see that the name of the data is Wine and that the type is “data.” Other types are also possible, such as “image” and “batch.” The author is listed, followed by the creation date and last-modified date. The next field, data, contains the actual data table. These data, or the data from any DSO field, can be extracted from the DSO just as they would be from a conventional structure array (type help struct or refer to the Examining a Structure Array section of Chapter 2 for help) using DSOname.fieldname syntax. For instance:

>> wine.data

ans =

    2.5000   63.5000   40.1000   78.0000   61.1000
    0.9000   58.0000   25.1000   78.0000   94.1000
    1.7000   46.0000   65.0000   78.0000  106.4000
    1.2000   15.7000  102.1000   78.0000  173.0000
    1.5000   12.2000  100.0000   77.0000  199.7000
    2.0000    8.9000   87.8000   76.0000  176.0000
    3.8000    2.7000   17.1000   69.0000  373.6000
    1.0000    1.7000  140.0000   73.0000  283.7000
    2.1000    1.0000   55.0000   79.0000   34.7000
    0.8000    0.2000   50.4000   73.0000   36.4000

The labels can be extracted in a similar manner:

>> wine.label

ans =

  [10x6] char
  [ 5x6] char

Note that ans is a cell array, i.e., the labels for each mode of the array are stored in a cell that is indexed to that mode. Thus, the labels for mode 1, the data set rows, can be extracted with:

>> wine.label{1}

ans =

France
Italy 
Switz 
Austra
Brit  
U.S.A.
Russia
Czech 
Japan 
Mexico

Note that curly brackets, {}, are used to index into cell arrays (type help cell for more information on cell arrays). In a similar way the labels for mode 2, the data set columns, can be extracted by executing:

>> wine.label{2}

ans =

Liquor
Wine  
Beer  
LifeEx
HeartD

Other fields in the DSO include .axisscale (e.g., time or wavelength scale), .title (titles for the axes), and .class (e.g., class variables for samples). Note that a typical data set will not have all of the available fields filled. The Wine data set does not have axis scales, for instance, nor class variables.

DSOs also allow for multiple sets of many of these fields; for instance, you may store more than one set of labels for a particular mode. Most GUI tools including Analysis, PlotGUI and the DataSet Editor support multiple sets but there are some rare situations where their use has not yet been fully implemented. GUIs allow limited support of multiple sets; axis scales and titles are not yet completely supported in the main Analysis GUI.

The user is encouraged to explore more of the DSOs included with PLS_Toolbox. For an example with axis scales, please see spec1 in the nir_data.mat file. For an example with class variables, please see arch in the arch.mat file.


Creating A DataSet Object

The following shows an example using the 'wine' data set in the PLS_Toolbox. Other examples can be found in the datasetdemo.m script.

The first step in the example is to load the 'wine' data set and examine the variables. The MATLAB commands are:

»load wine_raw
»whos
  Name  Size Bytes Class
  dat   10x5  400  double array
  names 10x6  120  char array
  vars   5x6   60  char array

The variable 'dat' contains the data array corresponding to the 5 variables wine, beer, and liquor consumption, life expectancy, and heart disease for 10 samples (countries).

The country names are contained in the variable 'names' and the variable names are contained in 'vars'. The next step creates a DataSet object, gives it a name, authorship, and description.

»wined = dataset(dat);
»wined.name = 'Wine';
»wined.author = 'A.E. Newman';
»wined.description= ...
{'Wine, beer, and liquor consumption (gal/yr)',...
'life expectancy (years), and heart disease rate', ...
'(cases/100,/yr) for 10 countries.'};
»wined.label{1} = names;
»wined.label{2} = vars;

Additional assignments can also be made. Here the label for the first mode (rows) is shown explicitly next to the data array (like sample labels). Also, titles, axis, and titles are assigned.

»wined.labelname{1} = 'Countries';
»wined.label{1} = ...
{'France' ...
'Italy', ...
'Switz', ...
'Austra', ...
...
'Mexico'};
»wined.title{1} = 'Country';
»wined.class{1} = [1 1 1 2 3];
»wined.classname{1} = 'Continent';
»wined.axisscale{1} = 1:5;
»wined.axisscalename{1} = 'Country Number';

Additional assignments can also be made for mode 2. Here the label for the second mode (columns) is shown explicitly above the data array (like column headings). Also, titles, axis, and titles are assigned.

»wined.labelname{2} = 'Variables';
»wined.label{2} = ...
{'Liquor','Wine','Beer','LifeExp','HeartD'};

If the data matrix is N-way the assignment process can be extended to Mode 3, Mode 4, ... Mode N. It can also be extended to using multiple sets of labels and axis scales e.g.

»wined.labelname{2,2} = 'Alcohol Content and Quality';
»wined.label{2,2} = {'high','medium','low','good','bad'};

An individual label can be replaced by further indexing into a given label set using curly braces followed by the string replacement:

»wined.label{2,2}{4} = 'excellent';

Sub-portions of the DataSet can be retrieved by indexing into the main DataSet object. For example, here the first three columns ('Liquor', 'Wine', and 'Beer') are extracted into a new DataSet named "alcohol":

»alcohol = wined(:,1:3);

Similarly, a shortcut to extract a single variable or sample out of the DataSet is to index into the main DataSet object using the label for the requested item. For example, to extract a DataSet containing only the Liquor values, you could use:

»alcohol = wined.liquor;

Note that the upper-case characters in the label do not matter. If there are any spaces or mathematical symbols in the label, you must enclose the label in parenthesis and quotes:

»alcohol = wined.('liquor');

Additionally, any field in the DataSet can also be indexed into directly. Here the second country name is pulled out of the labels by extracting the entire second row of the mode 1 labels:

»country2 = wined.label{1}(2,:);

Using the Class Lookup Table

Classes in a DataSet are stored as numeric values (in the .class field). Each numeric value and be associated with a text value using the classlookup field. This field contains a simple nx2 table with numeric values in the first column and string values in the second. For example, the arch dataset has a classlookkup table for elements:

>> load arch
>> arch.classlookup{1,1}
ans = 
    [0]    'Class 0'
    [1]    'K'      
    [2]    'BL'     
    [3]    'SH'     
    [4]    'AN'   


There are two basic ways to alter a classlookup table, by extracting the table or directly accessing the table.

Extracting the Table

Extracting the table can be useful if you need to perform several changes:

a  = arch.classlookup{1};
a{3,2} = 'YYY';
a{4,2} = 'ZZZ';
arch.classlookup{1} = a;
arch.classlookup{1}
ans =
   [0]    'Class 0'
   [1]    'K'
   [2]    'YYY'
   [3]    'ZZZ'
   [4]    'AN'


Direct Access

If you just need to change a single value (or a small number of values) you can directly access the lookup table using .assignstr and .assignval fields. For example, the change class 0 of arch from "Class 0" to "Unknown":

arch.classlookup{1}.assignstr = {0 'Unknown'}
>> arch.classlookup{1}

ans =

   [0]    'Unknown'
   [1]    'K'
   [2]    'BL'
   [3]    'SH'
   [4]    'AN'

Then, to change the numeric value of "Unknown" from 0 to 6:

>> arch.classlookup{1}
arch.classlookup{1}.assignval = {6 'Unknown'}
ans =

   [0]    'Class 0'
   [1]    'K'
   [2]    'BL'
   [3]    'SH'
   [4]    'AN'
   [6]    'Unknown'