Skip to main content
Version: v1.4.1

Regression - Concrete Compressive Strength Prediction

Predicting concrete compressive strength from mixture components using materials science data.

Dataset Source: UCI ML Repository - Concrete Compressive Strength Problem Type: Regression Target Variable: Concrete compressive strength (MPa) Use Case: Construction engineering, quality control, materials optimization

Package Imports

1!pip install xplainable
2!pip install xplainable-client
1import pandas as pd
2import xplainable as xp
3from xplainable.core.models import XRegressor
4from xplainable.core.optimisation.genetic import XEvolutionaryNetwork
5from xplainable.core.optimisation.layers import Evolve, Tighten
6from xplainable_preprocessing import PipelineSpec, StepSpec, compile_spec
7from sklearn.model_selection import train_test_split
8import requests
9import json
10
11# Additional imports specific to this example
12import numpy as np
13import matplotlib.pyplot as plt
14from ucimlrepo import fetch_ucirepo
15
16from xplainable_client.client.client import XplainableClient
17from xplainable_client.client.base import XplainableAPIError

Xplainable Cloud Setup

1# Initialize Xplainable Cloud client
2client = XplainableClient(
3 api_key="", #Create api key in xplainable cloud - https://platform.xplainable.io/
4 hostname="https://platform.xplainable.io"
5)
Out:

<Response [200]>

Data Loading and Exploration

Load the Concrete Compressive Strength dataset from UCI ML Repository.

