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 columnNames() method is under the tf.data.CSVDatset class. It returns all the column names of the CSV file.
Syntax:
tf.data.csv(source).columnNames()
Parameters: This method has a single parameter as mentioned 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 list of strings i.e the column names of the CSV file.
The examples below will demonstrate this method.
Example 1: In this example, we will find all the column names of the following values.
Serial Number,Name,Order Id,Amount,Payment Mode 12141661A321,Geek 1,YP12164AZEA,1200,DEBIT CARD 12141661A322,Geek 2,ZSER15563VS,23324,COD 12141661A323,Geek 3,DSR5442HG6,322,CREDIT CARD 12141661A324,Geek 4,GF3467FSGTW,1890,PAYPAL 12141661A325,Geek 5,RSTYCBBJSST,141,COD
We will now retrieve the column names using the columnNames() method.
Javascript
// Importing the tensorflow.Js library import * as tf from "@tensorflow/tfjs" // This is the source of the csv file // It can be a link or the location of the File const source = 'sampleData.csv' async function run() { // Creating the Dataset from the source const csvDataset = tf.data.csv(Source); // Retrieving the column names from the // dataset using columnNames() method const columnNames = await csvDataset.columnNames(); // Printing the columnNames console.log(columnNames) } await run(); |
Output:
Serial Number, Name, Order Id, Amount, Payment Mode
Example 2: In this example, we will find all the column names of the following values.
S No,Name,Height(cm),Weight(Kgs) 2,Geek 2,167,58 3,Geek 3,179,46 1,Geek 1,164,51 4,Geek 4,166,53 5,Geek 5,138,63
We will now retrieve the column names using the columnNames() method.
Javascript
// Importing the tensorflow.Js library import * as tf from "@tensorflow/tfjs" const source2 = 'sampleData2.csv' async function run() { // Create the Dataset from the source const csvDataset = tf.data.csv(source2); // Retrieve the column names from the // dataset using method const ColumnNames = (await csvDataset.columnNames()); // Printing the column names console.log(ColumnNames) } await run(); |
Output:
S No, Name, Height(cm), Weight(Kgs)
Reference: https://js.tensorflow.org/api/latest/#tf.data.CSVDataset.columnNames