Repository navigation
Expand file tree
/
Copy pathml_model.py
More file actions
226 lines (181 loc) · 6.28 KB
/
Copy pathml_model.py
File metadata and controls
226 lines (181 loc) · 6.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import OneHotEncoder
from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier
from sklearn.metrics import (
mean_absolute_error,
mean_squared_error,
r2_score,
accuracy_score,
classification_report,
confusion_matrix
)
#load data from manufacturing defect data file
df = pd.read_csv(r"C:\Users\Megan\OneDrive\Documents\Python_Practice\manufacturing_defect_data.csv")
# make sure Defect Rate is numeric
df["Defect Rate"] = (
df["Defect Rate"]
.astype(str)
.str.replace("%", "", regex=False)
.astype(float)
)
print("\n--- Dataset Loaded for Machine Learning ---")
print(df.head())
print(f"\nRows: {len(df)}")
print(f"Columns: {len(df.columns)}")
#targets
# Regression target:
# Predict exact defect rate percentage
y_reg = df["Defect Rate"]
# Classification target:
# Flag jobs with unusually high defect risk
high_defect_threshold = df["Defect Rate"].quantile(0.75)
df["High Defect Risk"] = (df["Defect Rate"] >= high_defect_threshold).astype(int)
y_clf = df["High Defect Risk"]
print(f"\nHigh defect threshold (75th percentile): {high_defect_threshold:.2f}%")
#feature selection
# Use only inputs known before or during production setup.
# Exclude leakage / outcome columns.
feature_columns = [
"Customer",
"Industry",
"Shift",
"Operator Level",
"Material",
"Department",
"Machine",
"Process Type",
"Complexity Score",
"Tool Condition",
"Rush Order",
"Machine Downtime",
"Inspection Type",
"Outside Process",
"Outside Process Type",
"Hardware",
"Hardware Type",
"Deburr",
"Setup Minutes",
"Cycle Time Seconds",
"Total Parts",
"Thread"
]
X = df[feature_columns]
categorical_columns = X.select_dtypes(include="object").columns.tolist()
numeric_columns = X.select_dtypes(exclude="object").columns.tolist()
print("\n--- Features Used ---")
print(feature_columns)
#reprocessesing
categorical_transformer = Pipeline(steps=[
("imputer", SimpleImputer(strategy="most_frequent")),
("onehot", OneHotEncoder(handle_unknown="ignore"))
])
numeric_transformer = Pipeline(steps=[
("imputer", SimpleImputer(strategy="median"))
])
preprocessor = ColumnTransformer(
transformers=[
("cat", categorical_transformer, categorical_columns),
("num", numeric_transformer, numeric_columns)
]
)
#regression model
X_train_reg, X_test_reg, y_train_reg, y_test_reg = train_test_split(
X, y_reg, test_size=0.2, random_state=42
)
regression_model = Pipeline(steps=[
("preprocessor", preprocessor),
("model", RandomForestRegressor(
n_estimators=200,
max_depth=12,
min_samples_leaf=3,
random_state=42,
n_jobs=-1
))
])
regression_model.fit(X_train_reg, y_train_reg)
y_pred_reg = regression_model.predict(X_test_reg)
mae = mean_absolute_error(y_test_reg, y_pred_reg)
rmse = mean_squared_error(y_test_reg, y_pred_reg) ** 0.5
r2 = r2_score(y_test_reg, y_pred_reg)
print("\n--- Regression Model: Predict Defect Rate ---")
print(f"MAE: {mae:.4f}")
print(f"RMSE: {rmse:.4f}")
print(f"R²: {r2:.4f}")
#classification model
X_train_clf, X_test_clf, y_train_clf, y_test_clf = train_test_split(
X, y_clf, test_size=0.2, random_state=42, stratify=y_clf
)
classification_model = Pipeline(steps=[
("preprocessor", preprocessor),
("model", RandomForestClassifier(
n_estimators=200,
max_depth=12,
min_samples_leaf=3,
class_weight="balanced",
random_state=42,
n_jobs=-1
))
])
classification_model.fit(X_train_clf, y_train_clf)
y_pred_clf = classification_model.predict(X_test_clf)
accuracy = accuracy_score(y_test_clf, y_pred_clf)
cm = confusion_matrix(y_test_clf, y_pred_clf)
print("\n--- Classification Model: Predict High Defect Risk ---")
print(f"Accuracy: {accuracy:.4f}")
print("\nClassification Report:")
print(classification_report(y_test_clf, y_pred_clf))
#feature importance
#get feature names after one-hot encoding
ohe = regression_model.named_steps["preprocessor"].named_transformers_["cat"].named_steps["onehot"]
encoded_cat_names = ohe.get_feature_names_out(categorical_columns)
all_feature_names = list(encoded_cat_names) + numeric_columns
feature_importance = pd.Series(
regression_model.named_steps["model"].feature_importances_,
index=all_feature_names
).sort_values(ascending=False)
print("\n--- Top 15 Most Important Features for Defect Rate Prediction ---")
print(feature_importance.head(15))
#save predictions
results = X_test_reg.copy()
results["Actual Defect Rate"] = y_test_reg.values
results["Predicted Defect Rate"] = y_pred_reg
results["Prediction Error"] = results["Actual Defect Rate"] - results["Predicted Defect Rate"]
results.to_csv("ml_predictions_output.csv", index=False)
print("\nSaved: ml_predictions_output.csv")
#visual feature of actual vs predicted
plt.figure(figsize=(10, 6))
plt.scatter(y_test_reg, y_pred_reg, alpha=0.6, edgecolor="black")
plt.xlabel("Actual Defect Rate (%)")
plt.ylabel("Predicted Defect Rate (%)")
plt.title("Actual vs Predicted Defect Rate")
plt.grid(True, linestyle="--", alpha=0.4)
plt.tight_layout()
plt.show()
#visual features
top_features = feature_importance.head(12).sort_values()
plt.figure(figsize=(10, 6))
top_features.plot(kind="barh", edgecolor="black")
plt.xlabel("Feature Importance")
plt.ylabel("Feature")
plt.title("Top Features Driving Defect Rate Prediction")
plt.grid(axis="x", linestyle="--", alpha=0.4)
plt.tight_layout()
plt.show()
plt.figure(figsize=(6, 5))
plt.imshow(cm, interpolation="nearest")
plt.title("Confusion Matrix - High Defect Risk")
plt.colorbar()
plt.xticks([0, 1], ["Low Risk", "High Risk"])
plt.yticks([0, 1], ["Low Risk", "High Risk"])
plt.xlabel("Predicted Label")
plt.ylabel("True Label")
for i in range(cm.shape[0]):
for j in range(cm.shape[1]):
plt.text(j, i, cm[i, j], ha="center", va="center")
plt.tight_layout()
plt.show()