1# Load dataset using ucimlrepo
2try:
3 # Fetch dataset
4 concrete = fetch_ucirepo(id=165)
5
6 # Data (as pandas dataframes)
7 X = concrete.data.features
8 y = concrete.data.targets
9
10 # Combine features and target
11 df = pd.concat([X, y], axis=1)
12
13 # Display basic information
14 print(f"Dataset shape: {df.shape}")
15 print(f"
16Features: {list(X.columns)}")
17 print(f"
18Target variable statistics:")
19 print(y.describe())
20
21 df.head()
22
23except Exception as e:
24 print(f"Error loading dataset: {e}")
25 print("Install ucimlrepo: pip install ucimlrepo")
Out:

Error loading dataset: Error connecting to server

Install ucimlrepo: pip install ucimlrepo

1. Data Preprocessing

Preprocess the concrete mixture data for optimal model performance.

1# Define preprocessing pipeline using PipelineSpec
2# Note: The Concrete Compressive Strength dataset is typically clean with no missing values.
3# This minimal pipeline ensures consistent preprocessing structure.
4spec = PipelineSpec(steps=[
5 StepSpec(
6 transformer="FillMissingTransformer",
7 params={"strategy": "median"}
8 )
9])
10
11pipeline = compile_spec(spec)
12df = pipeline.fit_transform(df)
13
14print(f"Processed dataset shape: {df.shape}")
15print(f"Missing values: {df.isnull().sum().sum()}")
16df.head()

Preprocessor Persistence

Save the preprocessing pipeline spec to Xplainable Cloud for reproducibility.

1# Persist the preprocessor to Xplainable Cloud
2# Uncomment to save preprocessor
3# try:
4# preprocessor_id = client.preprocessing.create_preprocessor(
5# spec=spec,
6# name="Concrete Compressive Strength Preprocessor",
7# description="Fills missing values with median for concrete mixture data"
8# )
9# print(f"Preprocessor created with ID: {preprocessor_id}")
10# except XplainableAPIError as e:
11# print(f"Error creating preprocessor: {e}")

Create Train/Test Split

1df.columns
1# Assuming the target column is the last one
2target_col = "Concrete compressive strength"
3X, y = df.drop(columns=[target_col]), df[target_col]
4
5X_train, X_test, y_train, y_test = train_test_split(
6 X, y, test_size=0.2, random_state=42
7)
8
9print(f"Training set: {X_train.shape[0]} samples")
10print(f"Test set: {X_test.shape[0]} samples")
1model = XRegressor(ignore_nan=False)
1model.fit(X_train, y_train)

2. Model Optimization

Optimize hyperparameters for concrete strength prediction.

1network = XEvolutionaryNetwork(model)
2
3# Add the layers
4# Start with an initial Tighten layer
5network.add_layer(
6 Tighten(
7 iterations=100,
8 learning_rate=0.1,
9 early_stopping=20
10 )
11 )
12
13# Add an Evolve layer with a high severity
14network.add_layer(
15 Evolve(
16 mutations=100,
17 generations=50,
18 max_severity=0.5,
19 max_leaves=20,
20 early_stopping=20
21 )
22 )
23
24# Add another Evolve layer with a lower severity and reach
25network.add_layer(
26 Evolve(
27 mutations=100,
28 generations=50,
29 max_severity=0.3,
30 max_leaves=15,
31 early_stopping=20
32 )
33 )
34
35# Add a final Tighten layer with a low learning rate
36network.add_layer(
37 Tighten(
38 iterations=100,
39 learning_rate=0.025,
40 early_stopping=20
41 )
42 )
43
44# Fit the network (before or after adding layers)
45network.fit(X_train, y_train)
46
47# Run the network
48network.optimise()

4. Model Interpretability and Explainability

Understand which concrete mixture components most influence compressive strength.

1model.explain()

5. Model Persistence (Optional)

Save the model to Xplainable Cloud.

1# Uncomment to save model to Xplainable Cloud
2# try:
3# model_id, version_id = client.models.create_model(
4# model=model,
5# model_name="Concrete Compressive Strength Model",
6# model_description="Predicting concrete strength from mixture components",
7# x=X_train,
8# y=y_train
9# )
10# except XplainableAPIError as e:
11# print(f"Error creating model: {e}")

6. Model Deployment (Optional)

Deploy the model for real-time strength predictions.

1# Uncomment to deploy model
2# try:
3# deployment_response = client.deployments.deploy(
4# model_version_id=version_id
5# )
6# deployment_id = deployment_response.deployment_id
7# except XplainableAPIError as e:
8# print(f"Error deploying model: {e}")
9#
10# # Activate deployment
11# try:
12# client.deployments.activate_deployment(deployment_id=deployment_id)
13# except XplainableAPIError as e:
14# print(f"Error activating deployment: {e}")
15#
16# # Generate deploy key for inference
17# try:
18# deploy_key = client.deployments.generate_deploy_key(
19# deployment_id=deployment_id,
20# name="Concrete Strength Deployment Key"
21# )
22# print(f"Deploy key created: {str(deploy_key)}")
23# except XplainableAPIError as e:
24# print(f"Error generating deploy key: {e}")
25#
26# # Generate example deployment payload
27# try:
28# example_payload = client.deployments.generate_example_deployment_payload(
29# model_version_id=version_id
30# )
31# print("Example payload:", example_payload)
32# except XplainableAPIError as e:
33# print(f"Error generating example payload: {e}")

7. Model Testing

Evaluate model performance on concrete strength predictions.

1# Evaluate on test set
2test_predictions = model.predict(X_test)
3test_performance = model.evaluate(X_test, y_test)
4
5print("Test Set Performance:")
6for metric, value in test_performance.items():
7 print(f"{metric}: {value:.4f}")
8
9# Plot predictions vs actual
10plt.figure(figsize=(10, 6))
11plt.scatter(y_test, test_predictions, alpha=0.6)
12plt.plot([y_test.min(), y_test.max()], [y_test.min(), y_test.max()], 'r--', lw=2)
13plt.xlabel('Actual Compressive Strength (MPa)')
14plt.ylabel('Predicted Compressive Strength (MPa)')
15plt.title('Concrete Compressive Strength: Predictions vs Actual')
16plt.grid(True, alpha=0.3)
17plt.show()
18
19# Feature importance analysis
20print("
21Top features influencing concrete strength:")
22print("- Cement content typically shows high importance")
23print("- Age of concrete is usually a strong predictor")
24print("- Water-to-cement ratio affects strength significantly")