forked from ziedtuihri/MLOps
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctionalities.py
More file actions
165 lines (144 loc) · 5.52 KB
/
functionalities.py
File metadata and controls
165 lines (144 loc) · 5.52 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
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import mean_squared_error, silhouette_score
from sklearn.cluster import KMeans, AgglomerativeClustering, DBSCAN
from sklearn.mixture import GaussianMixture
def load_data(path):
df = pd.read_csv(path)
df["date"] = pd.to_datetime(df["date"])
df["wip"].fillna(value=0, inplace=True)
return df
def preprocess_data(df):
df = df.sort_values(by="date").reset_index(drop=True)
df["department"] = df.apply(
lambda row: (
"finishing"
if row["wip"] == 0
else (
"sewing"
if pd.isna(row["department"]) and row["wip"] > 0
else row["department"]
)
),
axis=1,
)
df = df.dropna(subset=["date"])
for i in range(1, len(df) - 1):
if pd.isna(df.loc[i, "quarter"]):
previous_quarter = df.loc[i - 1, "quarter"]
next_quarter = df.loc[i + 1, "quarter"]
previous_date = df.loc[i - 1, "date"]
next_date = df.loc[i + 1, "date"]
if (
previous_quarter == next_quarter
and previous_date.month == next_date.month
and previous_date.day == next_date.day
):
df.loc[i, "quarter"] = previous_quarter
df = df.dropna(subset=["quarter"])
for i in range(1, len(df) - 1):
if pd.isna(df.iloc[i]["day"]):
previous_date = df.iloc[i - 1]["date"]
next_date = df.iloc[i + 1]["date"]
if previous_date.date() == next_date.date():
df.iloc[i, df.columns.get_loc("day")] = df.iloc[i - 1]["day"]
df = df.dropna(subset=["day"])
def fill_idle_columns(row):
if pd.isna(row["idle_time"]) and pd.isna(row["idle_men"]):
row["idle_time"] = 0
row["idle_men"] = 0
if pd.isna(row["idle_time"]):
if row["idle_men"] == 0:
row["idle_time"] = 0
if pd.isna(row["idle_men"]):
if row["idle_time"] == 0:
row["idle_men"] = 0
return row
df = df.apply(fill_idle_columns, axis=1)
df["incentive"] = df["incentive"].fillna(0)
df["over_time"] = df["over_time"].fillna(0)
df = df.dropna(subset=["no_of_workers", "team"])
def set_no_of_style_change(row):
if pd.isna(row["no_of_style_change"]) and pd.notna(row["smv"]):
if row["smv"] == 11.41:
return 2
elif row["smv"] == 30.1:
return 1
return row["no_of_style_change"]
df["no_of_style_change"] = df.apply(set_no_of_style_change, axis=1)
df = df.dropna(
subset=[
"no_of_style_change",
"smv",
"actual_productivity",
"targeted_productivity",
]
)
df["no_of_workers"] = df["no_of_workers"].apply(lambda x: int(x))
df["actual_productivity"] = pd.to_numeric(
df["actual_productivity"], errors="coerce"
)
df["quarter"] = df["quarter"].astype(str).str.replace("Quarter", "")
df["quarter"] = pd.to_numeric(df["quarter"], errors="coerce")
df["department"] = df["department"].str.replace("sweing", "sewing")
df["department"] = df["department"].str.replace("finishing ", "finishing")
# Encode categorical columns
df["department"] = df["department"].replace({"sewing": 0, "finishing": 1})
day_map = {
"Monday": 0,
"Tuesday": 1,
"Wednesday": 2,
"Thursday": 3,
"Saturday": 4,
"Sunday": 5,
}
df["day"] = df["day"].replace(day_map)
# Remove any remaining non-numeric columns except the target
for col in df.columns:
if df[col].dtype == "object" and col != "actual_productivity":
print(f"Warning: Dropping non-numeric column: {col}")
df = df.drop(columns=[col])
df = df.drop(columns=["date"], errors="ignore")
return df
def train_gradient_boosting(X_train, y_train, X_test, y_test):
gbr = GradientBoostingRegressor(
n_estimators=100, learning_rate=0.01, max_depth=5, random_state=0
)
gbr.fit(X_train, y_train)
y_pred = gbr.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
rmse = np.sqrt(mse)
return rmse
def clustering_kmeans(X_scaled, n_clusters=2):
kmeans = KMeans(n_clusters=n_clusters, random_state=42)
labels = kmeans.fit_predict(X_scaled)
sil = silhouette_score(X_scaled, labels)
return labels, sil
def clustering_cah(X_scaled, n_clusters=2):
clusterer = AgglomerativeClustering(
n_clusters=n_clusters, metric="euclidean", linkage="ward"
)
labels = clusterer.fit_predict(X_scaled)
sil = silhouette_score(X_scaled, labels)
return labels, sil
def clustering_dbscan(X_scaled, eps=0.7, min_samples=10):
dbscan = DBSCAN(eps=eps, min_samples=min_samples)
labels = dbscan.fit_predict(X_scaled)
if len(set(labels)) > 1:
sil = silhouette_score(X_scaled, labels)
else:
sil = None
return labels, sil
def clustering_gmm(X_scaled, n_components=2):
gmm = GaussianMixture(n_components=n_components, random_state=42)
labels = gmm.fit_predict(X_scaled)
sil = silhouette_score(X_scaled, labels)
return labels, sil
def scale_features(X_train, X_test):
sc = StandardScaler()
X_train_sc = sc.fit_transform(X_train)
X_test_sc = sc.transform(X_test)
return X_train_sc, X_test_sc