mirror of
https://github.com/ArthurDanjou/handson-ml3.git
synced 2026-01-14 12:14:36 +01:00
Large change: replace os.path with pathlib, move to Python 3.7
This commit is contained in:
@@ -39,7 +39,7 @@
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"First, let's import a few common modules, ensure MatplotLib plots figures inline and prepare a function to save the figures. We also check that Python 3.5 or later is installed (although Python 2.x may work, it is deprecated so we strongly recommend you use Python 3 instead), as well as Scikit-Learn ≥0.20."
|
||||
"First, let's import a few common modules, ensure MatplotLib plots figures inline and prepare a function to save the figures."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -48,17 +48,17 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Python ≥3.5 is required\n",
|
||||
"# Python ≥3.7 is required\n",
|
||||
"import sys\n",
|
||||
"assert sys.version_info >= (3, 5)\n",
|
||||
"assert sys.version_info >= (3, 7)\n",
|
||||
"\n",
|
||||
"# Scikit-Learn ≥0.20 is required\n",
|
||||
"# Scikit-Learn ≥1.0 is required\n",
|
||||
"import sklearn\n",
|
||||
"assert sklearn.__version__ >= \"0.20\"\n",
|
||||
"assert sklearn.__version__ >= \"1.0\"\n",
|
||||
"\n",
|
||||
"# Common imports\n",
|
||||
"import numpy as np\n",
|
||||
"import os\n",
|
||||
"from pathlib import Path\n",
|
||||
"\n",
|
||||
"# to make this notebook's output stable across runs\n",
|
||||
"np.random.seed(42)\n",
|
||||
@@ -72,14 +72,11 @@
|
||||
"mpl.rc('ytick', labelsize=12)\n",
|
||||
"\n",
|
||||
"# Where to save the figures\n",
|
||||
"PROJECT_ROOT_DIR = \".\"\n",
|
||||
"CHAPTER_ID = \"decision_trees\"\n",
|
||||
"IMAGES_PATH = os.path.join(PROJECT_ROOT_DIR, \"images\", CHAPTER_ID)\n",
|
||||
"os.makedirs(IMAGES_PATH, exist_ok=True)\n",
|
||||
"IMAGES_PATH = Path() / \"images\" / \"decision_trees\"\n",
|
||||
"IMAGES_PATH.mkdir(parents=True, exist_ok=True)\n",
|
||||
"\n",
|
||||
"def save_fig(fig_id, tight_layout=True, fig_extension=\"png\", resolution=300):\n",
|
||||
" path = os.path.join(IMAGES_PATH, fig_id + \".\" + fig_extension)\n",
|
||||
" print(\"Saving figure\", fig_id)\n",
|
||||
" path = IMAGES_PATH / f\"{fig_id}.{fig_extension}\"\n",
|
||||
" if tight_layout:\n",
|
||||
" plt.tight_layout()\n",
|
||||
" plt.savefig(path, format=fig_extension, dpi=resolution)"
|
||||
@@ -127,14 +124,14 @@
|
||||
"\n",
|
||||
"export_graphviz(\n",
|
||||
" tree_clf,\n",
|
||||
" out_file=os.path.join(IMAGES_PATH, \"iris_tree.dot\"),\n",
|
||||
" out_file=IMAGES_PATH / \"iris_tree.dot\",\n",
|
||||
" feature_names=iris.feature_names[2:],\n",
|
||||
" class_names=iris.target_names,\n",
|
||||
" rounded=True,\n",
|
||||
" filled=True\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
"Source.from_file(os.path.join(IMAGES_PATH, \"iris_tree.dot\"))"
|
||||
"Source.from_file(IMAGES_PATH / \"iris_tree.dot\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -485,7 +482,7 @@
|
||||
"source": [
|
||||
"export_graphviz(\n",
|
||||
" tree_reg1,\n",
|
||||
" out_file=os.path.join(IMAGES_PATH, \"regression_tree.dot\"),\n",
|
||||
" out_file=IMAGES_PATH / \"regression_tree.dot\",\n",
|
||||
" feature_names=[\"x1\"],\n",
|
||||
" rounded=True,\n",
|
||||
" filled=True\n",
|
||||
@@ -498,7 +495,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"Source.from_file(os.path.join(IMAGES_PATH, \"regression_tree.dot\"))"
|
||||
"Source.from_file(IMAGES_PATH / \"regression_tree.dot\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -797,7 +794,7 @@
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user