Skip to content
Related Articles

Related Articles

Tensorflow.js tf.data.csv() Function

Improve Article
Save Article
  • Last Updated : 10 Feb, 2022
Improve Article
Save Article

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.

The tf.data.csv() function is used to create a CSV-Dataset by reading and decoding CSV file(s) from provided URL or local path.

Syntax: 

tf.data.csv(source, csvConfig);

Parameters: This method accepts the following two parameters.

  • source: It is a URL or local path to get a CSV file.
  • csv-Config: It is a CSV-Config object that contains configurations of reading and decoding from CSV file(s) and it is an optional argument that has the following properties.
    • hasHeader: It is used to indicate whether the first row of provided CSV file is a header line with column names and should not include in data.
    • columnNames: It is used to denote a list of strings that corresponds to the CSV column name.
    • columnConfigs: A dict whose key is column names, values is an object stating if this column is required, column’s data type default value, and if this column is the label. It has the following keys:
      • required: It is used to make value in this column required if this is set to true.
      • dtype: It is used to denote the data type of this column.
      • Default: It is used to denote the default value of this column.
      • IsLabel: It is used to indicate whether this column is label instead of features or not.
      • configuredColumnsOnly: Only columns provided in columnconfigs will be parsed and provided during iteration if this is set to true.
      • delimiter: It is used to denote the string used to parse each line of the input file.
      • delimWhitespace: It true, delimiter field should be null, Parsing delimiter is whitespace and treat continuous multiple whitespace as one delimiter.

Return value: It returns tf.data.CSVDataset object.

Example 1: In this example, we will create Dataset by reading and decoding the CSV files by providing URL and predicate single column from the data set.

Javascript




// Requiring module
const tf = require("@tensorflow/tfjs")
 
// Sample CSV data link
const csvUrl = `https://storage.googleapis.com/tfjs-examples/
multivariate-linear-regression/data/boston-housing-train.csv`;
 
async function predicateSingleColumn() {
    // We want to predict single column "indus".
    const list = ['crim', 'zn', 'indus',
        'chas', 'nox', 'rm',
        'age', 'dis', 'rad',
        'tax', 'ptratio',
        'lstat', 'medv'];
    const csvDataset = tf.data.csv(
        csvUrl, {
        hasHeader: true,
        columnNames: list,
        columnConfigs: {
            indus: {
                isLabel: true
            }
        },
        configuredColumnsOnly: true,
        delimWhitspace: true
    });
    console.log(csvDataset)
}
 
// Function call
predicateSingleColumn();


 
 

Output: 

 

CSVDataset {
  size: null,
  input: URLDataSource {
    url: 'https://storage.googleapis.com/tfjs-examples/\n' +
      'multivariate-linear-regression/data/boston-housing-train.csv',
    fileOptions: {}
  },
  hasHeader: true,
  fullColumnNames: [
    'crim',    'zn',
    'indus',   'chas',
    'nox',     'rm',
    'age',     'dis',
    'rad',     'tax',
    'ptratio', 'lstat',
    'medv'
  ],
  columnNamesValidated: false,
  columnConfigs: { indus: { isLabel: true } },
  configuredColumnsOnly: undefined,
  delimiter: ',',
  delimWhitespace: false,
  base: TextLineDataset {
    size: null,
    input: URLDataSource {
      url: 'https://storage.googleapis.com/tfjs-examples/\n' +
        'multivariate-linear-regression/data/boston-housing-train.csv',
      fileOptions: {}
    }
  }
}

Example 2: In this example, we will create Dataset by reading and decoding the CSV files by providing URL and predicate multiple columns from the data set.

 

Javascript




// Requiring module
const tf = require("@tensorflow/tfjs")
 
// Sample CSV data link
const csvUrl = `https://storage.googleapis.com/tfjs-examples/
multivariate-linear-regression/data/boston-housing-train.csv`;
 
async function predicateMultipleColumns() {
    // We want to predict the multiple column.
    const list = ['crim', 'zn', 'indus',
        'chas', 'nox', 'rm',
        'age', 'dis', 'rad',
        'tax', 'ptratio',
        'lstat', 'medv'];
    const csvDataset = tf.data.csv(
        csvUrl, {
        hasHeader: true,
        columnNames: list,
        columnConfigs: {
            indus: {
                isLabel: true
            },
            rad: {
                isLabel: true
            },
            ram: {
                isLabel: true
            }
        },
        configuredColumnsOnly: true,
        delimWhitspace: true
    });
    console.log(csvDataset)
}
 
// Function call
predicateMultipleColumns();


 
 

Output: 

 

CSVDataset {
  size: null,
  input: URLDataSource {
    url: 'https://storage.googleapis.com/tfjs-examples/\n' +
      'multivariate-linear-regression/data/boston-housing-train.csv',
    fileOptions: {}
  },
  hasHeader: true,
  fullColumnNames: [
    'crim',    'zn',
    'indus',   'chas',
    'nox',     'rm',
    'age',     'dis',
    'rad',     'tax',
    'ptratio', 'lstat',
    'medv'
  ],
  columnNamesValidated: false,
  columnConfigs: {
    indus: { isLabel: true },
    rad: { isLabel: true },
    ram: { isLabel: true }
  },
  configuredColumnsOnly: undefined,
  delimiter: ',',
  delimWhitespace: false,
  base: TextLineDataset {
    size: null,
    input: URLDataSource {
      url: 'https://storage.googleapis.com/tfjs-examples/\n' +
        'multivariate-linear-regression/data/boston-housing-train.csv',
      fileOptions: {}
    }
  }
}

 

Reference: https://js.tensorflow.org/api/latest/#data.csv

 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!