Data Science from Scratch 2nd Edition

Author: Joel Grus
File Type: pdf
Size: 11.5 MB
Language: English
Pages: 403

Data Science from Scratch 2nd Edition with Python: First Principles, Practical Workflows, and Real-World Applications

Introduction

Data science has become one of the most valuable technical disciplines for engineers, researchers, analysts, and technology professionals. At its core, data science is not simply about writing Python code or training machine-learning models. It is about transforming raw information into useful evidence that supports better decisions.

The phrase “Data Science from Scratch” represents an especially important learning philosophy: understand the fundamental ideas before depending heavily on automated libraries. Python makes this approach accessible because it allows beginners to work directly with data while giving advanced professionals enough flexibility to build sophisticated analytical systems.

A first-principles approach encourages you to understand what happens behind familiar tools. Instead of treating a machine-learning function as a black box, you learn how data is represented, cleaned, transformed, analyzed, visualized, and ultimately converted into predictions or decisions.

Image

Image

For engineering students, this approach is particularly valuable. Engineers frequently encounter measurements, simulations, sensor readings, experiments, production records, financial information, and operational data. The ability to analyze such information can turn an ordinary engineering workflow into a data-driven system.

Throughout this article, we will explore the foundations of data science with Python, including theory, definitions, workflows, comparisons, examples, applications, challenges, and practical advice.


Background Theory

From Raw Data to Useful Knowledge

Data science sits at the intersection of several disciplines:

  • Statistics 📊
  • Mathematics
  • Computer programming 💻
  • Domain knowledge
  • Data visualization
  • Machine learning 🤖
  • Database technology
  • Scientific reasoning

Raw data rarely arrives in a form that can immediately answer an engineering question. It may contain missing values, inconsistent formats, duplicated records, measurement errors, or irrelevant information.

A data scientist therefore works through a sequence of transformations.

Raw data → Clean data → Structured information → Analysis → Insight → Decision

This sequence is fundamental to understanding data science.

Why First Principles Matter

Modern Python ecosystems provide powerful libraries for almost every data-related task. However, using libraries without understanding the underlying concepts can create problems.

For example, a professional should understand:

  • What a dataset represents.
  • Why missing values matter.
  • How variables differ from observations.
  • Why correlation does not automatically imply causation.
  • How sampling affects conclusions.
  • Why model evaluation is necessary.
  • How data leakage can produce misleading results.

Libraries can automate calculations, but they cannot replace reasoning.

Python as a Data Science Foundation

Python is particularly suitable because it supports both simple experimentation and production-scale systems.

Common tools include:

ToolTypical Purpose
PythonProgramming and analysis
NumPyNumerical computing
pandasTabular data manipulation
MatplotlibVisualization
SciPyScientific computing
scikit-learnMachine learning
JupyterInteractive experimentation
SQLDatabase querying

A beginner can start with plain Python lists and dictionaries before gradually introducing specialized libraries.


Definition

What Is Data Science?

Data science is the systematic process of collecting, preparing, analyzing, modeling, visualizing, and interpreting data to discover useful information and support decisions.

The discipline combines computational techniques with statistical thinking and domain expertise.

What Does “From Scratch” Mean?

“From scratch” does not mean avoiding every existing library.

Instead, it means learning the principles behind the tools.

For example, before using an advanced visualization library, you should understand what a chart communicates. Before training a predictive model, you should understand the distinction between input variables, target variables, training data, and evaluation data.

This creates transferable knowledge.

Data Science vs Data Analysis

Data analysis generally focuses on understanding existing information.

Data science is broader and can include:

  • Data collection
  • Data engineering
  • Exploratory analysis
  • Statistical modeling
  • Machine learning
  • Prediction
  • Automation
  • Deployment

Therefore, data analysis can be considered an important component of the larger data-science workflow.


Step-by-Step Data Science Workflow

Step 1: Define the Problem

Every successful project begins with a question.

Instead of saying:

“I want to analyze this dataset.”

define a meaningful objective.

For example:

“Can historical machine readings help identify equipment that may require maintenance?”

The second question gives the project a purpose.

Step 2: Collect the Data

Data can originate from many sources:

  • CSV files
  • Databases
  • Sensors
  • APIs
  • Laboratory experiments
  • Web applications
  • Business systems
  • Industrial equipment

The quality of the final analysis depends heavily on the quality of the original data.

