import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neural_network import MLPRegressor
from sklearn.metrics import mean_squared_error, r2_score
# Sample data: house sizes (in square feet) and prices (in $1000s)
X = np.array([[500], [750], [1000], [1250], [1500], [1750], [2000], [2250], [2500], [2750], [3000]])
y = np.array([150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650])
# Split data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Normalize features
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# Define a small neural network model
model = MLPRegressor(hidden_layer_sizes=(5,), activation='relu', max_iter=1000, random_state=42)
# Train the model
model.fit(X_train_scaled, y_train)
# Predict on test data
y_pred = model.predict(X_test_scaled)
# Calculate metrics
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
print(f"Test MSE: {mse:.3f}")
print(f"Test R2 (accuracy): {r2*100:.1f}%")