Stop Coding Machine Learning Algorithms From Scratch

You Don’t Have To Implement Algorithms
if you’re a beginner and just getting started.

Stop.

Are you implementing a machine learning algorithm at the moment?

Why?

Implementing algorithms from scratch is one of the biggest mistakes I see beginners make.

In this post you will discover:

  • The algorithm implementation trap that beginners fall into.
  • The very real difficulty of engineering world-class implementations of machine learning algorithms.
  • Why you should be using off-the-shelf implementations.

Let’s get started.

Don't Implement Machine Learning Algorithms

Don’t Implement Machine Learning Algorithms
Photo by kirandulo, some rights reserved.

Caught In The Implementation Trap

Here’s a snippet of an email I received:

… I am really struggling. Why do I have to implement algorithms from scratch?

It seems that a lot of developers get caught in this challenge.

They are told or imply that:

Algorithms must be implemented
before being used.

Or that:

You can only learn machine learning by
implementing algorithms.

Here are some similar questions I stumbled across:

  • Why is there a need to manually implement machine learning algorithms when there are many advanced APIs like tensorflow available? (on Quora)
  • Is there any value implementing machine learning algorithms by yourself or should you use libraries? (on Quora)
  • Is it useful to implement machine learning algorithms? (on Quora)
  • Which programming language should I use to implement Machine Learning algorithms? (on Quora)
  • Why do you and other people sometimes implement machine learning algorithms from scratch? (on GitHub)

You’re Probably Doing it Wrong

You don’t have to implement machine learning algorithms from scratch.

This is a part of the bottom-up approach traditionally used to teach machine learning.

  1. Learn Math.
  2. Learn Theory.
  3. Implement Algorithm From Scratch.
  4. ??? (magic happens here).
  5. Apply Machine Learning.

It is a lot easier to apply machine learning algorithms to a problem and get a result than it is to implement them from scratch.

A Lot Easier!

Learning how to use an algorithm rather than implement an algorithm is not only easier, it is a more valuable skill. A skill that you can start using to make a real impact very quickly.

There’s a lot of low-hanging fruit that you can pick with applied machine learning.

Implementing Machine Learning Algorithms Well
…is Really Hard!

Algorithms that you use to solve business problems need to be fast and correct.

Fast Algorithms

The more sophisticated nonlinear methods require a lot more data than their linear counterparts.

This means they need to do a lot of work, which may take a long time.

Algorithms need to be fast to process through all of this data. Especially, at scale.

This may require a re-interpretation of the linear algebra that underlies the method in such a way that best suits a specific matrix operation in an underlying library.

It may require specialized knowledge of caching to make the most of your hardware.

These are not ad hoc tricks that come together after you get a “hello world” implementation working. These are engineering challenges that encompass the algorithm implementation project.

Correct Algorithms

Machine learning algorithms will give you a result, even when their implementation is crippled.

You get a number. An output. A prediction.

Sometimes the prediction is correct and sometimes it is not.

Machine learning algorithms use randomness. They are stochastic algorithms.

This is not just a matter of unit tests, it is a matter of having a deep understanding of the technique and devising cases to prove the implementation is as expected and edge cases are handled.

Use An Off-The-Shelf Implementation

You may be an excellent engineer.

But your “hello world” implementation of an algorithm will probably not cut-it when compared to an off-the-shelf implementation.

Your implementation will probably be based on a textbook description, meaning it will be naive and slow. And you may or may not have the expertise to devise tests to ensure the correctness of your implementation.

Off-the-shelf implementations in open source libraries are built for speed and/or robustness.

How could you not use a standard machine learning library?

They may be tailored to a very narrow problem type intended to be as fast as possible. They may also be intended for general purpose use, ensuring they operate correctly on a wide range of problems, beyond those you have considered.

Libraries Are Not All Created Equal

Not all algorithm implementations you download off the Internet are created equal.

The code snippet from GitHub maybe a grad students “hello world” implementation, or it may be the highly optimized implementation contributed to by the entire research team at a large organization.

You need to evaluate the source of the code you are using. Some sources are better or more reliable than others.

General purposes libraries are often more robust at the cost of some speed.

Lighting fast implementations by hacker-engineers often suffer poor documentation and are highly pedantic when it comes to their expectations.

Consider this when you pick your implementation.

Recommendations

