# 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:

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 python.org 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.

## 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 https://www.python.org/shell/ to try out a short snippet. The downside compared to the Google Colab is you cannot save your work.

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.

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:

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.

## Summary

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.

### 5 Responses to How to Learn Python for Machine Learning

1. Wang Lei September 27, 2021 at 11:03 pm #

This is a great idea to share your blogs with us because your Article helps us to learn so many things through your blogs. Thank you so much admin for uploading such amazing content with us your blog is really helpful for me. wish you all the best for upcoming comments.

2. Łukasz Wojtów September 28, 2021 at 3:56 am #

I’m not sure if that’s a right place to ask this. Moderators: please remove this post if it’s not.

I’m thinking on building a web service where you can import data, train AI models, and later use these models to make predictions on unseen data. It would accept any kind data that can be expressed as numbers and an outcome (something useful to predict). This service would be simple enough to be used by non-programmers. Just point and click in the browser – no experience in data science required. The only requirement: bring your own data. Would anyone be interested in a commercial service like this?

To give a personal example: I have a list of transactions made by a stock trading system and I trained a model to predict which trades are likely to be profitable. My initial tests show that a typical Donchian channel system can be improved from 8% to 59% average trade profit (that is, by removing some trades) and accuracy can be improved from 31% to 57%.

• Adrian Tam September 28, 2021 at 9:47 am #

I don’t think it is appropriate to judge the commercial value. But system like this is surely possible to create.

3. ahmed September 28, 2021 at 10:58 am #

in using weka, if the prediction results in indicators like R, MAE, RMSE shows that my ML classifier is better/worse than in the testing results (ex: we are comparing 3 classifiers) and didn’t keep the same order in the testing as in the prediction. what does that’s mean, I’m really confused about that? Please answer soon

• Adrian Tam September 29, 2021 at 11:43 pm #

How much difference is that? You cannot expect the numbers are exactly the same. What we don’t want to see is the numbers are far off from each other.