Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Homework 4 – Titanic Statistics Analysis (Streaming)

Task Definition

The goal of Homework 4 is to analyze passenger data from the Titanic dataset (train.csv) while reading the file using streams instead of loading the entire file into memory.

Specifically, the task requires:

  1. Calculating total fares paid by all passengers.
  2. Calculating average fare for each passenger class (1st, 2nd, and 3rd).
  3. Calculating total number of survived and non-survived passengers.
  4. Calculating total number of survived men, women, and children (under 18 years old).

Important requirement:

  • The dataset must be read using streaming, not by loading the whole file at once.
  • Implementation can use Node.js streams, readline, or external npm packages.

📝 Description

This project implements a Titanic dataset analysis tool in Node.js that processes passenger data from a CSV file using stream-based file reading.

Instead of loading the entire dataset into memory, the application reads the file line-by-line using Node.js streaming utilities. This approach is more memory-efficient and scalable for large datasets.

The project contains:

  • CSV streaming utilities – read and parse CSV files using streams.
  • Titanic analysis class – performs statistical analysis on the dataset.
  • Main script (index.js) – loads the data and prints calculated statistics.

Project structure:

.
├── index.js                 # Application entry point
├── train.csv                # Titanic dataset
└── src
    ├── Titanic.js           # Dataset analysis class
    └── file_handling.js     # CSV streaming and parsing utilities

🎯 Purpose

The homework focuses on practicing:

  1. Node.js Streams – reading files efficiently using streams.
  2. Memory-efficient data processing – avoiding loading large files into memory.
  3. CSV parsing – safely splitting CSV rows including quoted values.
  4. Object-oriented design – encapsulating analysis logic inside a reusable class.
  5. Modern JavaScript features – ES modules, async/await, private class fields.

🔍 How It Works

1. Streaming the CSV File

The module file_handling.js uses:

fs.createReadStream();
readline.createInterface();

This allows the file to be processed line by line.

Example:

const stream = fs.createReadStream(filename);

const rl = readline.createInterface({
	input: stream,
	crlfDelay: Infinity,
});

Each line is processed asynchronously using:

for await (const line of rl)

2. Parsing CSV Rows

A regular expression is used to correctly split CSV values while respecting quoted commas:

/,(?=(?:(?:[^"]*"){2})*[^"]*$)/

Each row is converted into an object using the dataset headers.

Example row:

{
  PassengerId: "1",
  Survived: "0",
  Pclass: "3",
  Name: "Braund, Mr. Owen Harris",
  Sex: "male",
  Age: "22",
  Fare: "7.25"
}

3. Data Analysis

The Titanic class performs statistical calculations on the dataset.

Total Fares

getTotalFares();

Returns the sum of all passenger fares.


Average Fare by Class

getAvgFaresByClass();

Calculates average ticket price for each passenger class.

Example output:

{
  "1": 84.15,
  "2": 20.66,
  "3": 13.68
}

Total Survived Passengers

getTotalSurvived();

Counts rows where:

Survived === "1"

Survived by Gender

getTotalSurvivedByGender();

Groups survived passengers by gender.

Example:

{
  male: 109,
  female: 233
}

Survived Children

getTotalSurvivedChildren((ageLimit = 18));

Counts passengers younger than the given age limit who survived.


📜 Output Example

Run the program:

node index.js

Example output:

=== Titanic Dataset Stats ===
Total Fares: 28693.95
Average Fares by Class: { '1': 84.15, '2': 20.66, '3': 13.68 }
Total Survived: 342
Total Survived by Gender: { male: 109, female: 233 }
Total Survived Children (under 18): 61

📦 Usage

Run the script:

node index.js

The application will:

  1. Stream the CSV file
  2. Parse each line into objects
  3. Perform statistical analysis
  4. Print results to the console

✅ Dependencies

  • Node.js 18+

Built-in modules used:

  • fs
  • readline

No external npm packages are required.


📊 Project Status

Status: ✅ Completed

  • CSV streaming implemented using Node.js streams.
  • Dataset processed line-by-line for better memory efficiency.
  • Titanic analysis class provides multiple statistical methods.
  • Asynchronous iteration (for await...of) used for streaming processing.

📄 License

MIT License


🧮 Conclusion

This project demonstrates how to build a stream-based data processing tool in Node.js.

Key highlights:

  • Efficient streaming file processing
  • Reliable CSV parsing with quoted values
  • Clean object-oriented dataset analysis
  • Separation between data ingestion and analytics

Using streams makes the implementation scalable and memory-efficient, which is essential when working with large datasets.


Made with ❤️ and JavaScript by Sam Malikin 🎓

About

A Node.js streaming data analysis project that processes the Titanic CSV dataset line-by-line using streams and calculates statistics such as total fares, average fares by class, and survival metrics by gender and age.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages