Skip to content

Commit b13bb02

Browse files
aswanipranjalnorvig
authored andcommitted
Added Decision Tree Learner example to learning.ipynb. (#686)
* added example for Decision Tree Learner * fixed docstring in learning.py and description in learning.ipynb
1 parent d8c7992 commit b13bb02

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

learning.ipynb

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@
124124
"\n",
125125
"* **examples**: Holds the items of the dataset. Each item is a list of values.\n",
126126
"\n",
127-
"* **attrs**: The indexes of the features (by default in the range of [0,f), where *f* is the number of features. For example, `item[i]` returns the feature at index *i* of *item*.\n",
127+
"* **attrs**: The indexes of the features (by default in the range of [0,f), where *f* is the number of features). For example, `item[i]` returns the feature at index *i* of *item*.\n",
128128
"\n",
129129
"* **attrnames**: An optional list with attribute names. For example, `item[s]`, where *s* is a feature name, returns the feature of name *s* in *item*.\n",
130130
"\n",
@@ -1072,6 +1072,42 @@
10721072
"</ol>"
10731073
]
10741074
},
1075+
{
1076+
"cell_type": "markdown",
1077+
"metadata": {},
1078+
"source": [
1079+
"### Example\n",
1080+
"\n",
1081+
"We will now use the Decision Tree Learner to classify a sample with values: 5.1, 3.0, 1.1, 0.1."
1082+
]
1083+
},
1084+
{
1085+
"cell_type": "code",
1086+
"execution_count": 2,
1087+
"metadata": {},
1088+
"outputs": [
1089+
{
1090+
"name": "stdout",
1091+
"output_type": "stream",
1092+
"text": [
1093+
"setosa\n"
1094+
]
1095+
}
1096+
],
1097+
"source": [
1098+
"iris = DataSet(name=\"iris\")\n",
1099+
"\n",
1100+
"DTL = DecisionTreeLearner(iris)\n",
1101+
"print(DTL([5.1, 3.0, 1.1, 0.1]))"
1102+
]
1103+
},
1104+
{
1105+
"cell_type": "markdown",
1106+
"metadata": {},
1107+
"source": [
1108+
"As expected, the Decision Tree learner classifies the sample as \"setosa\" as seen in the previous section."
1109+
]
1110+
},
10751111
{
10761112
"cell_type": "markdown",
10771113
"metadata": {},
@@ -1760,7 +1796,7 @@
17601796
"name": "python",
17611797
"nbconvert_exporter": "python",
17621798
"pygments_lexer": "ipython3",
1763-
"version": "3.5.3"
1799+
"version": "3.6.3"
17641800
}
17651801
},
17661802
"nbformat": 4,

learning.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ def plurality_value(examples):
542542
return DecisionLeaf(popular)
543543

544544
def count(attr, val, examples):
545-
"""Count the number of examples that have attr = val."""
545+
"""Count the number of examples that have example[attr] = val."""
546546
return sum(e[attr] == val for e in examples)
547547

548548
def all_same_class(examples):

0 commit comments

Comments
 (0)