Hire a web Developer and Designer to upgrade and boost your online presence with cutting edge Technologies

Friday, 17 January 2025

How to Learn Python for Machine Learning

Python has become a de facto 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 the right way to learn a programming language is and how to get help. After reading this post, you will know:

  • The right mentality to learn Python for use in machine learning
  • Good resources to learn Python
  • How to find answers for questions related to Python

How to Learn Python

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

Similarly, when learning Python or any programming language, you can either read other people’s code, 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 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 the secant method to find a root for a function. If you are new to Python, you should 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. If this is the case, it is best for you to start from a beginner’s book on programming to learn about the concept of functions, variables, loops, etc.

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

Therefore, even with 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 idiomatic way of coding, and do some work even if you cannot explain it in detail.

What to Avoid

If you decide to learn Python, it is inevitable you will want to learn from a book. Just picking 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 exercises 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 from drilling too deep into a single topic because this will make you lose track of the bigger goal of using Python to do useful things. Topics such as multithreading, network sockets, and 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 interpreters may behave a bit differently. The standard interpreter from python.org is CPython, 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 obsolete dialect. But bear in mind that Python gained its momentum with Python 2, and you may still see quite a lot of Python 2 programming around.

Resources

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 read The Python Tutorial. It is short but guides you through different aspects of the language. It lets you take a peek at what Python can do and how to do it.

After the tutorial, you probably should keep the Python Language Reference and the Python Library Reference handy. You will reference them from time to time to check the syntax and lookup 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 Anaconda 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 might consider using Google Colab. This allows you to write Python programs 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 that you cannot save your work.

Asking for Help

When you start from an example you saw from a book and modify it, you might break the code, making it fail to run. It is especially true in machine learning examples, where you have many lines of code that cover data collection, preprocessing, building a model, training, validation, prediction, and finally presenting the result in a visualized manner. When you see an error result from your code, the first thing you need to do is pinpoint the few lines that caused the error. Try to check the output from each step to make sure it is in the 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 this way, you should encounter error messages from time to time. If you try to make sense from it, then it will be easier to figure out what caused 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 for 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. Or sometimes, searching  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 this writing when Python 3.9 rolled out recently and some new syntax was 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 know of 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 how one should study Python and the resources that can help you start. A goal-oriented approach to study can help you get the result quicker. However, as always, you need to spend some significant time on it before you become proficient.

No comments:

Post a Comment

Connect broadband

How to Learn Python for Machine Learning

Python has become a de facto lingua franca for machine learning. It is not a difficult language to learn, but if you are not particularly fa...