Step 3: Inspect the Dataset

Before performing sophisticated analysis, inspect the structure.

A Python workflow might begin conceptually with:

data.head()
data.info()
data.describe()

These operations help identify columns, data types, missing values, and basic distributions.

Step 4: Clean the Data

Data cleaning may involve:

  • Removing duplicates
  • Correcting data types
  • Handling missing observations
  • Standardizing labels
  • Detecting suspicious values
  • Converting dates
  • Removing irrelevant records

Data cleaning is often one of the largest parts of a practical project.

Step 5: Explore the Data

Exploratory data analysis asks questions such as:

  • What patterns exist?
  • Which variables appear related?
  • Are there unusual observations?
  • Does the dataset contain different groups?
  • How does behavior change over time?

Image

Image

ImageImage

Image

Image

Visualization is extremely useful at this stage because humans can recognize many patterns more easily through graphics than through large tables.

Step 6: Build a Model

If prediction is required, a suitable model can be selected.

Depending on the problem, this might involve:

  • Linear models
  • Decision trees
  • Random forests
  • Nearest-neighbor methods
  • Clustering
  • Neural networks

The simplest suitable model should normally be considered before a highly complex one.

Step 7: Evaluate the Result

A model that performs well on the data it has already seen may still fail on new data.

Evaluation therefore requires appropriate testing strategies.

Important questions include:

  • Does the model generalize?
  • Is the test data independent?
  • Are the evaluation metrics appropriate?
  • Is the model biased toward a particular group?
  • Is there data leakage?

Step 8: Communicate the Findings

A technically accurate analysis has limited value if nobody understands the result.

Effective communication may involve:

📊 Charts
📈 Dashboards
📝 Reports
💻 Interactive applications
🎯 Recommendations

The final objective is not simply to produce a model. It is to create useful knowledge.


Comparison

First-Principles Learning vs Library-First Learning

AspectFirst-Principles ApproachLibrary-First Approach
UnderstandingDeepPotentially superficial
Speed initiallySlowerFaster
DebuggingUsually easierCan be difficult
FlexibilityHighDepends on library
Long-term skillsStrongVariable
Best forStudents and professionalsRapid prototyping

Python vs Traditional Spreadsheet Analysis

FeaturePythonSpreadsheet
AutomationExcellentModerate
Large datasetsStrongMore limited
ReproducibilityExcellentCan be difficult
Machine learningExtensiveLimited
VisualizationExtensiveAccessible
ProgrammingRequiredMinimal

Neither approach is universally superior. Spreadsheets can be excellent for quick business analysis, while Python becomes increasingly powerful as datasets and workflows become more complex.


Diagrams & Tables

The Data Science Pipeline

             ┌───────────────┐
             │  Data Sources │
             └───────┬───────┘
                     ↓
             ┌───────────────┐
             │ Data Cleaning │
             └───────┬───────┘
                     ↓
             ┌───────────────┐
             │ Exploration   │
             └───────┬───────┘
                     ↓
             ┌───────────────┐
             │   Modeling    │
             └───────┬───────┘
                     ↓
             ┌───────────────┐
             │  Evaluation   │
             └───────┬───────┘
                     ↓
             ┌───────────────┐
             │   Decision    │
             └───────────────┘

The process is rarely perfectly linear. Engineers frequently return to earlier stages after discovering problems with the data or model.

Typical Dataset Structure

ObservationTemperaturePressureVibrationStatus
Machine ANormalNormalLowHealthy
Machine BHighNormalMediumWarning
Machine CHighHighHighFailure Risk
Machine DNormalNormalLowHealthy

Here, each row represents an observation, while each column represents a variable.

Image

Image

ImageImage

Three Fundamental Questions

A useful data-science project should continuously ask:

QuestionPurpose
What happened?Descriptive analysis
Why might it have happened?Diagnostic analysis
What might happen next?Predictive analysis
What should we do?Prescriptive decision-making

This progression helps transform data into action.


Examples

Example 1: Predicting Equipment Maintenance

Imagine an industrial facility collecting vibration, temperature, and operating-hour data from pumps.

A first-principles data-science workflow could:

  1. Import historical records.
  2. Remove duplicate measurements.
  3. Identify missing sensor readings.
  4. Visualize vibration behavior.
  5. Compare healthy and problematic equipment.
  6. Build a classification model.
  7. Evaluate predictions using unseen records.
  8. Create a maintenance alert system.

