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
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:
ef 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 x2–612 or x2−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. 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.
This article has been published from the source link without modifications to the text. Only the headline has been changed.
Source link