chroe(a1): update excercise, use python instead of jupyter
This commit is contained in:
@@ -1259,6 +1259,13 @@
|
|||||||
" filled=True, rounded=True, \n",
|
" filled=True, rounded=True, \n",
|
||||||
" special_characters=True) "
|
" special_characters=True) "
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
@@ -1277,7 +1284,7 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.13.12"
|
"version": "3.14.4"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|||||||
@@ -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 |
Reference in New Issue
Block a user