.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/regression_tree.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_regression_tree.py: =============================================================== Fuzzy Regression Tree example =============================================================== Example plot for :class:`FuzzyDecisionTreeRegressor` generated on synthetic data. .. GENERATED FROM PYTHON SOURCE LINES 8-71 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from sklearn.metrics import mean_squared_error, r2_score from sklearn.model_selection import train_test_split from fuzzytree._classes import FuzzyDecisionTreeRegressor def generate_synthetic_data(n_samples=500, noise=0.1): """ Generate synthetic regression data: y = sin(x) + noise """ X = np.linspace(0, 10, n_samples).reshape(-1, 1) y = np.sin(X).ravel() + noise * np.random.randn(n_samples) return X, y def test_fuzzy_regression_tree(): # Generate synthetic data X, y = generate_synthetic_data() # Split data into training and testing sets X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=42 ) # Initialize the fuzzy regression tree regressor = FuzzyDecisionTreeRegressor( fuzziness=0.8, criterion="variance", # Assuming 'variance' is implemented for regression max_depth=5, min_membership_split=0.02, min_membership_leaf=0.01, min_impurity_decrease=0.001, ) # Fit the model regressor.fit(X_train, y_train) # Predict on the test set y_pred = regressor.predict(X_test) # Evaluate the model mse = mean_squared_error(y_test, y_pred) r2 = r2_score(y_test, y_pred) # Print the results print("Fuzzy Regression Tree Results:") print(f"Mean Squared Error (MSE): {mse:.4f}") print(f"R-squared (R2): {r2:.4f}") plt.scatter(X_test, y_test, color="blue", label="True Values") plt.scatter(X_test, y_pred, color="red", label="Predictions") plt.legend() plt.xlabel("X") plt.ylabel("y") plt.title("Fuzzy Regression Tree Performance") plt.show() if __name__ == "__main__": test_fuzzy_regression_tree() .. _sphx_glr_download_auto_examples_regression_tree.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: regression_tree.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: regression_tree.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: regression_tree.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_