Machine Learning - Beslisbomen
- Previous Page Training/Testing
- Next Page MySQL Basics

Beslissingsboom (Decision Tree)
In dit hoofdstuk zullen we u laten zien hoe u een 'beslissingsboom' kunt maken. Een beslissingsboom is een stroomschema dat u kan helpen beslissingen te nemen op basis van eerdere ervaringen.
In dit voorbeeld zal iemand proberen te beslissen of hij/zij moet deelnemen aan een komedievoorstelling.
Gelukkig registreert ons voorbeeldfiguur zich elke keer dat er een komedievoorstelling wordt gegeven in het dorp, en registreert hij/zij enkele informatie over komiek artiesten, en registreert hij/zij ook of hij/zij er al geweest is.
Age | Experience | Rank | Nationality | Go |
---|---|---|---|---|
36 | 10 | 9 | UK | NO |
42 | 12 | 4 | USA | NO |
23 | 4 | 6 | N | NO |
52 | 4 | 4 | USA | NO |
43 | 21 | 8 | USA | YES |
44 | 14 | 5 | UK | NO |
66 | 3 | 7 | N | YES |
35 | 14 | 9 | UK | YES |
52 | 13 | 7 | N | YES |
35 | 5 | 9 | N | YES |
24 | 3 | 5 | USA | NO |
18 | 3 | 7 | UK | YES |
45 | 9 | 9 | UK | YES |
Nu kan Python, gebaseerd op deze dataset, een beslisboom creëren die kan worden gebruikt om te beslissen of het de moeite waard is om aan een nieuw optreden deel te nemen.
Werkingsprincipe
Eerst importeer de benodigde modules en lees de dataset met pandas in:
Example
Lees en print de dataset:
import pandas from sklearn import tree import pydotplus from sklearn.tree import DecisionTreeClassifier import matplotlib.pyplot as plt import matplotlib.image as pltimg df = pandas.read_csv("shows.csv") print(df)
Om een beslisboom te maken, moeten alle gegevens numeriek zijn.
We moeten de niet-numerieke kolommen 'Nationality' en 'Go' omzetten in numerieke waarden.
Pandas heeft een map()
Method, deze methode accepteert een dictionary die informatie bevat over hoe waarden moeten worden omgezet.
{'UK': 0, 'USA': 1, 'N': 2}
Dit betekent dat de waarde 'UK' wordt omgezet in 0, 'USA' in 1 en 'N' in 2.
Example
Stringwaarden wijzigen in numerieke waarden:
d = {'UK': 0, 'USA': 1, 'N': 2} df['Nationality'] = df['Nationality'].map(d) d = {'YES': 1, 'NO': 0} df['Go'] = df['Go'].map(d) print(df)
Daarna moeten we de kenmerkkolom en de doelkolom scheiden.
De kenmerkkolom is de kolom waaruit we proberen te voorspellen, de doelkolom is de kolom met de waarden die we proberen te voorspellen.
Example
X is de kenmerkkolom, y is de doelkolom:
features = ['Age', 'Experience', 'Rank', 'Nationality'] X = df[features] y = df['Go'] print(X) print(y)
Nu kunnen we een daadwerkelijke beslisboom creëren die past bij onze details en vervolgens een .png-bestand opslaan op de computer:
Example
Maak een beslisboom, sla deze op als afbeelding en toon deze afbeelding:
dtree = DecisionTreeClassifier() dtree = dtree.fit(X, y) data = tree.export_graphviz(dtree, out_file=None, feature_names=features) graph = pydotplus.graph_from_dot_data(data) graph.write_png('mydecisiontree.png') img = pltimg.imread('mydecisiontree.png') imgplot = plt.imshow(img) plt.show()
Resultaat uitleg
De beslisboom gebruikt uw eerdere beslissingen om de kans te berekenen of u een komische acteur wilt zien.
Laat ons de verschillende aspecten van de beslisboom lezen:

