chroe(a1): update excercise, use python instead of jupyter

This commit is contained in:
2026-04-30 18:16:21 +02:00
parent 5cef52cd01
commit fc5cd0fb58
3 changed files with 46 additions and 1 deletions
+8 -1
View File
@@ -1259,6 +1259,13 @@
" filled=True, rounded=True, \n",
" special_characters=True) "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
@@ -1277,7 +1284,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.12"
"version": "3.14.4"
}
},
"nbformat": 4,
+38
View File
@@ -0,0 +1,38 @@
"""
Use a decision tree classifier to predict flowers based on sepal and petal length/width
"""
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.tree import DecisionTreeClassifier
from sklearn import tree
# load the iris data set and look at its dimensions
iris = datasets.load_iris()
print(iris.data.size)
print(iris.target.size)
print(iris.feature_names)
print(iris.target_names)
# use a decition tree classifier
classifier = DecisionTreeClassifier()
# use all but the last sample for training
classifier.fit(iris.data[:-1], iris.target[:-1])
# use the model to predict the last data sample
last_sample = iris.data[-1:]
last_target = iris.target[-1:]
print(f"predicted: {last_sample} vs real: {last_target}")
# print the tree for visual inspection
fig, ax = plt.subplots(figsize=(20, 10))
tree.plot_tree(
classifier,
feature_names=iris.feature_names,
class_names=iris.target_names,
filled=True,
rounded=True,
ax=ax,
)
fig.savefig("tree.png", dpi=150, bbox_inches="tight")
Binary file not shown.

After

Width:  |  Height:  |  Size: 280 KiB