-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotebook.py
More file actions
272 lines (212 loc) · 6.89 KB
/
notebook.py
File metadata and controls
272 lines (212 loc) · 6.89 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# %% [markdown]
# # Submission 1 : Menyelesaikan Permasalahan Human Resources
# %% [markdown]
# - Nama: Labib Ammar Fadhali
# - Email: labibfadhali12@gmail.com
# - Id Dicoding: labibaf
# %% [markdown]
# ## Persiapan
# %% [markdown]
# ### Menyiapkan library yang dibutuhkan
# %%
import pandas as pd
pd.set_option('display.max_rows', 50)
pd.set_option('display.max_columns', 50)
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score
from imblearn.over_sampling import RandomOverSampler
from sklearn.metrics import classification_report, confusion_matrix
import joblib
import warnings
warnings.filterwarnings('ignore')
# %% [markdown]
# ### Menyiapkan data yang akan diguankan
# %% [markdown]
# ## Data Understanding
# %%
df=pd.read_csv('./dataset/employee_data.csv')
df.head()
# %%
df.shape
# %%
df.info()
# %%
df.isnull().sum()
# %%
df.describe(include='all')
# %% [markdown]
# ## Data Preparation / Preprocessing
# %% [markdown]
# Handling Missing Value
# %%
df['Attrition'].value_counts()
# %%
df['Attrition']=df['Attrition'].fillna(df['Attrition'].mode()[0])
# %%
df['Attrition'].isna().sum()
# %% [markdown]
# Duplicate Check
# %%
df.duplicated().sum()
# %%
df.nunique()
# %% [markdown]
# ### Feature Engineering
# %%
df.head()
# %%
attrition_map={1:'Yes',0:'No'}
df['Attrition']=df['Attrition'].map(attrition_map)
education_map={1:'Below College',2:'College',3:'Bachelor',4:'Master',5:'Doctor'}
df['Education']=df['Education'].map(education_map)
performance_map={1:'Low',2:'Good',3:'Excelllent',4:'Outstanding'}
df['PerformanceRating']=df['PerformanceRating'].map(performance_map)
df['WorkLifeBalance']=df['WorkLifeBalance'].map(performance_map)
sactification_map={1:'Low',2:'Medium',3:'High',4:'Very High'}
df['EnvironmentSatisfaction']=df['EnvironmentSatisfaction'].map(sactification_map)
df['JobSatisfaction']=df['JobSatisfaction'].map(sactification_map)
df['RelationshipSatisfaction']=df['RelationshipSatisfaction'].map(sactification_map)
df['JobInvolvement']=df['JobInvolvement'].map(sactification_map)
# %%
df['AgeAtStartEmployment'] = df['Age'] - df['TotalWorkingYears']
# %%
drop_cols=['EmployeeCount','StandardHours','Over18']
df=df.drop(columns=drop_cols,axis=1)
# %%
df.head()
# %%
df.to_csv('./dataset/employee_data_cleaned.csv',index=False)
# %% [markdown]
# ## EDA
# %%
sns.countplot(df['Attrition'])
plt.title('Attrition Distribution')
plt.show()
# %%
plt.pie(df['Attrition'].value_counts(),labels=df['Attrition'].value_counts().index,autopct='%1.1f%%',startangle=90,colors=['#6ff2fc','#66b3ff'])
plt.show()
# %%
def categorical_plot(features,df,segment_feature=None):
"""
Parameter:
- df : DataFrame
DataFrame yang berisi data.
- features : list of strings
Daftar nama kolom yang akan diplot.
- segment_feature : string, opsional
Nama kolom untuk segmentasi data (misalnya untuk plot yang dihasilkan menggunakan hue).
"""
num_plot=len(features)
fig,ax=plt.subplots(num_plot,figsize=(8,5*num_plot))
for i, feature in enumerate(features):
if segment_feature:
sns.countplot(data=df,x=segment_feature,hue=feature,ax=ax[i])
else:
sns.countplot(data=df,x=feature,ax=ax[i])
plt.tight_layout()
plt.show()
# %%
categorical_columns = df.select_dtypes(include=['object']).columns.tolist()
categorical_columns.remove('Attrition')
# %%
categorical_plot(
features=categorical_columns,
df=df,
segment_feature='Attrition'
)
# %%
df_num = df.select_dtypes(include=['int64', 'float64'])
df_num['Attrition'] = df['Attrition']
df_num['Attrition'] = df_num['Attrition'].map({'Yes': 1, 'No': 0})
# %%
correlation = df_num.corr()
plt.figure(figsize=(12, 10))
sns.heatmap(correlation, annot=False, cmap='coolwarm')
plt.title('Korelasi antara Fitur Numerik dan Attrition')
plt.show()
# %% [markdown]
# ## Modeling
# %%
label_encoders = {}
for column in df.select_dtypes(include=['object']).columns:
label_encoders[column] = LabelEncoder()
df[column] = label_encoders[column].fit_transform(df[column])
# %%
X = df.drop(columns=['EmployeeId', 'Attrition'])
y = df['Attrition']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# %%
models = {
'Logistic Regression': LogisticRegression(random_state=42),
'Decision Tree': DecisionTreeClassifier(random_state=42),
'Random Forest': RandomForestClassifier(random_state=42),
'Gradient Boosting': GradientBoostingClassifier(random_state=42),
'Neural Network': MLPClassifier(random_state=42)
}
# %%
for name, model in models.items():
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f"Akurasi model {name}: {accuracy}")
# Classification Report
print(f"Classification Report model {name}:")
print(classification_report(y_test, y_pred))
# Confusion Matrix
cm = confusion_matrix(y_test, y_pred)
plt.figure(figsize=(6, 4))
sns.heatmap(cm, annot=True, fmt="d", cmap="Blues", cbar=False)
plt.title(f"Confusion Matrix - {name}")
plt.xlabel("Predicted")
plt.ylabel("Actual")
plt.show()
# %% [markdown]
# ## Evaluation
# %%
oversampler = RandomOverSampler(random_state=42)
X_resampled, y_resampled = oversampler.fit_resample(X, y)
# %%
X_train, X_test, y_train, y_test = train_test_split(X_resampled, y_resampled, test_size=0.2, random_state=42)
# %%
models = {
'Logistic Regression': LogisticRegression(random_state=42),
'Decision Tree': DecisionTreeClassifier(random_state=42),
'Random Forest': RandomForestClassifier(random_state=42),
'Gradient Boosting': GradientBoostingClassifier(random_state=42),
'Neural Network': MLPClassifier(random_state=42)
}
# %%
best_model = None
best_accuracy = 0
for name, model in models.items():
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f"Akurasi model {name}: {accuracy}")
if accuracy > best_accuracy:
best_accuracy = accuracy
best_model = model
# Classification Report
print(f"Classification Report model {name}:")
print(classification_report(y_test, y_pred))
# Confusion Matrix
cm = confusion_matrix(y_test, y_pred)
plt.figure(figsize=(6, 4))
sns.heatmap(cm, annot=True, fmt="d", cmap="Blues", cbar=False)
plt.title(f"Confusion Matrix - {name}")
plt.xlabel("Predicted")
plt.ylabel("Actual")
plt.show()
print(f"Model terbaik: {best_model}")
# %%
joblib.dump(best_model, './model/best_model.pkl')
print("Model terbaik telah disimpan sebagai 'best_model.pkl'.")