I'm studying Python. For the books you read, please refer to the blog post below.
10 recommended books for learning 2020 Python and a memo to Qiita about what you learned
In terms of content, this article focuses on the knowledge needed to understand frameworks written in Python (specifically, deep learning frameworks such as TensorFlow and PyTorch).
The following is a prerequisite.
――We are an eternal beginner. Please be kind
--The basics in Python are intended for readers who understand
--The #
line following the print
means the output result.
--Comments and editing requests are welcome
For the time being, we plan to add and modify it as needed.
Everything in Python seems to be an object. I'm object-oriented in an atmosphere, so I don't really understand the meaning.
Even the class seems to be an object, but I'm not sure what that means. I'm not sure even if I read the following.
Visual Guide to Objects and Classes in Python-Everything is an Object
For the time being, I can read and write programs even if I don't understand them, but if I keep this in mind, I feel like I'll be enlightened someday, so I'll write it first.
It seems obvious, but I think many people haven't read the official documentation (sorry, I didn't read too much).
I've read some Python books and noticed, but the books are also quite wrong, and some parts are omitted to make it easier for beginners to understand, so it's important to read the documentation. .. There is also Japanese.
I think it's tough to read everything from corner to corner, but it seems good to take a quick look at PEP8, which describes the coding rules.
PEP 8 -- Style Guide for Python Code
__hoge__
What is the expression __hoge__
that you often see? Those who say, it seems to be happy if you read the following article.
Even Python beginners want to use __hoge__
! ~ How to use special attributes and special methods ~
__call__
methodYou can write as follows.
class Function:
def __call__(self, x):
return 2*x
f = Function()
print(f(2))
# 4
that? Is this a function? In the method? If you think, you may want to check __call__
.
This is an image that is used quite a bit in deep learning frameworks.
The following four are used when dealing with multiple data in Python.
--List: []
--Tuple: ()
--Set: {}
or set ()
--Dictionary: {}
When writing a simple program by yourself, it is usually enough to use lists, but in deep learning frameworks, things other than lists are often used. Specifically, dictionaries are used to correspond the labels of teacher data with files.
When you read the code or use it yourself, you need to understand how these are different. Also, as I will explain later, please note that the tuple is actually a comma, not a parenthesis.
Reference: [Python] Differences between lists, tuples, dictionaries, and sets
A tuple with one element should be written as follows:
tuple = (1, )
print(tuple)
# (1,)
On the contrary, tuples are OK with just commas as shown below.
tuple = 1,
print(tuple)
# (1,)
In the introductory book, the list is often just []
and the tuple is ()
, so in fact it was a shock to make a tuple with a comma (,
). ..
Also, Python has a method called unpack assignment, which expands and assigns elements to multiple variables (in the code below, ʻa, b = b, a`), but this is actually inside Python. It is treated as a tuple.
a = 1
b = 2
a, b = b, a
print(a,b)
# 2 1
I was surprised because I used this without being aware of it at all.
Reference: Tuples with one element in Python require a comma at the end
For lists, you can write the following, which is called comprehension.
test = [i for i in range(5)]
print(test)
# [0, 1, 2, 3, 4]
Comprehensions can also be used for sets.
test = {i for i in range(5)}
print(test)
# {0, 1, 2, 3, 4}
You can use ʻif` to take out only the ones that meet the conditions, so it is convenient in various ways.
Looking only at the specifications, it seems like "what is it used for?", But with a deep learning framework, it is very useful as a loader for datasets (collections of teacher data). ImageDataGenerator for TensorFlow (Keras) and DataLoader for PyTorch.
In deep learning, learning is carried out in batches (small chunks of data), so this iterator mechanism is very useful. Therefore, if you use the data loader without understanding the mechanism of the iterator, you will not understand it well, so it is good to understand the basics.
I thought that the following etc. are easy to understand in the Qiita article.
Python Iterators and Generators
If you have any problems, you may be able to isolate the problems efficiently if you can use the test program, so it is good to know.
unittest --- Unittest Framework Official Document Note on how to use Python standard unittest What I was addicted to in Python unittest and what I wanted to know earlier
It has nothing to do with the framework, but it's a useful tip when reading and writing Python.
If you need an index on the number of loops, you can use enumerate to concisely write:
test = ['a', 'b', 'c', 'd', 'e']
for i, x in enumerate(test):
print(i, x)
# 0 a
# 1 b
# 2 c
# 3 d
# 4 e
It's not a big deal, but it's so popular that it's useful to know when reading or writing code.
It's useful to know that trying to get an accurate value is a bit annoying.
import math
print(math.modf(1.5))
# (0.5, 1.0)
print(type(math.modf(1.5)))
# <class 'tuple'>
Reference: Get integer and decimal parts of numbers at the same time with Python, math.modf
*
in PythonIn C and C ++, the asterisk is a pointer, so I have only unpleasant memories, but Python is completely different, so I'm relieved (?). It seems good to take a look at the following summary (though I think it's okay to look it up in case of trouble).
Reverse asterisk lookup for Python 3.x
@
You can calculate the matrix product with @
. Please note that it can be used only with Python3.5, NumPy 1.10.0 or later.
import numpy as np
x = np.array([1, 2])
y = np.array([1],[2])
x @ y
# array([5])
The same result can be obtained with np.matmul (x, y)
. There is also np.dot (x, y)
, but this seems to change the result when it is an array of 3 dimensions or more (in 2 dimensions, the same result as matmul, @
) ..
In machine learning systems, matrix multiplication is often used, so you may see it.
I've briefly summarized what I learned in Python in terms of knowledge to deepen my understanding of the framework. I'm thinking of making a note so that I don't forget about other themes.
If you want to know more about Python, please refer to the following articles. Also, if you have any other better books, please let us know in the comments or on Twitter.
10 recommended books for learning 2020 Python and a memo to Qiita about what you learned
Recommended Posts