Saturday, March 2, 2024
No menu items!
HomeArtificial Intelligence and Machine LearningHow to Learn Python for Machine Learning

How to Learn Python for Machine Learning

Python has become a defacto lingua franca for machine learning. It is not a difficult language to learn, but if you are not particularly familiar with the language, there are some tips that can help you learn faster or better.

In this post, you will discover what is the right way to learn a programming language and how to get help. After reading this post, you will know:

What should be the right mentality to learn Python for use in machine learning
What are the good resources to learn Python
How to find answers for questions related to Python

Let’s get started.

How to Learn Python for Machine Learning
Photo by Federico Di Dio, some rights reserved.

How to learn Python

There are many ways to learn a language, same for natural languages like English, or programming language like Python. Babies learn a language from listening and mimicking. Slowly, when they learned the pattern and some vocabulary, they can make up their own sentences. On the contrary, when college students learn Latin, probably they start with grammar rules. Singular and plural, indicative and subjunctive, nominative and accusative. Then we can build up a Latin sentence.

Similarly, learning Python or any programming language, you can either read other people’s code and try to understand, and then modify from it. Or you can learn the language rules and build up a program from scratch. The latter would be beneficial if your ultimate goal is to work on the language, such as writing the Python interpreter. But usually, the former approach is faster to get some results.

My suggestion is to learn from examples first. But to strengthen your foundation in understanding the language by revisiting the language rules from time to time. Let’s look at an example from Wikipedia:

def secant_method(f, x0, x1, iterations):
“””Return the root calculated using the secant method.”””
for i in range(iterations):
x2 = x1 – f(x1) * (x1 – x0) / float(f(x1) – f(x0))
x0, x1 = x1, x2
return x2

def f_example(x):
return x ** 2 – 612

root = secant_method(f_example, 10, 30, 5)

print(“Root: {}”.format(root)) # Root: 24.738633748750722

This Python code is implementing secant method to find a root for a function. If you are new to Python, what you should do is to look at the example, and see how much you can understand. If you have prior knowledge from other programming languages, you would probably guess def defines a function. But if you do not, you might feel confused and it is best for you to start from a beginner’s book on programming to learn about the concept of functions, variables, loops, etc.

Next thing you might think you can do is to modify the functions. For example, what if we are not using the secant method to find root but to use Newton’s method? You may guess how to modify the equation on line 4 to do it. What about bisection method? You need to add a statement of if f(x2)>0 to decide which way should we go. If we look at the function f_example, we see the symbol **. This is the exponent operator to mean $x$ to the power 2 there. But should we it be $x^2 – 612$ or $x^{2-612}$? You would need to go back and check the language manual to see the operator precedence hierarchy.

Therefore, even a short example like this, you can learn a lot of language features. By learning from more examples, you can deduce the syntax, get used to the idiomic way of coding, and do some work even if you cannot explain it in detail.

What to avoid

If you decided to learn Python, it is inevitable to learn from a book. Just pick up any beginner’s book on Python from your local library should work. But when you read, keep the bigger picture of your learning goal in mind. Do some exercise while you read, try out the codes from the book and make up your own. It is not a bad idea to skip some pages. Reading a book cover to cover may not be the most efficient way to learn. You should prevent yourself drilling too deep into a single topic, because this will make you lost your track to the bigger goal of using Python to do useful things. Topics such as multithreading, network sockets, object-oriented programming can be treated as advanced topics for later.

Python is a language that is decoupled from its interpreter or compiler. Therefore, different interpreter may behave a bit different. The standard interpreter from is the CPython, which is also called the reference implementation. A common alternative is PyPy. Regardless of which one you use, you should learn with Python 3 rather than Python 2 as the latter is an obsoleted dialect. But bear in mind that Python gained its momentum with Python 2 and you may still see quite a lot of Python 2 program around.


Reading Resources

If you cannot go to the library to pick up a printed book, you can make use of some online resources instead. I would highly recommend beginners to read The Python Tutorial. It is short but guides you to through different aspects of the language. It lets you have a glance on what Python can do, and how to do it.

After the tutorial, probably you should keep the Python Language Reference and the Python Library Reference handy. You will reference to them from time to time to check the syntax and look up function usages. Do not force yourself to remember every function.

Programming Environment

Python is built-in in macOS but you may want to install a newer version. In Windows, it is common to see people using Anacronda instead of installing just the Python interpreter. But if you feel it is too much hassle to install an IDE and the Python programming environment, you may consider to use Google Colab. This allows you to write Python program in a “notebook” format. Indeed, many machine learning projects are developed in the Jupyter notebook as it allows us to quickly explore different approaches to a problem and visually verify the result.

You can also use an online shell at to try out a short snippet. The downside compared to the Google Colab is you cannot save your work.

Asking for help

When you start from an example you saw from a book and modifying it, you might break the code and make it failed to run. It is especially true in machine learning examples, where you have many lines of code that covered from data collection, preprocessing, building a model, training, validation, prediction and finally presenting the result in a visualized manner. When you see error from your code, the first thing you need to do is to pin point the few lines that caused the error. Try to check the output from each step to make sure it is in a correct format. Or try to roll back your code to see which change you made started to introduce errors.

It is important to make mistakes and learn from mistakes. When you try out syntax and learn your way, you should encounter error messages from time to time. Try to make sense from it, then you will be easier to figure out what was causing the error. Almost always, if the error comes from a library that you’re using, double confirm your syntax with the library’s documentation.

If you are still confused, try to search it on the internet. If you’re using Google, one trick you can use is to put the entire error message in a pair of double quotes when you search it. Or sometimes, search on StackOverflow might give you better answers.

Further Readings

Here I list out some pointers for a beginner. As referenced above, the Python Tutorial is a good start. This is especially true at the time of writing, when Python 3.9 rolled out recently and some new syntax are introduced. Printed books are usually not as updated as the official tutorial online.

There are many primer level books for Python. Some short ones that I knew are:

Python Crash Course, 2nd edition, by Eric Matthes, 2019.
Introduction to Computation and Programming Using Python, 3rd edition, by John Guttag, 2021.

For a bit more advanced learner, you may want to see more examples to get something done. A cookbook style book might help a lot as you can learn not only the syntax and language tricks, but also the different libraries that can get things done.

Python Cookbook, 3rd edition, by David Beazley and Brian K. Jones, 2013.


In this post, you learned about how should one study Python and the resources that can help you start. A goal-oriented approach to study can help you get the result quicker, but as always, you need to spend some significant time into it before you become proficient.

The post How to Learn Python for Machine Learning appeared first on Machine Learning Mastery.

Read MoreMachine Learning Mastery



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments