Python learning memo for machine learning by Chainer until the end of Chapter 2

What This is an article that summarizes what I noticed and researched when learning machine learning using Chainer. This time, I will study control syntax ʻif and while`, which are often used in various languages.

** Thank you ** I wrote it for my own reflection, but I am surprised to see it unexpectedly and show a reaction. We would also like to thank you for taking the time to comment and point out. It is very helpful.

Finally It is written based on my understanding, so it may be incorrect. I will correct any mistakes, please forgive me.

Content

2. Notes on getting started with Python Continued

if Familiar conditional branching type. if, elif, else. The following is a description example. Neither {} nor ʻendis required and it is easy to see! There seems to be nothing special about if. ** Executed when the conditional expression returnsTrue` **

if a > 0:    
    print('Greater than 0')
elif a == 0:
    print('0')
else:
    print('Less than 0')

while There is nothing special about the while loop, and it is easy to read with a clean description! ʻIfis also executed when the conditional expression returnsTrue`.

count = 0

while count < 3:  #No conditional expression not False Conditional expression is OK
    print(count)
    count += 1
    break      #You can also force the loop out with

I was curious when I wrote it, but where is the ** 1 loop? ** If you look it up, this is the description

Python treats statements with the same indentation as the same block without using special characters.

I want to be careful about managing intensity. Another thing, I checked if there is a conditional expression judgment method other than True, Flase

bool type is a subclass of int type. Bool type True and False are equivalent to 1, 0

Because there was, it seems that numbers can be used. Subclass? ??

function

The definition of the function is also simple. You can also initialize the arguments and pass them as shown below. Since the extend is equivalent to the block, the end of the definition is not specified.

def hello(message='Welcome to the Chainer Tutorial'):
    print(message)
    return message #You can also specify the return value

Variable scope (lifetime)

Basically, it will be initialized when you exit the block. Certainly the same as C language.

a = 1
#Substitute 2 for a inside the function
def change():
    a = 2  
change()
a # =1 is output

To handle variables globally, write global. The following is a sample sentence, but there are unclear points ...

a = 1
def change():
    global a  #Declaration that a is a global variable
    a = 2       #Assignment to a global variable
change()
a # =2 is output

Does this mean that you first define ʻa with ʻa = 1 and then redefine it as global a? Or is the first ʻa and global a` distinguished in the program? On the other hand, when I wrote the following, I got a syntax error.

global a = 1
def change():
    a = 2       #Assignment to a global variable
change()
a

I can't imagine the cause for a moment, but is the global declaration valid only within the local block? It's like changing from a local variable to a global variable. Isn't it possible to make a declaration that can be used globally from the beginning like C language? If you are really in trouble, let's find out.

Class The concept of objects can be imagined through C ++ and Ruby. init is a constructor. self indicates the created instance itself.

The method automatically passes a reference to that instance as the first argument when called from the instance.

Because there is, it seems safe to think that self itself is a pointer. By the way, is self.name_plate a built-in function? (Or similar variables)

class House:
    def __init__(self, name):
        self.name_plate = name

my_house = House('Chainer') #Declaration

One argument. Since the first argument is given as self, it is not necessary to specify it. Not limited to init, self does not need to be specified at the time of calling.

Inheritance

It can be inherited like C ++. Chain is the child class and Link is the parent class. The point is that the constructor calls the constructor of the parent class if there is no description. Use super () to refer to a parent class from a child class. The function with the same name as the parent class is overridden.

class Chain(Link):    

    def __init__(self):
        #Of the parent class`__init__()`Call the method
        super().__init__()

    def true():
        return True

** This is the end of Chapter 2 Python learning ** It was long ... w Next time, I will work on Chapter 3. Comment It's getting cold recently, so I took a bath for the first time this year. As the weekends get tired, I tend to get sleepy even if I try to study at the end of work, but if I take a good bath, I can secure enough concentration until bedtime. In order to continue, I think it's best to keep your body in good condition, do not overdo it, and keep working for a short time with high concentration.

Recommended Posts