Rank
Rank <= 6.5
Vertegenwoordigt dat acteurs met een rang van 6.5 of lager zullen volgen: True
Pijl (naar links), de rest volgt. False
Pijl (naar rechts).
gini = 0.497
Vertegenwoordigt de kwaliteit van de splitsing en is altijd een getal tussen 0.0 en 0.5, waarbij 0.0 betekent dat alle monsters hetzelfde resultaat krijgen, en 0.5 betekent dat de splitsing volledig in het midden wordt gedaan.
samples = 13
Vertegenwoordigt dat er nog steeds 13 komische acteurs over zijn op dit beslispunt, omdat dit de eerste stap is, zijn ze allemaal komische acteurs.
value = [6, 7]
Vertegenwoordigt dat van de 13 komische acteurs er 6 een "NO" zullen krijgen, en 7 een "GO" zullen krijgen.
Gini
Er zijn veel manieren om monsters te splitsen, in dit tutorial gebruiken we de GINI-methode.
De Gini-methode gebruikt de volgende formule:
Gini = 1 - (x/n)2 - (y/n)2
Waarbij x het aantal bevestigende antwoorden ("GO") is, n het aantal monsters, en y het aantal ontkennende antwoorden ("NO") is, berekend met de volgende formule:
1 - (7 / 13)2 - (6 / 13)2 = 0.497

Het volgende bevat twee vakken, waarvan een voor komische acteurs met een 'Rank' van 6.5 of lager, en de rest is een vak.
True - Hier eindigen 5 komische acteurs:
gini = 0.0
Vertegenwoordigt dat alle monsters hetzelfde resultaat krijgen.
samples = 5
Vertegenwoordigt dat er nog steeds 5 komische acteurs in deze tak (de rang van 5 acteurs is 6.5 of lager).
value = [5, 0]
Indicating that 5 get 'NO' and 0 get 'GO'.
False - 8 drama actors continue:
Nationality (Nationality)
Nationality <= 0.5
Indicating that comedians with a nationality value less than 0.5 will follow the left arrow (this means everyone from the UK), and the rest will follow the right arrow.
gini = 0.219
Meaning that about 22% of the samples will move in one direction.
samples = 8
Indicating that there are still 8 comedians left in this branch (8 comedians with a rating higher than 6.5).
value = [1, 7]
Indicating that among these 8 comedians, 1 will receive 'NO', and 7 will receive 'GO'.

True - 4 drama actors continue:
Age (Age)
Age <= 35.5
Indicating that comedians aged 35.5 years or younger will follow the left arrow, and the rest will follow the right arrow.
gini = 0.375
Meaning that about 37.5% of the samples will move in one direction.
samples = 4
Indicating that there are still 4 comedians left in this branch (4 comedians from the UK).
value = [1, 3]
Indicating that among these 4 comedians, 1 will receive 'NO', and 3 will receive 'GO'.
False - 4 comedians end here:
gini = 0.0
It indicates that all samples get the same result.
samples = 4
Indicating that there are still 4 comedians left in this branch (4 comedians from the UK).
value = [0, 4]
Indicating that among these 4 comedians, 0 will receive 'NO', and 4 will receive 'GO'.

True - 2 comedians end here:
gini = 0.0
It indicates that all samples get the same result.
samples = 2
Indicating that there are still 2 comedians left in this branch (2 comedians 35.5 years old or younger).
value = [0, 2]
Indicating that in these 2 comedians, 0 will receive 'NO', and 2 will receive 'GO'.
False - 2 drama actors continue:
Experience (Experience)
Experience <= 9.5
Indicating that comedians with 9.5 years of experience or more will follow the left arrow, and the rest will follow the right arrow.
gini = 0.5
Indicating that 50% of the samples will move in one direction.
samples = 2
Indicating that there are still 2 comedians left in this branch (2 comedians over 35.5 years old).
value = [1, 1]
Indicating that 1 of the 2 comedians will receive 'NO', and 1 will receive 'GO'.

True - 1 comedy actor ends here:
gini = 0.0
It indicates that all samples get the same result.
samples = 1
It indicates that there is still 1 comedian left in this branch (1 comedian with 9.5 years or less of experience).
value = [0, 1]
It indicates that 0 represents "NO", 1 represents "GO".
False - 1 comedian here:
gini = 0.0
It indicates that all samples get the same result.
samples = 1
It indicates that there is still 1 comedian left in this branch (1 comedian with more than 9.5 years of experience).
value = [1, 0]
1 represents "NO", 0 represents "GO".
Predicted Values
We can use the decision tree to predict new values.
For example: Should I watch a show starring a 40-year-old American comedian with 10 years of experience and a comedy rating of 7?
Example
Use predict()
Methods to predict new values:
print(dtree.predict([[40, 10, 7, 1]]))
Example
What is the answer if the comedy level is 6?
print(dtree.predict([[40, 10, 6, 1]]))
Different Results
If it is run enough times, even if you enter the same data, the decision tree will provide you with different results.
This is because the decision tree cannot give us 100% definite answers. It is based on the probability of the results, and the answers may vary.
- Previous Page Training/Testing
- Next Page MySQL Basics