<jupyter_start><jupyter_text>Regression Trees Plotting<jupyter_code># Generating random data
m = 200
x = np.random.rand(m, 1)
y = 4 * (x - 0.5)**2 + np.random.randn(m, 1)/10
from sklearn.tree import DecisionTreeRegressor

dt = DecisionTreeRegressor(max_depth=2, random_state=42)
dt.fit(x, y)
export_graphviz(dt, "reg_tree.dot", feature_names=["x"], rounded=True, filled=True)
Source.from_file("reg_tree.dot")
# Plotting Regression Tree

def plot_regression_tree(dt, x, y, axes=[0, 1, -0.2, 1]):
  x1 = np.linspace(axes[0], axes[1], 500).reshape(-1, 1)
  y_pred = dt.predict(x1)
  plt.axis(axes)
  plt.plot(x1, y_pred, "r-", linewidth=2)
  plt.plot(x, y, "b.")
  plt.xlabel("$x$")
  plt.ylabel("$y$")
plot_regression_tree(dt, x, y)
plt.text(0.21, 0.65, "Depth=0", fontsize=12)
plt.text(0.01, 0.2, "Depth=1", fontsize=12)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19199057714958775, 'fraction_numerical': 0.05535924617196702, 'mean_word_length': 5.640625, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '638135', 'license': 'MIT', 'n_tokens_mistral': 409, 'n_tokens_neox': 375, 'n_words': 71, 'path': 'chapter_notebooks/handson-ml-chap6.ipynb', 'repo_name': 'vaibhavkumar11/hands-on-ml', 'size': '849'}