Concepts / Deploying Machine Learning Models to Production

Deploying Machine Learning Models to Production

Analytics teams often use Python and R because they allow quick testing of ideas, while production teams often use Go, Scala, C++, or Java to build robust, scalable systems.

  • Programming

A Churn Model That Works on a Laptop

Imagine a small analytics team that has spent a few weeks testing ideas for predicting customer churn. They have a model that performs well on historical data, and it was built quickly using Python and scikit-learn. The natural next question is how that work reaches the customers and systems that need it.

This article is about that question. The path from a promising analysis to a running service is not always straightforward. Python and scikit-learn are strong tools for many machine learning applications, and they allow very quick analysis and prototyping. They are also used in production systems in many organizations, including very large ones such as international banks and global social media companies. Even so, many companies have complex infrastructure, and it is not always easy to include Python inside those systems. The rest of this article explains why that difficulty exists and what a common response looks like.

What do you think happens?

The churn model works well in the analytics team's Python environment. Do you think the production system must run that same Python code in order to use the model?

  • Yes, the Python code must run unchanged in production

  • No, the solution can be rebuilt in the language the production system uses

Reveal answer
Answer:

No, the solution can be rebuilt in the language the production system uses

A relatively common response is to reimplement the solution inside the larger production framework using a high-performance language. The idea behind the solution is kept, but the code that carries it is written again in the language the production system already uses. The Python code is not moved across unchanged.

Why Two Kinds of Teams Choose Different Languages

In many companies, the work is divided between two kinds of teams. Data analytics teams work with languages such as Python and R, which make it easy to test ideas quickly. Production teams work with languages such as Go, Scala, C++, and Java, which they use to build robust and scalable systems. The division is not accidental. It reflects two different jobs.

The central point is that data analysis has different requirements from building live services, so using different languages for these tasks makes sense. An analyst cares most about how fast a question can be explored and changed. A production engineer cares most about whether a service stays reliable and can handle large volumes of work. The same model may be interesting to both, but they judge it by different standards.

QuestionAnalytics teamProduction team
Typical languagesPython and RGo, Scala, C++, or Java
Main priorityTesting ideas quicklyBuilding robust, scalable systems
Main question askedHow fast can we explore and change this?Does the service stay reliable under large volumes of work?
Judges the same model bySpeed of explorationReliability and scale

Two teams, two jobs, two sets of language priorities

Tracing a Solution Across the Handoff

Follow the solution from the analytics environment to the running service. The analytics team does the exploratory work in Python and arrives at a solution. That solution is not copied across unchanged. It is rebuilt in a high-performance language and placed inside the larger framework that the production team maintains. The handoff therefore happens at the level of the solution, not at the level of the original Python environment.

Exploratory analysisPython or RSolution foundidea and logicReimplementationhigh-performance languageLarger productionframeworkmaintained by productionteamRunning servicerobust and scalablearrives atrebuilt, not copiedplaced insideruns as
What are the stages a model passes through on its way from the analytics environment into the running production service?

Notice what the diagram does not show. The Python environment is not one of the stages that the production system depends on. The idea passes through the handoff, and the code that carries it is written again. This is the reason the source describes the handoff as happening at the level of the solution.

Worked example

Following the churn model through the handoff

A small analytics team has a churn model that performs well on historical data, built quickly in Python with scikit-learn. How does that model reach the systems that need it?

  1. 1
    Step 1: Exploration in the analytics environment

    The team tests ideas quickly in Python, which is why this environment suits the work. The model performs well on historical data.

  2. 2
    Step 2: Identify the solution to carry forward

    The thing to carry forward is the solution the team found, meaning the idea behind the model, not the Python environment it was built in.

  3. 3
    Step 3: Reimplement inside the production framework

    The solution is rebuilt in a high-performance language and placed inside the larger framework that the production team maintains. The code is written again in the language the production system already uses.

  4. 4
    Step 4: Serve the work from the production system

    The production system runs the reimplemented solution, where it can be reliable and handle large volumes of work.

Result

The model reaches the customers and systems that need it because its solution is rebuilt in the production language, not because the Python code is shipped as it was.

Reimplementing Versus Embedding

There is an obvious alternative to reimplementation. Instead of rebuilding the solution, you could embed the whole Python library, or the whole programming language, inside the production system. The production system would then call into Python while it runs. This keeps the original code, but it creates a different set of work.

Production systemcontains the embeddedPythonWhole Python libraryor languageruns inside productionData formatconversionto and from the two sidesProduction systemcontains the reimplementedsolutionReimplementedsolutionhigh-performance languageNo Python inproductionthe idea is kept, the codeis rewrittenembedsrequirescontainsreplaces
Where does the Python code actually run in each approach, and what does the production system contain in each case?

The source explains why reimplementation is often preferred. It can be easier than embedding a whole library or programming language, and it avoids the work of converting data to and from the different formats that the two sides would otherwise have to share. In the embedding approach, the production system must contain a Python component and handle the boundary between the two languages. In the reimplementation approach, the production system contains only code in its own language.

ConsiderationReimplement in a high-performance languageEmbed the whole library or language
What the production system containsCode written in its own languageA Python library or language running inside it
Data format handlingAvoids converting data between formatsRequires converting data to and from the two formats
EffortOften easier, according to the sourceCan be harder, given the integration work
What is preservedThe idea behind the solutionThe original code itself
CaveatNot a universal ruleNot a universal rule

Trade-offs between the two approaches, as the source describes them

Mistakes When Reading the Handoff

  • Common mistake

    Assuming the Python code must be deployed unchanged for the model to work in production

    Why it fails

    The source describes a common response in which the solution is reimplemented in the production language. The idea is kept, but the code is written again.

    Do this instead

    Think of the handoff as happening at the level of the solution, and ask what idea needs to be carried across.

  • Common mistake

    Treating the choice of language as a judgment of which team is better

    Why it fails

    Data analysis has different requirements from building live services. Analysts optimize for how fast a question can be explored, and production engineers optimize for reliability and scale.

    Do this instead

    Describe each language by the priority it serves rather than by how good the team is.

  • Common mistake

    Assuming reimplementation is always the right choice

    Why it fails

    The source states that reimplementation is a relatively common solution and that it is not a universal rule.

    Do this instead

    Compare reimplementing with embedding for the specific infrastructure involved, and identify the trade-offs of each before deciding.

  • Common mistake

    Forgetting that embedding requires handling data format conversion between the two sides

Practice: Choosing an Approach

MEDIUM

A small analytics team has built a promising model in Python. The production team runs a large system written in Java, with complex infrastructure that makes it hard to include Python. Describe the two possible ways to bring the model into production, trace what each one puts inside the production system, and state one trade-off of each.

Hints
  • Start by naming the two approaches: reimplementing the solution, and embedding the whole Python library or language.
  • For each approach, ask what the production system contains afterward, using the comparison diagram as a guide.
  • Think about data format conversion when you consider the embedding approach.

Key Takeaways

  • Analytics teams often use Python and R to test ideas quickly, while production teams often use Go, Scala, C++, or Java to build robust, scalable systems.
  • Data analysis has different requirements from building live services, so different languages for these tasks makes sense.
  • A common response is to reimplement the analytics team's solution inside the larger production framework using a high-performance language, carrying the idea across while rewriting the code.
  • Reimplementation can be easier than embedding a whole library or language and converting data between formats, but it is not a universal rule.

More concepts starting with D