When asked, I typically recommend one of three platforms:

  1. Weka. A graphical user interface that does not require any code. Perfect if you want to focus on the machine learning first and learning how to work through problems.
  2. Python. The ecosystem including pandas and scikit-learn. Excellent for stitching together a solution to a machine learning problem in development that is robust enough to also be deployed into operations.
  3. R. The more advanced platform that although has an esoteric language and sometimes buggy packages, offers access to state-of-the-art methods written directly by academics. Great for one-off projects and R&D.

These are just my recommendations, there are many more machine learning platforms to choose from.

Sometimes You Must Implement

You do not have to implement machine learning algorithms when getting started in machine learning.

But you can.

And there can be very good reasons for doing so.

For example here are 3 big reasons:

  • You want to implement to learn how the algorithm works.
  • There is no available implementation of the algorithm you need.
  • There is no suitable (fast enough, etc.) implementation of the algorithm you need.

The first is my favorite. It’s the one that may have confused you.

You can implement machine learning algorithms to learn how they work. I recommend it. It’s very efficient for developers to learn this way.

But.

You do not have to start by implementing machine learning algorithms. You will build your confidence and skill in machine learning a lot faster by learning how to use machine learning algorithms before implementing them.

The implementation and any research required to complete the implementation would then be an improvement on your understanding. An addition that would help you to get better results the next time you used that algorithm.

Summary

In this post, you discovered that beginners fall into the trap of implementing machine learning algorithms from scratch.

They are told that it’s the only way.

You discovered that engineering fast and robust implementations of machine learning algorithms is a tough challenge.

You learned that it is much easier and more desirable to learn how to use machine learning algorithms before implementing them. You also learned that implementing algorithms is a great way to learn more about how they work and get more from them, but only after you know how to use them.

Have you been caught in this trap?
Share your experiences in the comments.

Further Reading


Frustrated With Machine Learning Math?

Mater Machine Learning Algorithms

See How Algorithms Work in Minutes

…with just arithmetic and simple examples

Discover how in my new Ebook: Master Machine Learning Algorithms

It covers explanations and examples of 10 top algorithms, like:
Linear Regression, k-Nearest Neighbors, Support Vector Machines and much more…

Finally, Pull Back the Curtain on
Machine Learning Algorithms

Skip the Academics. Just Results.

Click to learn more.


10 Responses to Stop Coding Machine Learning Algorithms From Scratch

  1. SalemAmeen October 5, 2016 at 1:23 pm #

    I think, this website and related books are the best in data science. I enjoy reading what you have write.

  2. Diego S October 7, 2016 at 1:43 am #

    I’m a professional developer and stock market trader a 12 years from Brazil and now I know that here I found what I was looking for. Amazing and easy articles to learn Machine Learning is here with Jason!

  3. MIke S October 8, 2016 at 10:49 am #

    Good article, thank you.

  4. Dennis October 9, 2016 at 2:23 pm #

    I think you are right that for implementation, unless you are a strong researcher its foolish to implement algos from scratch — but, for the purposes of understanding whats happening under the hood its pretty useful to code something up yourself. Otherwise its just all mystery meat

  5. Sean McClure October 18, 2016 at 8:46 am #

    Excellent post and definitely true. Writing machine learning algorithms from scratch is not a realistic approach to data science and will almost always lead to irrelevant attempts at building a data product that delivers. We must remember that the purpose of data science is to build products that leverage machine learning, and building products well means rapidly attempting many approaches and pivoting in the face of failed iterations. This is not possible if data scientists are caught in the weeds trying to reinvent the wheel on something that has already been built, tested and improved by the machine learning community.

    Our analyses must be held accountable to the standards deemed relevant in product development and there is little chance a home-grown algorithm can live up to that standard. Data scientists operate at a higher level of abstraction than academics that look to make slightly better models via fundamental improvements. Instead, we must learn the ‘art’ of swapping machine learning libraries in and out at a rapid pace, letting the data, and the end user of the product, lead us to the solution. I call it having great ‘package awareness.’ Being aware of which machine learning package/library does what, the knobs it allows us to turn, its strength and weaknesses and its ability to operate in production are what matters.

    There is always value in going deep and appreciating the mathematics of an approach or the elegance of an algorithm’s heuristic, but this depth of appreciation is better garnered through real-world attempts at trying to solve a problem. There is too much danger of attaching oneself to a set of algorithms we are already familiar with or whose underlying elegance we appreciate. The right algorithm is the one that solves the real problem; helping build a product that matters.

    • Jason Brownlee October 19, 2016 at 9:07 am #

      Really great comment Sean, thanks. I could not agree more.

Leave a Reply