Linear regression using Python Pandas and Numpy
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Linear regression models a numeric target as a weighted sum of one or more input features. With Pandas and NumPy, you can prepare tabular data, build the design matrix, solve for coefficients, and generate predictions without pulling in a full machine learning framework.
That makes it a good exercise for understanding what regression is actually doing under the hood. Pandas handles the table-shaped data, and NumPy handles the matrix algebra.
Build a Small Regression Dataset
Start with a DataFrame so the feature columns are named and easy to inspect.
We want to predict score from the two feature columns.
Create the Design Matrix With NumPy
Linear regression needs an intercept term. The usual trick is to add a column of ones to the feature matrix.
Now the first column represents the intercept coefficient, and the remaining columns represent feature coefficients.
Fit the Regression Model
A common way to solve ordinary least squares is np.linalg.lstsq. It is more stable than manually inverting matrices.
If the output were something like:
- Intercept around
47 - Weight for
hours_studiedaround4 - Weight for
practice_testsaround2
then the model would be estimating score roughly as base score plus four points per study hour plus two points per practice test.
Make Predictions
Once the coefficients are known, predictions are just matrix multiplication.
To predict on new data, build new rows with the same feature order and the same intercept column.
Evaluate the Fit
Even a simple regression should be evaluated. One quick metric is the coefficient of determination, or R^2.
An R^2 value closer to 1 means the model explains more of the variation in the target. That does not prove the model is good in every practical sense, but it is a useful first check.
Why Use Pandas and NumPy Together
Pandas is convenient for cleaning and selecting columns. NumPy is better for the matrix math itself. That pairing keeps the workflow lightweight and transparent:
- Use Pandas to inspect, filter, and transform the table.
- Convert to NumPy arrays for fitting and prediction.
- Write results back to a DataFrame for analysis.
This is often enough for small projects, teaching, prototyping, or sanity-checking a model before moving to scikit-learn.
Common Pitfalls
One common mistake is forgetting the intercept column. If you omit it, the model is forced through zero, which can distort the coefficients badly.
Another issue is non-numeric data. Pandas columns that look numeric may still contain strings or missing values. Convert types and handle nulls before calling NumPy.
Multicollinearity is another practical problem. If two features contain nearly the same information, the fitted weights may become unstable even if predictions look reasonable.
Finally, do not judge a regression only on the training data. A model can appear accurate on a tiny hand-built dataset and still generalize poorly on new observations.
Summary
- Use Pandas for data preparation and NumPy for the regression math.
- Build a design matrix with an intercept column before fitting.
- Prefer
np.linalg.lstsqover manual matrix inversion. - Predict with matrix multiplication once the coefficients are known.
- Check fit quality and data assumptions before trusting the model.
Related reading
- Linear Regression with positive coefficients in Python
- Linear vs nonlinear neural network?
- LinearRegressionWithSGD returns NaN
- Liquid State Machine How it works and how to use it?
- List of all classification algorithms
- List of all classification algorithms
- List attributes of an object
- List comprehension Returning two or more items for each item
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.