<jupyter_start><jupyter_text>Decision Tree Regressor  Python version (sklearn)http://scikit-learn.org/stable/auto_examples/tree/plot_iris.htmlexample-tree-plot-iris-py<jupyter_code>%matplotlib inline
import pylab
pylab.rcParams['figure.figsize'] = (16.0, 8.0)
import numpy as np
from sklearn.tree import DecisionTreeRegressor
import matplotlib.pyplot as plt

# Create a random dataset
rng = np.random.RandomState(1234)
X = np.sort(5 * rng.rand(80, 1), axis=0)
y = np.sin(X).ravel()
y[::5] += 3 * (0.5 - rng.rand(16))

# Fit regression model
regr_1 = DecisionTreeRegressor(max_depth=2)
regr_2 = DecisionTreeRegressor(max_depth=5)
regr_1.fit(X, y)
regr_2.fit(X, y)

# Predict
X_test = np.arange(0.0, 5.0, 0.01)[:, np.newaxis]
y_1 = regr_1.predict(X_test)
y_2 = regr_2.predict(X_test)

# Plot the results
plt.figure()
plt.scatter(X, y, c="k", label="data")
plt.plot(X_test, y_1, c="g", label="max_depth=2", linewidth=2)
plt.plot(X_test, y_2, c="r", label="max_depth=5", linewidth=2)
plt.xlabel("data")
plt.ylabel("target")
plt.title("Decision Tree Regression")
plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>Crystal version (crystal-learn) ```ruby require "random"require "../math"require "../random"require "../array"require "../trees"require "csv"x = Random.sequence(80).map {|x| x * 5}x.sort!y = Math.sin(x)seq = Random.sequence(16).map{|x| 3 * (0.5 - x)}y.each_with_index do |e, i|  y[i] += seq[i/5] if i%5 == 0endregr2 = ML::Classifiers::DecisionTreeRegressor.new(max_depth: 2)regr5 = ML::Classifiers::DecisionTreeRegressor.new(max_depth: 5)x = x.map {|xi| [xi]}regr2.fit(x, y)regr5.fit(x, y) Predictx_test = ML.arange(0.0, 5.0, step: 0.01).map {|x| [x]}y_pred2 = regr2.predict(x_test).map {|x| x.round(2)}y_pred5 = regr5.predict(x_test).map {|x| x.round(2)}puts "regresor max_depth: 2"regr2.show_tree(column_names: ["x", "y"]) puts "regresor max_depth: 5" regr5.show_tree(column_names: ["x", "y"])f = File.open("regressor.csv", mode: "w")result = CSV.build(f) do |csv|  x.zip(y).each do |x_i, y_i|    csv.row x_i[0], y_i, "true"  end  x_test.zip(y_pred2).each do |x_i, y_i|    csv.row x_i[0], y_i, "pred_2"  end  x_test.zip(y_pred5).each do |x_i, y_i|    csv.row x_i[0], y_i, "pred_5"  endendf.close()```<jupyter_code>import pandas as pd
data = pd.read_csv('regressor.csv', names = ["x", "y", "kind"])
data_true = data[data.kind == "true"]
data_pred2 = data[data.kind == "pred_2"]
data_pred5 = data[data.kind == "pred_5"]


X = np.array(data_true.x)
y = np.array(data_true.y)

X_test = np.array(data_pred.x)
y_pred2 = np.array(data_pred2.y)
y_pred5 = np.array(data_pred5.y)


plt.figure()
plt.scatter(X, y, c="k", label="data")
plt.plot(X_test, y_pred2, c="g", label="max_depth=2", linewidth=2)
plt.plot(X_test, y_pred5, c="r", label="max_depth=5", linewidth=2)
plt.xlabel("data")
plt.ylabel("target")
plt.title("Decision Tree Regression (Crystal)")
plt.legend()
plt.show()
from sklearn import tree
tree.export_graphviz(regr_1, out_file='tree.dot')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19326891036321225, 'fraction_numerical': 0.032989003665444855, 'mean_word_length': 5.885321100917431, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '508199', 'license': 'MIT', 'n_tokens_mistral': 1377, 'n_tokens_neox': 1304, 'n_words': 238, 'path': 'examples/replicate-sklearn/sklearn-dtregressor-comparison.ipynb', 'repo_name': 'pbrusco/crystal-learn', 'size': '3001'}