The objective is not merely to predict failure. The objective is to help engineers schedule maintenance before expensive downtime occurs.

Example 2: Customer Behavior

An online engineering software company may analyze:

  • Login frequency
  • Product usage
  • Subscription type
  • Feature usage
  • Customer support interactions

The analysis can help identify customers who may need assistance or educational resources.

Example 3: Environmental Monitoring

A monitoring system may collect air-quality measurements from multiple locations.

Python can help engineers:

  • Detect abnormal readings.
  • Compare locations.
  • Identify seasonal patterns.
  • Produce visual reports.
  • Automate daily analysis.

Real-World Applications

Engineering

Data science is increasingly useful in:

🏗️ Structural engineering
⚙️ Mechanical engineering
🔌 Electrical engineering
🚗 Automotive engineering
✈️ Aerospace engineering
🏭 Industrial engineering
🌊 Environmental engineering

For example, structural monitoring systems can generate large streams of measurements from sensors. Data science can help identify unusual patterns that deserve engineering investigation.

Manufacturing

Manufacturers use data-driven systems for:

  • Predictive maintenance
  • Quality control
  • Production optimization
  • Fault detection
  • Energy monitoring
  • Supply-chain forecasting

Finance

Data science supports:

  • Risk analysis
  • Fraud detection
  • Customer segmentation
  • Forecasting
  • Portfolio analysis

Healthcare

Data-driven methods can assist with:

  • Operational planning
  • Medical research
  • Resource allocation
  • Patient-flow analysis
  • Image analysis

Because healthcare data is highly sensitive, privacy, security, validation, and responsible use are essential.

Energy

Energy companies can use data science for:

  • Demand forecasting
  • Equipment monitoring
  • Renewable-energy prediction
  • Grid optimization
  • Consumption analysis

Common Mistakes

Starting With a Model Instead of a Problem

A sophisticated algorithm cannot compensate for an unclear objective.

Solution: Define the business, engineering, or scientific question first.

Ignoring Data Quality

A model trained on unreliable measurements can produce unreliable conclusions.

Solution: Perform systematic data-quality checks before modeling.

Overusing Complex Algorithms

Complexity can make systems harder to understand, maintain, and explain.

Solution: Establish a simple baseline before introducing advanced methods.

Confusing Correlation With Causation

Two variables can move together without one causing the other.

For example, increased electricity consumption and industrial production might occur simultaneously because both are influenced by operational activity.

Ignoring Data Leakage

If information from the future accidentally enters the training process, model performance may appear excellent while failing in production.

Poor Visualization

A chart containing too many colors, categories, labels, or unnecessary elements can obscure the actual message.

Solution: Design visualizations around one clear question.


Challenges & Solutions

ChallengePractical Solution
Missing dataInvestigate why values are missing before choosing a treatment
Large datasetsUse efficient data structures and database queries
Messy formatsStandardize fields during preprocessing
Model overfittingUse validation and simpler models
Biased dataExamine sampling and representation
Difficult interpretationUse clear visualizations and explanations
ReproducibilityDocument code, datasets, and processing steps
Deployment problemsTest models under realistic production conditions

Handling Large Data

Python can process substantial datasets, but not every dataset should be loaded into memory simultaneously.

For larger systems, engineers may combine Python with:

  • SQL databases
  • Cloud platforms
  • Distributed processing
  • Data warehouses
  • Streaming systems

Making Analysis Reproducible

A professional project should make it possible for another person to understand how the result was produced.

A useful project structure might look like:

data-science-project/
│
├── data/
├── notebooks/
├── src/
├── tests/
├── reports/
├── requirements.txt
└── README.md

This simple organization can dramatically improve maintainability.


Case Study

Predictive Maintenance for an Industrial Pump

Consider an engineering company operating hundreds of pumps.

Historically, technicians inspect equipment according to fixed schedules. This approach can result in two problems:

  1. Healthy equipment may receive unnecessary maintenance.
  2. A component may fail before its scheduled inspection.

The company begins collecting operational data from sensors.

Data Collection

The system records:

  • Temperature
  • Vibration
  • Pressure
  • Operating hours
  • Flow conditions
  • Maintenance history

Data Preparation