Python learning memo for machine learning by Chainer until the end of Chapter 2
Python learning memo for machine learning by Chainer from Chapter 2
Python learning memo for machine learning by Chainer Chapter 13 Basics of neural networks
Python learning memo for machine learning by Chainer Chapter 7 Regression analysis
Python learning memo for machine learning by Chainer Chapter 8 Introduction to Numpy
Python learning memo for machine learning by Chainer Chapter 10 Introduction to Cupy
Python learning memo for machine learning by Chainer Chapter 9 Introduction to scikit-learn
Python learning memo for machine learning by Chainer Chapter 13 Neural network training ~ Chainer completed
Python learning memo for machine learning by Chainer Chapters 1 and 2
[Learning memo] Basics of class by python
Pandas of the beginner, by the beginner, for the beginner [Python]
Python & Machine Learning Study Memo ④: Machine Learning by Backpropagation
A memorandum of scraping & machine learning [development technique] by Python (Chapter 4)
The story of low learning costs for Python
Upgrade the Azure Machine Learning SDK for Python
Python & Machine Learning Study Memo ②: Introduction of Library
Take the free "Introduction to Python for Machine Learning" online until 4/27 application
[Machine learning pictorial book] A memo when performing the Python exercise at the end of the book while checking the data
Align the number of samples between classes of data for machine learning with Python
Summary of the basic flow of machine learning with Python
Memo for building a machine learning environment using Python
Pandas of the beginner, by the beginner, for the beginner [Python]
Code for checking the operation of Python Matplotlib
Explain the code of Tensorflow_in_ROS
Check the memory protection of linux kerne with the code for ARM
Example of code rewriting by ast.NodeTransformer
About Python code for simple moving average assuming the use of Numba
Python learning memo for machine learning by Chainer until the end of Chapter 2
The first step of machine learning ~ For those who want to implement with python ~
[Machine learning] "Abnormality detection and change detection" Let's draw the figure of Chapter 1 in Python.
Predict the presence or absence of infidelity by machine learning
Introduction to Deep Learning for the first time (Chainer) Japanese character recognition Chapter 4 [Improvement of recognition accuracy by expanding data]
Machine learning summary by Python beginners
<For beginners> python library <For machine learning>
Interval scheduling learning memo ~ by python ~
"Scraping & machine learning with Python" Learning memo
[Python machine learning] Recommendation of using Spyder for beginners (as of August 2020)
[Python of Hikari-] Chapter 07-02 Exception handling (continuous execution of the program by exception handling)
How to use machine learning for work? 01_ Understand the purpose of machine learning
Summary of pages useful for studying the deep learning framework Chainer
Python & Machine Learning Study Memo: Environment Preparation
Amplify images for machine learning with python
Learning notes from the beginning of Python 1
[Shakyo] Encounter with Python for machine learning
[Python] Web application design for machine learning
An introduction to Python for machine learning
Python Basic Course (at the end of 15)
Python & Machine Learning Study Memo ③: Neural Network
Learning notes from the beginning of Python 2
Python & Machine Learning Study Memo ⑥: Number Recognition
Judgment of igneous rock by machine learning ②
[Introduction to machine learning] Until you run the sample code with chainer
[Python] [Machine learning] Beginners without any knowledge try machine learning for the time being
[Example of Python improvement] What is the recommended learning site for Python beginners?
Judge the authenticity of posted articles by machine learning (Google Prediction API).
100 language processing knocks for those who do not understand the meaning of problem sentences Chapter 8 Machine learning
Machine learning memo of a fledgling engineer Part 1
Classification of guitar images by machine learning Part 1
The contents of the Python tutorial (Chapter 5) are itemized.
The contents of the Python tutorial (Chapter 4) are itemized.
The contents of the Python tutorial (Chapter 2) are itemized.
The contents of the Python tutorial (Chapter 8) are itemized.
The contents of the Python tutorial (Chapter 1) are itemized.
Deep Learning from scratch The theory and implementation of deep learning learned with Python Chapter 3
Send Gmail at the end of the process [Python]
Image processing? The story of starting Python for
[Python] Collect images with Icrawler for machine learning [1000 images]