CSV Loader in Typescript

Enrico Zanardo
The Startup
Published in
2 min readMay 21, 2020

A npm package that allow to manage the content of the .csv files that are then used for machine learning purposes later on.

NOAA’s National Ocean Service

How to use it

Imagine having a .csv file called cars.csv for which further analysis must be performed on a machine learning application. 👨‍💻

Here the repository of the source code: https://github.com/onezerobinary/csv-loader

Sample File

This is a chunk of the sample csv file that will be used during this explanation.

Options ⚙️

Before to load the file it is important to decide which actions are needed to the data source before to be able to use it for further analysis.

Let’s talk about the meaning of each option:

- dataColumns: This is an array that contains the list of the columns that are taken into account for further analysis (like features columns);
- labelColumns: This is an array that contains the list of the columns that are taken into account as the target columns for further analysis;
- converters: This is a dictionary of values pairs where the key is a column name and the value is a function that will be activated for each value in the column represented by the key. More detail in the next paragraph.
- shuffle: is a boolean. If true the data inside the dataset will be shuffled;
- splitTest: Represent, in percentage, the subset of the dataset that will be used as test data. The possible values are in the range 0.0 (no test data) to 1.0 (all dataset is used as test data) with intervals of 0.1. For example 0.1, 0.2 .. and so on. The meaning of the values are the percentage that will be taken for example 0.1 = 10%.

Converters 🔬

Converters is a dictionary that could be used to provide some dedicated actions to the specific column data based on the name of the column that will be used as a key.

Example:

This specific converter, for example, is converting from Miles Per Gallon (mpg) to Kilometres Per Litre.

This is the function used from this converter when the data of the column mpg is parsed from the csv-loader.

Full Example

Here is reported the full example written in the index.ts file that show a possible use of this library. 🚀

The cars.csv file is stored into a folder called /csv/. Once that the CSVManager is created and the path of the csv file is defined as well as its options, the result is stored into the dataSet constant.

The dataSet has four elements representing the test labels, the test features, the labels and the features of the initially given cars.csv file.

Now this elements can be used for further machine learning analysis. 🎉

Happy hacking 🙌

--

--