Python
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score

# 1. Generate Synthetic Data
np.random.seed(0)
n_samples = 20

# Features: Cutting Speed, Feed Rate, Depth of Cut
cutting_speed = np.random.uniform(50, 200, n_samples)
feed_rate = np.random.uniform(0.1, 1.0, n_samples)
depth_of_cut = np.random.uniform(0.5, 5.0, n_samples)

# Target: Surface Roughness (Ra) - Synthetic relationship
surface_roughness = 0.2 * cutting_speed + 1.5 * feed_rate + 0.8 * depth_of_cut + np.random.normal(0, 0.5, n_samples)

# Create DataFrame
data = pd.DataFrame({
    'Cutting Speed': cutting_speed,
    'Feed Rate': feed_rate,
    'Depth of Cut': depth_of_cut,
    'Surface Roughness': surface_roughness
})

print("Synthetic Data:\n", data.head())

# 2. Data Visualization
plt.figure(figsize=(12, 8))

# Plot Cutting Speed vs Surface Roughness
plt.subplot(2, 2, 1)
plt.scatter(data['Cutting Speed'], data['Surface Roughness'], color='blue')
plt.title('Cutting Speed vs Surface Roughness')
plt.xlabel('Cutting Speed')
plt.ylabel('Surface Roughness')

# Plot Feed Rate vs Surface Roughness
plt.subplot(2, 2, 2)
plt.scatter(data['Feed Rate'], data['Surface Roughness'], color='green')
plt.title('Feed Rate vs Surface Roughness')
plt.xlabel('Feed Rate')
plt.ylabel('Surface Roughness')

# Plot Depth of Cut vs Surface Roughness
plt.subplot(2, 2, 3)
plt.scatter(data['Depth of Cut'], data['Surface Roughness'], color='red')
plt.title('Depth of Cut vs Surface Roughness')
plt.xlabel('Depth of Cut')
plt.ylabel('Surface Roughness')

plt.tight_layout()
plt.show()

# 3. Train-Test Split
X = data[['Cutting Speed', 'Feed Rate', 'Depth of Cut']]
y = data['Surface Roughness']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)

# 4. Train Model
model = LinearRegression()
model.fit(X_train, y_train)

# 5. Make Predictions
y_pred = model.predict(X_test)

# 6. Evaluate Model
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)

print(f"Mean Squared Error: {mse:.2f}")
print(f"R-squared: {r2:.2f}")

# Plot predictions vs actual
plt.figure(figsize=(6, 6))
plt.scatter(y_test, y_pred, color='purple')
plt.plot([y_test.min(), y_test.max()], [y_test.min(), y_test.max()], 'k--', lw=2)
plt.title('Actual vs Predicted Surface Roughness')
plt.xlabel('Actual Surface Roughness')
plt.ylabel('Predicted Surface Roughness')
plt.show()
Synthetic Data:
    Cutting Speed  Feed Rate  Depth of Cut  Surface Roughness
0     132.322026   0.980757      2.117786          29.182035
1     157.278405   0.819243      2.466644          34.851311
2     140.414506   0.515331      3.639340          31.511968
3     131.732477   0.802476      0.771015          27.576705
4     113.548220   0.206447      3.500450          25.805584
Mean Squared Error: 0.10
R-squared: 0.97