Thursday, October 18, 2018

TDS API




Here is the API (Application Programming Interface) of the TDS (Tiny DataSet helper class) for the Machine Learning examples


This is a collection of methods allowing the creation , exploration and manipulation of a dataset.

1. Creation of a TinyDataSet (TDS)

There are three different methods to create a TinyDataSet.

+++ CSV Data +++ +++ End of CSV Data +++

1.1. Static method TDS.fromCSV(string)
This static method allows the creation of a TinyDataSet object from a String containing CSV data.

let csv = `
ID,filename,width,height,type
1,blobs.gif,256,254,8-bit
2,boats.gif,720,576,8-bit
3,clown.jpg,320,200,RGB
4,gel.gif,276,476,8-bit
5,LineGraph.jpg,516,356,8-bit
`;

let dataset = TDS.fromCSV(csv);

In the example above, a TinyDataSet object is generated from the String csv and is stored in the variable dataset.
Note: In JavaScript, a multi-line String is delimited by `back quotes`.

The resulting TinyDataSet is an array of objects corresponding to the rows (or vectors.

[
  {"ID":1,"filename":"blobs.gif","width":256,"height":254,"type":"8-bit"},
  {"ID":2,"filename":"boats.gif","width":720,"height":576,"type":"8-bit"},
  {"ID":3,"filename":"clown.jpg","width":320,"height":200,"type":"RGB"},
  {"ID":4,"filename":"gel.gif","width":276,"height":476,"type":"8-bit"},
  {"ID":5,"filename":"LineGraph.jpg","width":516,"height":356,"type":"8-bit"}
]
1.2. Static method TDS.fromTableIJ(ResultsTable)
If the measurements (features) are displayed in a Results window, you can build a TinyDataSet object with the static method fromTableIJ(..).

let table = ResultsTable.getResultsTable();
let dataset = TDS.fromTableIJ(table);
1.3. Static method fromFile(filename)
TODO

2. Get Properties

2.1. length
Get the number of vectors (or rows) in the TinyDataSet. For example, ...

let len = dataset.length;
console.log(len); // Display 5.

2.2. Methods vector(index) or row(index)
To get a row, use one of the methods vector(..) or row(..) like this...

// Get the 3rd row in dataset
let row = dataset.row(3);  
// row contains {"ID":4,"filename":"gel.gif","width":276,"height":476,"type":"8-bit"}

If you want to read the feature width in the 2nd row, use the following code...

// Get the 2nd row in dataset and read the feature width.
let w = dataset.row(2).width; // 320

2.3. Methods vectors() or rows()
To get the rows as an array of objects. It is useful if you want to make some computation of the values.

let array = dataset.rows();

3. Extraction, Cleaning TinyDataSet

3.1. Method columns(array)
To extract column(s), use the method columns(..) and put as argument an array containing the names of the desired features/headings.

let cols = dataset.columns(['filename','width','height'])
console.log(cols.toString() );

The result is an object where the rows correspond to the previous columns (transpose). The values are stored as array.

[
  filename: ["blobs.gif","boats.gif","clown.jpg","gel.gif","LineGraph.jpg"]
  width: [256,720,320,276,516]
  height: [254,576,200,476,356]
]

If you want to only read the values of column width, just type...

let colWidth = cols.row('width');
// [256,720,320,276,516]

3.2. Methods filter(func) and TDS.byFeatures(array)
The method filter(func) is used to only keep some of the features of a TinyDataSet object. To work properly, the TDS must be arranged in rows (and not columns).
Most of the time, the method filter(func) is used in conjunction with the static method TDS.byFeatures([..]).

4. Misc.

Here are some useful method(s) of TinyDataSet.
4.1. Method toString()
Allows the conversion of the contents of the TinyDataSet as a String. It is useful to display the contents of the object TinyDataSet like this...

let dataset = TDS.fromFile('./myData.csv');
console.log(dataset.toString()); 

<<  Previous: ML TOC Next: TinyDataSet  >>


4. Other crazybiocomputing posts

Further readings are available in ...

No comments:

Post a Comment