How to Load Machine Learning Data From Scratch In Python
Before you model, you load
Every project starts the same way: a CSV file and a set of assumptions about what is inside it. Pandas hides that step behind one function call, which is wonderful right up until the moment a column silently becomes a string of objects and your model refuses to train.
In this tutorial you will load a CSV with nothing but Python's standard library, convert columns to the right types, and end up with a clean list of lists of floats — the format every other tutorial in this series expects.
1. Read a CSV file
The csv module gives you a reader that yields one list of strings per row. Two things matter: skip blank rows, and decide what to do with a header.
2. Convert strings to floats
Everything a CSV reader hands you is a string. Numeric columns need converting, and this is exactly where hidden whitespace and empty cells announce themselves.
3. Convert class labels to integers
Classification algorithms want integers, not strings. Build the lookup explicitly so you can map predictions back to human-readable labels later — an anonymous 0/1/2 in a report helps nobody.
4. Load a real dataset over the network
Now put the three pieces together on the Pima Indians Diabetes dataset, fetched live in your browser.
Checklist before you move on
- Every column is the type you expect — print one row and read it.
- Row count matches the source. A silent off-by-one usually means a header you forgot to skip.
- Missing values have an explicit policy: drop, impute, or fail loudly.
- The class column is last, encoded as integers, with a lookup you kept.