Tensorflow.js tf.data.CSVDataset Class
Last Updated :
06 Aug, 2021
Tensorflow.js is an open-source library developed by Google for running machine learning models and deep learning neural networks in the browser or node environment.
Tensorflow.js has a rich set of libraries which have a lot of uses for creating, developing and deploying models, but the main required element to train or create a model is data. The data is usually huge to develop and train a model and is usually taken as CSV files. Therefore, TensorFlow Library provides a library called CSVDataset class to deal with CSV files. This tf.data.CSVDataset class extends the tf.data.Dataset class.
Syntax:
tf.data.csv( source )
Methods: The tf.data.CSVDataset class has a predefined method called columnNames() function that helps to get the column names of the CSV file. Whenever the data has columns in it, it is retrieved using columnNames() function or else an error is thrown when the file does not have column names. The values that can be parsed as numbers are emitted as type number and the other values are parsed as a string.
Syntax:
tf.data.csv( source ).columnNames()
Parameters: This method has a single parameter as mentioned above and described below.
- source: The source is the file where the CSV file is present. It can either be a link to the file or the file location in the system.
Return Value: It returns the column names of the CSV file as an array.
Example 1: Consider the following CSV file
S No., UserName, Address, Mobile number, Item Ordered, Payment Type
1, Bertram, 59 Oak Valley St., (886) 692-1076, hair clip, COD
2, Cecil, Toledo, OH 43612, (791) 560-1299, toy soldier, PAYPAL
3, Clarence, Port Saint Lucie, (791) 560-1299, book of jokes, CREDIT CARD
4, Clive, Warwick, RI 02886, (749) 409-1970, sharpie, CREDIT CARD
5, Maureen Massillon, OH 44646, (500) 539-2735, shoes, DEBIT CARD
The column names can be retrieved using the following code.
Javascript
import * as tf from "@tensorflow/tfjs"
const source = 'SampleData.csv'
async function run() {
const csvDataset = tf.data.csv(Source);
const ColumnNames = (await csvDataset.columnNames());
console.log(ColumnNames)
}
await run();
|
Output:
S No., UserName, Address, Mobile number, Item Ordered, Payment Type
Example 2: Consider the following CSV file
S No, Name, Marks(out of 100)
1, Geek 1, 31
2, Geek 2, 65
3, Geek 3, 97
4, Geek 4, 75
5, Geek 5, 58
The column names can be retrieved using the following code.
Javascript
import * as tf from "@tensorflow/tfjs"
const source = 'sample.csv'
async function run() {
const csvDataset = tf.data.csv(Source);
const ColumnNames = (await csvDataset.columnNames());
console.log(ColumnNames)
}
await run();
|
Output:
S No,Name,Marks(out of 100)
Reference: https://js.tensorflow.org/api/latest/#class:data.CSVDataset
Share your thoughts in the comments
Please Login to comment...