diff --git a/ML/aufgaben/a1/decisiontree_iris.ipynb b/ML/aufgaben/a1/decisiontree_iris.ipynb index af067c2..8b2b4c0 100644 --- a/ML/aufgaben/a1/decisiontree_iris.ipynb +++ b/ML/aufgaben/a1/decisiontree_iris.ipynb @@ -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, diff --git a/ML/aufgaben/a1/decisiontree_iris.py b/ML/aufgaben/a1/decisiontree_iris.py new file mode 100644 index 0000000..cb25aa9 --- /dev/null +++ b/ML/aufgaben/a1/decisiontree_iris.py @@ -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") diff --git a/ML/aufgaben/a1/tree.png b/ML/aufgaben/a1/tree.png new file mode 100644 index 0000000..98bda30 Binary files /dev/null and b/ML/aufgaben/a1/tree.png differ