** 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.
#comment
a = 1 #comment
'''The following will be a comment
b = c
c = d
'''
#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
print(i,f,s,b,l,t,d)
# 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.
l2.append(1)
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)
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.
print(1)
print(2)
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
#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.
return
#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:
print(1)
#program group 2 An error occurs because there is no control part for this program group.
print(2)
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
else:
print("other than that")
#Output other than that
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
print(v)
#Output 1
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.
print(v)
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.
print("(",i,v,")")
#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.
print("(",v0,v1,")")
#output( 1 5 ) ( 2 6 ) ( 3 7 ) ( 4 8 )
a = 3
#a is greater than or equal to 0
while a>=0:
print(a)
#Decrease the value of a by one
a -= 1
#Output 3 2 1 0
#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):
print("p1:",param1,"p2",param2)
#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
#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
si = "1" #Character type
sf = "2.3" #Character type
i = 4 #Integer type
#Integer ⇒ Character conversion and addition of character strings are character combinations.
print(str(i)+si)
# 41
#Character ⇒ Integer conversion, addition after integer conversion is the addition of integers as it is
print(i+int(si))
# 5
#Character ⇒ Floating point conversion, integer+Floating point calculation results are automatically floating point
print(i+float(sf))
# 6.3
#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]
#reference
#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]
#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],
}
print(d)
#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():
print(k,v)
#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"])
#print(jmx,jmi)
#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