Engineers discover that some sensors occasionally stop transmitting. They also identify duplicated records caused by communication retries.

The team cleans the data and creates a consistent historical dataset.

Exploration

Visualization reveals that some pumps develop unusual vibration patterns before maintenance events.

This observation does not automatically prove that vibration causes failure. However, it provides a useful hypothesis for further investigation.

Modeling

The engineering team develops a predictive model using historical observations.

Instead of asking whether the model is simply “accurate,” they examine whether its predictions are useful for maintenance planning.

Deployment

The final system assigns equipment to categories such as:

🟢 Normal
🟡 Monitor
🔴 Investigate

Engineers receive alerts when measurements show patterns associated with elevated risk.

Result

The value of the project is not the Python code itself.

The value comes from connecting:

Sensors → Data → Analysis → Prediction → Engineering action

This is the essence of applied data science.


Essential Tips

Learn Python Fundamentals First

Before moving deeply into machine learning, become comfortable with:

  • Variables
  • Functions
  • Loops
  • Conditional logic
  • Lists
  • Dictionaries
  • File handling
  • Exceptions
  • Modules

Understand Data Structures

A large part of data science involves selecting appropriate structures for representing information.

Learn how Python lists, tuples, dictionaries, sets, arrays, and tabular structures differ.

Learn SQL

Many professional datasets live inside databases rather than CSV files.

SQL is therefore an extremely valuable companion skill.

Master Data Cleaning

Do not underestimate preprocessing.

A professional data scientist often spends significant time understanding where data came from and whether it can be trusted.

Visualize Before Modeling

Exploratory visualization can reveal:

  • Outliers
  • Trends
  • Groups
  • Missing information
  • Suspicious relationships

Start Simple

A simple model that is understood and validated can be more valuable than an extremely sophisticated model that nobody can explain.

Think Like an Engineer

Always ask:

“What decision will this analysis improve?”

That question keeps the project focused on practical value.


FAQs

What is Data Science from Scratch?

It is an approach to learning data science by understanding fundamental programming, statistical, computational, and analytical concepts before relying heavily on automated tools.

Is Python difficult for beginners?

Python is generally approachable because its syntax is relatively readable. Beginners can start with basic programming concepts and gradually progress toward data analysis and machine learning.

Do I need advanced mathematics?

Basic data-science concepts can be learned without advanced mathematics. However, professionals working with statistical modeling, machine learning, optimization, or research benefit greatly from stronger mathematical knowledge.

Should I learn pandas before NumPy?

There is no universal requirement. Learning basic Python first is more important. After that, NumPy provides useful numerical foundations, while pandas is particularly convenient for tabular datasets.

Is data cleaning really necessary?

Yes. Incorrect, duplicated, inconsistent, or incomplete data can produce misleading analytical results regardless of how sophisticated the model is.

Is machine learning the same as data science?

No. Machine learning is one component of data science. Data science also includes data collection, cleaning, exploration, visualization, statistical reasoning, communication, and decision-making.

Can engineers benefit from learning data science?

Absolutely. Engineering systems increasingly generate measurements and operational data. Data-science skills can support predictive maintenance, quality control, simulation analysis, monitoring, optimization, and research.

What should I learn after Python fundamentals?

A practical progression is:

Python → NumPy → pandas → visualization → SQL → statistics → machine learning → deployment

The exact sequence can change depending on your career goals.


Conclusion

Data Science from Scratch with Python is fundamentally about learning how to think with data.

The most important skill is not memorizing Python commands. It is understanding the complete journey from a real-world question to a trustworthy answer.

A strong data scientist learns to:

🔍 Define meaningful questions
🧹 Clean unreliable information
📊 Explore patterns
🐍 Use Python effectively
🤖 Build appropriate models
🧪 Evaluate results honestly
📈 Communicate findings clearly
⚙️ Connect predictions with real-world decisions

For students, this first-principles approach builds a strong technical foundation. For professionals, it provides a framework for solving practical problems across engineering, finance, manufacturing, energy, technology, and scientific research.

The most effective path is therefore not “learn every data-science library.” It is:

Understand the problem → understand the data → understand the method → use the right tool → validate the result → communicate the insight.

That mindset transforms Python from a programming language into a powerful engineering instrument for turning data into knowledge and knowledge into action.

Unlock exclusive content
Enjoy all premium content by watching a short ad
Preparing ad...
BY ADX360