DEV Community

Cover image for When the Algorithm Stays the Same but the Code Changes
Oluwafemi Paul Adeyemi
Oluwafemi Paul Adeyemi

Posted on

When the Algorithm Stays the Same but the Code Changes

A machine learning algorithm does not belong to a programming language.

Linear regression is still linear regression whether it is implemented in Python, R, JavaScript, or another language. Yet the code used to implement it can look remarkably different.

Why?

The answer lies in the distinction between an algorithm and its implementation. An algorithm describes the procedure or mathematical steps used to solve a problem, while an implementation is the way those steps are translated into instructions that a particular programming language and its libraries can execute. The algorithm therefore represents the underlying idea, whereas the implementation represents one way of putting that idea into practice.

The Algorithm

Consider a simple linear regression model:

y = β₀ + β₁x + ε

The objective is to estimate the parameters β₀ and β₁ so that the model can describe the relationship between the predictor and the response. That mathematical structure does not change because we change programming languages. What changes is how we express the algorithm in code.

The Implementation

In Python, using Scikit-learn:

from sklearn.linear_model import LinearRegression

model = LinearRegression()
model.fit(X, y)

predictions = model.predict(X)
Enter fullscreen mode Exit fullscreen mode

In R, the same idea can be expressed using the built-in "lm()" function:

model <- lm(y ~ x, data = data)

predictions <- predict(model)
Enter fullscreen mode Exit fullscreen mode

The syntax is clearly different.

Python uses an object and calls "fit()" and "predict()". R uses a formula interface and the "lm()" function. Yet both are implementing the same fundamental statistical idea: estimating a linear relationship between variables and using that relationship for prediction.

JavaScript can express the same concept through a machine learning library as well:

const model = new LinearRegression();

await model.fit(X, y);

const predictions = model.predict(X);
Enter fullscreen mode Exit fullscreen mode

Once again, the code changes, but the underlying algorithm does not.

What Actually Changes?

When an algorithm crosses programming-language boundaries, several things may change. First is syntax. Each language has its own grammar and conventions. Second is the API. One library may use "fit()" while another uses a different function or method. Third is the data structure. Python may use NumPy arrays, R may use vectors and data frames, while JavaScript may use arrays or typed arrays.

Fourth is the workflow. Libraries can make different design choices about how models are created, trained, evaluated, and used. These differences can make the same algorithm appear unfamiliar when encountered in another language.

What Does Not Change?

The more important question is what remains unchanged. The mathematical formulation remains the same. The statistical objective remains the same. The basic interpretation of the model remains the same.

Changing from Python to R does not transform linear regression into another statistical method. The programming language provides a different way of expressing and using the method. This distinction is important for anyone working across programming-language ecosystems.

If you learn only the API of a particular library, changing languages can feel like starting again. But if you understand the algorithm, its mathematics, its assumptions, and what its outputs mean, learning another implementation becomes much easier.

The code may change its language, syntax, and workflow. The algorithm, however, remains the same. That is the difference between learning a machine learning library and learning machine learning itself.

Top comments (0)