-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
531881f
commit 4dc73a7
Showing
7 changed files
with
464 additions
and
27 deletions.
There are no files selected for viewing
Binary file added
BIN
+74.9 KB
_images/ffee57543fcb9ba0788136ffb2b5969e658f2102b6011e1c4a014807f2f843b3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
Traceback (most recent call last): | ||
File "/home/runner/.local/lib/python3.10/site-packages/jupyter_cache/executors/utils.py", line 58, in single_nb_execution | ||
executenb( | ||
File "/home/runner/.local/lib/python3.10/site-packages/nbclient/client.py", line 1305, in execute | ||
return NotebookClient(nb=nb, resources=resources, km=km, **kwargs).execute() | ||
File "/home/runner/.local/lib/python3.10/site-packages/jupyter_core/utils/__init__.py", line 172, in wrapped | ||
return loop.run_until_complete(inner) | ||
File "/usr/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete | ||
return future.result() | ||
File "/home/runner/.local/lib/python3.10/site-packages/nbclient/client.py", line 705, in async_execute | ||
await self.async_execute_cell( | ||
File "/home/runner/.local/lib/python3.10/site-packages/nbclient/client.py", line 1058, in async_execute_cell | ||
await self._check_raise_for_error(cell, cell_index, exec_reply) | ||
File "/home/runner/.local/lib/python3.10/site-packages/nbclient/client.py", line 914, in _check_raise_for_error | ||
raise CellExecutionError.from_cell_and_msg(cell, exec_reply_content) | ||
nbclient.exceptions.CellExecutionError: An error occurred while executing the following cell: | ||
------------------ | ||
from sklearn.ensemble import RandomForestClassifier | ||
from sklearn.datasets import load_iris | ||
from sklearn.model_selection import train_test_split | ||
from sklearn.tree import export_graphviz | ||
import pydotplus | ||
import matplotlib.pyplot as plt | ||
|
||
# 加载数据 | ||
data = load_iris() | ||
X = data.data | ||
y = data.target | ||
|
||
# 划分训练集和测试集 | ||
X_train, X_test, y_train, _ = train_test_split(X, y, test_size=0.2, random_state=42) | ||
|
||
# 创建随机森林分类器 | ||
model = RandomForestClassifier() | ||
|
||
# 训练模型 | ||
model.fit(X_train, y_train) | ||
|
||
# 可视化决策树(第一棵树) | ||
estimator = model.estimators_[0] | ||
dot_data = export_graphviz(estimator, out_file=None, feature_names=data.feature_names, | ||
class_names=data.target_names, filled=True, rounded=True) | ||
graph = pydotplus.graph_from_dot_data(dot_data) | ||
|
||
# 保存为图像文件 | ||
graph.write_png('random_forest_tree.png') | ||
|
||
# 使用matplotlib显示图像 | ||
plt.figure(figsize=(10, 10)) | ||
plt.imshow(plt.imread('random_forest_tree.png')) | ||
plt.axis('off') | ||
plt.show() | ||
------------------ | ||
|
||
|
||
[0;31m---------------------------------------------------------------------------[0m | ||
[0;31mModuleNotFoundError[0m Traceback (most recent call last) | ||
Cell [0;32mIn[3], line 5[0m | ||
[1;32m 3[0m [38;5;28;01mfrom[39;00m [38;5;21;01msklearn[39;00m[38;5;21;01m.[39;00m[38;5;21;01mmodel_selection[39;00m [38;5;28;01mimport[39;00m train_test_split | ||
[1;32m 4[0m [38;5;28;01mfrom[39;00m [38;5;21;01msklearn[39;00m[38;5;21;01m.[39;00m[38;5;21;01mtree[39;00m [38;5;28;01mimport[39;00m export_graphviz | ||
[0;32m----> 5[0m [38;5;28;01mimport[39;00m [38;5;21;01mpydotplus[39;00m | ||
[1;32m 6[0m [38;5;28;01mimport[39;00m [38;5;21;01mmatplotlib[39;00m[38;5;21;01m.[39;00m[38;5;21;01mpyplot[39;00m [38;5;28;01mas[39;00m [38;5;21;01mplt[39;00m | ||
[1;32m 8[0m [38;5;66;03m# 加载数据[39;00m | ||
|
||
[0;31mModuleNotFoundError[0m: No module named 'pydotplus' | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.