2. Make a decision tree from 0 with Python and understand it (2. Python program basics)

** Create and understand decision trees from scratch in Python ** 1. Overview --2 Python Program Basics

I will explain the Python program for creating a decision tree.

2.1 Comments are # or'''' (3 single quotes)

a = 1 #comment

'''The following will be a comment
b = c
c = d

2.2 Type is dynamically typed (type is automatically determined)

#Variables are dynamically typed
# =Assigns (copies) values from right to left.
i = 1 #Integer type(int)
f = 2.1 #Floating point type(float)
s = "a" #Character type(string)
b = True #Boolean type(bool)
l = [0,1,2] #Array, list type
t = (0,1,2) #Tuple type
d = {"a":0, "b":1} #Dictionary, associative array type

# 1 2.1 a True [0, 1, 2](0, 1, 2) {'a': 0, 'b': 1}

#If you want to find out the type, use type.
print(type(i)) #output<class 'int'>

#The variable does not hold the actual value, but stores the actual value
#It is a variable called a reference type that points to a location.
#You can get the identifier of the actual value by id.
print(id(l)) # 00000000000000 (It depends on the execution)
l2 = l #For example, a copy of an array only references two, and the actual array is only one.
print(id(l2)) # 00000000000000 (Id above(l)Will be the same value as)
#Since there is only one actual array, adding an element with a reference to l2 will appear to have been added to the array l as well.

2.3 Operator (calculate or assign a value. The calculation result is basically put in a variable)

a = 1 #a contains 1
b = a #b also contains 1
a = 2 #a is overwritten by 2, b remains unchanged
a += 1 #The value of a increases by one(At this point a is 3)
a -= 1 #The value of a is decremented by one(At this point a is 2)
a *= 2 #The value of a doubles(At this point a is 4)
a /= 3 #The value of a becomes one-third(At this point a is 1.3333333333333333)
a = 1+2-3*4/5 #Calculated in the order of multiplication, division, addition, subtraction, a is 0.6
# (However, the floating point error is actually 0.6000000000000001)

2.4 Create a group of programs by indenting (shifting letters to the right)

a = 0
#Specify the execution conditions of the indented program group on the lower right side.
#There are conditions, repetitions, functionalizations, etc. shown below.
if a == 1:
    #The line that was shifted to the right by the indent after the if and the left side of the line was shifted to the right by the same amount
    #Make up one program group.
    pass #Indicates the end of the group so far, explicitly indented.
    #The line above this is a group of programs that will be executed when the if condition is met.
print(3) #Output regardless of the value of a, not subject to the above if

#Output 3

2.4.1 Grouped programs can be given control over their operation.

#Run or not, only once if run
if condition:
    # program group

#Repeat execution only for the elements of the array.
#The elements of the array are assigned to the variable v in the middle of the repetition in order from the front.
for v in array:
    # program group

#Continue running the program group as long as the conditions are met
while condition:
    # program group

#Decide when to execute later
#(Create a group and give it a name for the time being)
def Program group name():
    # program group
    #The result of executing this program group was executed using return.
    #It may be returned to the place.

#This is an error
#Indent changes print(2)Becomes another program group.
#But program group 2 has no explanation
if a == 1:
    #program group 1 Control of this program group is if a==1:
        #program group 2 An error occurs because there is no control part for this program group.

2.4.2 Conditional statement

a = 1
b = 2
c = 2

#a and b are the same, and a and c are different
if a==b and a!=c:
    print("Only c is different")

#a is greater than or equal to b(a and b contain the same), Or a exceeds c(a and c do not contain the same)
elif a>=b or a>c:
    print("a is greater than or equal to b or greater than c")

# if,Other than elif conditions
    print("other than that")

#Output other than that Another example of how to write a conditional statement
a = 1
b = 2
#0 is assigned to v if a and b are the same, and 1 is assigned otherwise.
v = 0 if a==b else 1
#Output 1

2.4.3 Repeated statement (for)

for v in [0,1,2,3]: #Array[0,1,2,3]The following processing is repeated for the number of elements of.
    #Processing, at this time v is set to the elements of the array in order from the front.
    pass #Explicitly end the indentation here.
#Output 0 1 2 3

#By using enumerate, the index and value of the array can be obtained in the iterative process.
for i,v in enumerate([5,3,7,8]):
    #i is the index and v is the element value.
#output( 0 5 ) ( 1 3 ) ( 2 7 ) ( 3 8 )

#Zip allows you to combine two arrays into one and iterate over it.
for v0,v1 in zip([1,2,3,4],[5,6,7,8]):
    #v0 contains the elements of the array of the first argument of zip, and v1 contains the elements of the second argument.
#output( 1 5 ) ( 2 6 ) ( 3 7 ) ( 4 8 )

2.4.4 Repeat statement (while)

a = 3

#a is greater than or equal to 0
while a>=0:
    #Decrease the value of a by one
    a -= 1

#Output 3 2 1 0

2.4.5 Functions and methods

#Function definition name is function_name,The arguments are param1 and param2, and param2 is the default argument.
#The argument used when not set when calling the function is specified.
def function_name(param1,param2=1):
    #This function calls param1+Returns the result of param2.
    return param1 + param2

#Function call (for the first time here, function_The program named name runs
#param1 argument is 5, param2 is not set (default argument is used)
v = function_name(5)
#output(function_Output by print statement in name function) p1: 5 p2 1
print("Return value",v)
#Output return value 6 Another example of how to write a lambda expression or function
#Function name= lambda (argument) : (Return value)Write the function in the way of writing.
f = lambda x: x*x
#When calling, it is the same as the function definition by def
v = f(2)
print(v) #Display as 4

2.5 Character type ⇔ Numeric type conversion

si = "1"   #Character type
sf = "2.3" #Character type
i = 4      #Integer type

#Integer ⇒ Character conversion and addition of character strings are character combinations.
# 41

#Character ⇒ Integer conversion, addition after integer conversion is the addition of integers as it is
# 5

#Character ⇒ Floating point conversion, integer+Floating point calculation results are automatically floating point
# 6.3

2.6 Array, list

#Array generation
a = [1,1,2,3,2]              # a=[[1,1,2,3,2]
b = [n for n in range(8)]  # b=[0, 1, 2, 3, 4, 5, 6, 7]

#Negative value is-It is an index that counts from the back with 1 as the last element.
v = a[0]  # v=1
v = a[-1] # v=2

#add to
a += [4]     # a=[1, 1, 2, 3, 2, 4]
a.append(5)  # a=[1, 1, 2, 3, 2, 4, 5]

#take out(A before execution=[1, 1, 2, 3, 2, 4, 5])
v = a.pop(0) # v=1, a=[1, 2, 3, 2, 4, 5]

#slice(A before execution=[1, 2, 3, 2, 4, 5])
# a[First index:Index one ahead of the end]
# a[:]If the index is omitted as in, the first is 0 and the last is the number of elements.
c = a[1:3] # c=[2, 3]

#Maximum minimum(A before execution=[1, 2, 3, 2, 4, 5])
mx,mi = max(a),min(a) # mx=5, mi=1

#average(mean),Median(median), Mode(mode),standard deviation(stdev), Distributed(variance)
# (A before execution=[1, 2, 3, 2, 4, 5])
from statistics import mean,median,mode,stdev,variance
v = mean(a) # v=2.8333333333333335
v = median(a) # v=2.5
v = mode(a) # v=2
v = stdev(a) # v=1.4719601443879744
v = variance(a) #v=2.1666666666666665

#Deduplication(A before execution=[1, 2, 3, 2, 4, 5])
c = set(a) # c={1, 2, 3, 4, 5}

#Sort (create a new sorted array)(A before execution=[1, 2, 3, 2, 4, 5])
c = sorted(a) # c=[1, 2, 2, 3, 4, 5](a remains unchanged)

#Sort (replace the contents of the array with the sorted one)(A before execution=[1, 2, 3, 2, 4, 5])
a.sort()      # a=[1, 2, 2, 3, 4, 5]

2.7 dictionary, associative array

#The generation, the key is the weather, the temperature, and the array is specified for the value.
d = {
    "National language" : [81, 60, 97, 96],
    "Math" : [80, 78, 75, 96],
    "English" : [76, 85, 65, 88],
#output{'National language': [81, 60, 97, 96], 'Math': [80, 78, 75, 96], 'English': [76, 85, 65, 88]}

#Get value by specifying key
a = d["Math"] # a=[80, 78, 75, 96]

#Set values by specifying a key (overwrite)
d["English"] = [77, 61, 91, 87] # d["English"]=[77, 61, 91, 87]

#Data loop
for k,v in d.items():
#Output national language[81, 60, 97, 96]Math[80, 78, 75, 96]English[77, 61, 91, 87]

#Maximum, minimum (in the case of national language score)
jmx,jmi = max(d["National language"]),min(d["National language"])

#Acquisition of subject name with the highest average value
from statistics import mean
#For max of the dictionary type array, you can specify a function in the key argument as to what to use to find the maximum.
#In this case, the argument k(Dictionary type key value)The lambda expression is used to find the average of the values from.
kmx = max(d,key=lambda k:mean(d[k])) # kmx="National language"

Recommended Posts

2. Make a decision tree from 0 with Python and understand it (2. Python program basics)
Make a decision tree from 0 with Python and understand it (4. Data structure)
Create a decision tree from 0 with Python and understand it (5. Information Entropy)
Create a decision tree from 0 with Python and understand it (3. Data analysis library Pandas edition)
Create a decision tree from 0 with Python (1. Overview)
Associate Python Enum with a function and make it Callable
Let's write a Python program and run it
Make a Python program a daemon and run it automatically when the OS starts
WEB scraping with python and try to make a word cloud from reviews
Let's make a simple game with Python 3 and iPhone
From buying a computer to running a program with python
Get mail from Gmail and label it with Python3
Make a fortune with Python
Hash with python and escape from a certain minister's egosa
Creating a decision tree with scikit-learn
Let's make a GUI with python.
python / Make a dict from a list.
I made a server with Python socket and ssl and tried to access it from a browser
Make a recommender system with python
Let's make a graph with python! !!
Precautions when inputting from CSV with Python and outputting to json to make it an exe
How to make a surveillance camera (Security Camera) with Opencv and Python
Make a simple OMR (mark sheet reader) with Python and OpenCV
Get data from MySQL on a VPS with Python 3 and SQLAlchemy
Make a thermometer with Raspberry Pi and make it viewable with a browser Part 4
I tried to make a periodical process with Selenium and Python
Read a file in Python with a relative path from the program
Make a scraping app with Python + Django + AWS and change jobs
I made a segment tree with python, so I will introduce it
Understand the probabilities and statistics that can be used for progress management with a python program
[Python] A program that creates stairs with #
Let's make a shiritori game with Python
Fractal to make and play with Python
A memo with Python2.7 and Python3 on CentOS
Let's make a voice slowly with Python
Understand the Decision Tree and classify documents
Let's make a web framework with Python! (1)
Make a desktop app with Python with Electron
Let's make a Twitter Bot with Python!
Let's make a web framework with Python! (2)
Deploy a Python app on Google App Engine and integrate it with GitHub
A python program that resizes a video and turns it into an image
[Ev3dev] Let's make a remote control program by Python with RPyC protocol
I tried to make a periodical process with CentOS7, Selenium, Python and Chrome
Make a thermometer with Raspberry Pi and make it visible on the browser Part 3
[Python] I introduced Word2Vec and played with it.
Make a Twitter trend bot with heroku + Python
[Python] Make a game with Pyxel-Use an editor-
Building a python environment with virtualenv and direnv
Divide each PowerPoint slide into a JPG file and output it with python
I want to make a game with Python
Try to make a "cryptanalysis" cipher with Python
Make OpenCV3 available from python3 installed with pyenv
[Python] Make a simple maze game with Pyxel
Launch a web server with Python and Flask
Implementation of TRIE tree with Python and LOUDS
Let's replace UWSC with Python (5) Let's make a Robot
Load a photo and make a handwritten sketch. With zoom function. Tried to make it.
Try to make a dihedral group with Python
Read line by line from a file with Python
Make JSON into CSV with Python from Splunk