Build a Regression MLP with Keras: Predict California Housing Prices
Learn how to load the California housing dataset, preprocess features, construct a Keras sequential regression MLP, train it with SGD, evaluate performance, and make predictions, all illustrated with concise Python code snippets.
Implement a Multilayer Perceptron with Keras
We use the California housing problem as an example and employ a regression neural network for prediction. The Scikit‑Learn fetch_california_housing() function loads the dataset, which contains only numeric features and no missing values. After loading, we split the data into training, validation, and test sets and scale all features:
# Import libraries
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
# Load data
housing = fetch_california_housing()
# Split dataset into training, validation, test sets
X_train_full, X_test, y_train_full, y_test = train_test_split(
housing.data, housing.target, random_state=42)
X_train, X_valid, y_train, y_valid = train_test_split(
X_train_full, y_train_full, random_state=42)
# Scale features
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_valid = scaler.transform(X_valid)
X_test = scaler.transform(X_test)Using the Sequential API, we build, train, and evaluate a regression MLP, which is similar to the classification MLP except the output layer has a single neuron (no activation) and uses mean‑squared error loss. Because the dataset is noisy, we use a hidden layer with fewer neurons to avoid over‑fitting:
np.random.seed(42)
tf.random.set_seed(42)
model = keras.models.Sequential([
keras.layers.Dense(30, activation="relu", input_shape=X_train.shape[1:]),
keras.layers.Dense(1)
])
model.compile(loss="mean_squared_error",
optimizer=keras.optimizers.SGD(lr=1e-3))
history = model.fit(X_train, y_train, epochs=20,
validation_data=(X_valid, y_valid))Finally, we evaluate the model on the test set and make predictions on new samples:
mse_test = model.evaluate(X_test, y_test)
X_new = X_test[:3]
y_pred = model.predict(X_new)Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Model Perspective
Insights, knowledge, and enjoyment from a mathematical modeling researcher and educator. Hosted by Haihua Wang, a modeling instructor and author of "Clever Use of Chat for Mathematical Modeling", "Modeling: The Mathematics of Thinking", "Mathematical Modeling Practice: A Hands‑On Guide to Competitions", and co‑author of "Mathematical Modeling: Teaching Design and Cases".
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
