import numpy as np
import matplotlib.pyplot as plt
from sklearn.ensemble import RandomForestRegressor
from sklearn.multioutput import MultiOutputRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
np.random.seed(42)
X = np.random.rand(100, 1) * 10
y1 = 2 * X.squeeze() + np.random.randn(100)
y2 = 3 * X.squeeze() + np.random.randn(100)
y = np.column_stack((y1, y2))
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42)
model = MultiOutputRegressor(
RandomForestRegressor(n_estimators=100, random_state=42))
model.fit(X_train, y_train)
predictions = model.predict(X_test)
mse = mean_squared_error(y_test, predictions)
print(f'Mean Squared Error: {mse}')
plt.figure(figsize=(10, 6))
plt.subplot(2, 1, 1)
plt.scatter(X_test, y_test[:, 0], label='True y1')
plt.scatter(X_test, predictions[:, 0], label='Predicted y1', marker='^')
plt.title('Output Variable 1')
plt.legend()
plt.subplot(2, 1, 2)
plt.scatter(X_test, y_test[:, 1], label='True y2')
plt.scatter(X_test, predictions[:, 1], label='Predicted y2', marker='^')
plt.title('Output Variable 2')
plt.legend()
plt.tight_layout()
plt.show()