Introduction
Germany's workforce shortage has prompted a significant push toward AI adoption in various industries, particularly in manufacturing and construction. This tutorial will guide you through building a simple AI-powered workforce optimization system using Python and machine learning. This system will help predict worker productivity and identify optimal staffing levels based on historical data.
Prerequisites
- Python 3.7 or higher installed
- Basic understanding of machine learning concepts
- Knowledge of pandas and scikit-learn libraries
- Working knowledge of data analysis and visualization
Step-by-step Instructions
Step 1: Set Up Your Environment
Install Required Libraries
We'll need several Python libraries to build our AI system. Run the following command in your terminal:
pip install pandas scikit-learn matplotlib seaborn numpy
Why this step? These libraries provide the essential tools for data manipulation, machine learning algorithms, and data visualization that we'll use throughout this project.
Step 2: Create Sample Data
Generate Worker Productivity Dataset
First, let's create a synthetic dataset that represents worker productivity data:
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
# Create synthetic data
np.random.seed(42)
# Generate dates for 12 months
dates = pd.date_range(start='2023-01-01', end='2023-12-31', freq='D')
# Create worker data
workers = []
for date in dates:
for worker_id in range(1, 21): # 20 workers
# Base productivity with some randomness
base_productivity = np.random.normal(80, 15)
# Add seasonal variation
seasonal_factor = 1 + 0.1 * np.sin(2 * np.pi * date.timetuple().tm_yday / 365)
productivity = max(0, base_productivity * seasonal_factor)
workers.append({
'date': date,
'worker_id': worker_id,
'productivity': productivity,
'shift_hours': np.random.normal(8, 1),
'overtime': np.random.choice([0, 1], p=[0.8, 0.2]),
'training_hours': np.random.exponential(2),
'team_size': np.random.choice([3, 4, 5, 6], p=[0.1, 0.3, 0.4, 0.2])
})
# Create DataFrame
df = pd.DataFrame(workers)
df.to_csv('worker_productivity_data.csv', index=False)
print('Sample data created and saved to worker_productivity_data.csv')
Why this step? We're creating realistic synthetic data that mimics real-world worker productivity metrics, which will be essential for training our AI model to make predictions.
Step 3: Load and Explore Data
Load the Dataset
Now, let's load and examine our dataset:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# Load the dataset
df = pd.read_csv('worker_productivity_data.csv')
# Display basic information
print('Dataset shape:', df.shape)
print('\nFirst few rows:')
print(df.head())
print('\nDataset info:')
print(df.info())
print('\nBasic statistics:')
print(df.describe())
Why this step? Understanding our data structure and basic statistics is crucial before building any machine learning model. This helps us identify potential issues and understand the relationships between variables.
Step 4: Data Preprocessing
Prepare Data for Machine Learning
Let's clean and prepare our data for model training:
# Convert date to datetime if not already
df['date'] = pd.to_datetime(df['date'])
# Feature engineering
# Create additional features
df['month'] = df['date'].dt.month
df['day_of_week'] = df['date'].dt.dayofweek
# Create a productivity category (low, medium, high)
bin_edges = [0, 60, 85, 100]
bin_labels = ['Low', 'Medium', 'High']
# Create a new column for productivity category
df['productivity_category'] = pd.cut(df['productivity'], bins=bin_edges, labels=bin_labels, include_lowest=True)
# Display the distribution
print('Productivity category distribution:')
print(df['productivity_category'].value_counts())
# Prepare features and target
features = ['shift_hours', 'overtime', 'training_hours', 'team_size', 'month', 'day_of_week']
X = df[features]
# For regression model, use productivity as target
y = df['productivity']
print('\nFeatures shape:', X.shape)
print('Target shape:', y.shape)
Why this step? Feature engineering helps our model better understand patterns in the data. We're creating meaningful features that can influence productivity predictions, such as day of week and month.
Step 5: Train Machine Learning Model
Build and Train a Regression Model
Let's create a machine learning model to predict worker productivity:
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.preprocessing import StandardScaler
# Split the data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Scale the features
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# Train Random Forest model
model = RandomForestRegressor(n_estimators=100, random_state=42)
model.fit(X_train_scaled, y_train)
# Make predictions
y_pred = model.predict(X_test_scaled)
# Evaluate the model
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
print(f'Model Performance:')
print(f'Mean Squared Error: {mse:.2f}')
print(f'R² Score: {r2:.2f}')
Why this step? We're using a Random Forest regressor, which is excellent for predicting continuous values like productivity scores. The model will help us understand which factors most influence worker productivity.
Step 6: Visualize Results
Create Productivity Analysis Charts
Let's visualize our model's performance and key insights:
# Create visualizations
fig, axes = plt.subplots(2, 2, figsize=(15, 10))
# Actual vs Predicted
axes[0, 0].scatter(y_test, y_pred, alpha=0.5)
axes[0, 0].plot([y_test.min(), y_test.max()], [y_test.min(), y_test.max()], 'r--', lw=2)
axes[0, 0].set_xlabel('Actual Productivity')
axes[0, 0].set_ylabel('Predicted Productivity')
axes[0, 0].set_title('Actual vs Predicted Productivity')
# Feature importance
feature_importance = pd.DataFrame({
'feature': features,
'importance': model.feature_importances_
})
feature_importance = feature_importance.sort_values('importance', ascending=False)
axes[0, 1].barh(feature_importance['feature'], feature_importance['importance'])
axes[0, 1].set_xlabel('Importance')
axes[0, 1].set_title('Feature Importance')
# Productivity over time
monthly_avg = df.groupby('date')['productivity'].mean()
axes[1, 0].plot(monthly_avg.index, monthly_avg.values)
axes[1, 0].set_xlabel('Date')
axes[1, 0].set_ylabel('Average Productivity')
axes[1, 0].set_title('Productivity Over Time')
axes[1, 0].tick_params(axis='x', rotation=45)
# Productivity distribution
axes[1, 1].hist(df['productivity'], bins=30, alpha=0.7)
axes[1, 1].set_xlabel('Productivity')
axes[1, 1].set_ylabel('Frequency')
axes[1, 1].set_title('Productivity Distribution')
plt.tight_layout()
plt.show()
Why this step? Visualizations help us understand our model's performance and identify trends in worker productivity. This is crucial for making data-driven decisions about staffing and resource allocation.
Step 7: Create Staffing Recommendations
Generate AI-Driven Staffing Suggestions
Finally, let's use our model to make staffing recommendations:
# Function to recommend staffing levels
def recommend_staffing(productivity_target, current_workers, features_dict):
'''
Recommend optimal staffing levels based on productivity target
'''
# Calculate required productivity
required_productivity = productivity_target / current_workers
# Create a scenario with current conditions
scenario = np.array([[
features_dict['shift_hours'],
features_dict['overtime'],
features_dict['training_hours'],
features_dict['team_size'],
features_dict['month'],
features_dict['day_of_week']
]])
# Scale the scenario
scenario_scaled = scaler.transform(scenario)
# Predict productivity
predicted_productivity = model.predict(scenario_scaled)[0]
# Calculate recommended workers
recommended_workers = max(1, int(productivity_target / predicted_productivity))
return {
'current_workers': current_workers,
'required_productivity': required_productivity,
'predicted_productivity': predicted_productivity,
'recommended_workers': recommended_workers,
'difference': recommended_workers - current_workers
}
# Example recommendation
example_features = {
'shift_hours': 8,
'overtime': 0,
'training_hours': 2,
'team_size': 5,
'month': 6,
'day_of_week': 2
}
# Recommend staffing for 1000 productivity units with 15 current workers
recommendation = recommend_staffing(1000, 15, example_features)
print('Staffing Recommendation:')
for key, value in recommendation.items():
print(f'{key}: {value}')
Why this step? This final step demonstrates how our AI system can be practically applied to solve real-world problems like workforce optimization, which is exactly what Germany's AI initiatives aim to address.
Summary
In this tutorial, we've built an AI-powered workforce optimization system that can predict worker productivity and recommend optimal staffing levels. We've covered data creation, preprocessing, machine learning model training, visualization, and practical application. This system mirrors the kind of AI solutions being deployed in Germany to address worker shortages through intelligent resource management.
The key insights from this project include understanding how different factors like training hours, team size, and shift patterns influence productivity, and how AI can help optimize workforce allocation in response to labor shortages.


