-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathModelcraft data modelling.py
More file actions
320 lines (210 loc) · 8.71 KB
/
Copy pathModelcraft data modelling.py
File metadata and controls
320 lines (210 loc) · 8.71 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/usr/bin/env python
# coding: utf-8
# In[1]:
# import neccessary modules
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder,StandardScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score,classification_report, precision_score, recall_score, f1_score, confusion_matrix
from sklearn.linear_model import LinearRegression, Lasso, Ridge
from sklearn.metrics import mean_squared_error,r2_score
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_absolute_error,mean_absolute_percentage_error
import matplotlib.pyplot as plt
import numpy as np
# In[2]:
# load the data
df = pd.read_csv('group_data_cleaning.csv')
df.head()
# In[3]:
df.info()
# #### Feature Engineering
# In[4]:
df['What is your current monthly income level?'].unique()
# In[5]:
# Create the binary variable 'Income_Above_200K' using a conditional mapping
df['Income_Above_200K'] = df['What is your current monthly income level?'].apply(lambda x: 1 if 'and more' in x or '200,000' in x else 0)
# In[6]:
df['Income_Above_200K'].unique()
# In[7]:
df['Career Success'] = df['Income_Above_200K']
# In[8]:
# check for the unique levels of education
df['What is your highest level of education?'].unique()
# In[9]:
# Create a dictionary to map educational levels
education_levels = {
'PhDs/Doctorate Degree': 6,
'MBA degree': 5,
"Master's degree": 4,
"Bachelor's degree": 3,
'Higher National Diploma': 2,
'Ordinary National Diploma': 1
}
# Map the educational levels to a new feature
df['Educational Achievement Level'] = df['What is your highest level of education?'].map(education_levels)
df['Educational Achievement Level']
# In[10]:
df['Educational Achievement Level'].unique()
# In[11]:
df = df.dropna(subset=['Educational Achievement Level'])
df['Educational Achievement Level'].unique()
# In[12]:
#create an experience level
df['Graduation Year'] = pd.to_datetime(df['Year of graduation'], format='%Y')
# Calculate experience level by subtracting graduation year from the current year
current_year = pd.Timestamp('now')
df['Experience Level'] = current_year.year - df['Graduation Year'].dt.year
df['Experience Level']
# ##### Data Splitting
# In[13]:
# Define the target variable
target_column = 'Career Success'
y = df[target_column]
# Define the features or predictors
feature_columns = [
'Year of graduation',
'Educational Achievement Level',
'Experience Level',
'What sector/industry is your company in? (E.g. Banking, Agriculture, Telecommunication)',
'What is your current monthly income level?',
]
X = df[feature_columns]
# Preprocess categorical features using label encoding
le = LabelEncoder()
for column in X.select_dtypes(include=['object']).columns:
X[column] = le.fit_transform(X[column])
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Initialize and train a classification model (Random Forest in this example)
model = RandomForestClassifier(random_state=42)
model.fit(X_train, y_train)
# Make predictions
y_pred = model.predict(X_test)
# Evaluate the model
accuracy = accuracy_score(y_test, y_pred)
classification_rep = classification_report(y_test, y_pred)
# Print the results
print(f"Accuracy: {accuracy}")
print("Classification Report:")
print(classification_rep)
# In[14]:
df['Career Success'].unique()
# In[15]:
#split the dataset
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)
# Make predictions on the validation dataset
y_val_pred = model.predict(X_val)
# Calculate and print the evaluation metrics
accuracy = accuracy_score(y_val, y_val_pred)
precision = precision_score(y_val, y_val_pred)
recall = recall_score(y_val, y_val_pred)
f1 = f1_score(y_val, y_val_pred)
confusion = confusion_matrix(y_val, y_val_pred)
print(f"Accuracy: {accuracy:.2f}")
print(f"Precision: {precision:.2f}")
print(f"Recall: {recall:.2f}")
print(f"F1 Score: {f1:.2f}")
print("Confusion Matrix:")
print(confusion)
# In[16]:
#split the dataset
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Standardize features
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# Linear Regression
lr = LinearRegression()
lr.fit(X_train, y_train)
y_pred_lr = lr.predict(X_test)
mse_lr = mean_squared_error(y_test, y_pred_lr)
# Lasso Regression with increased max_iter
lasso = Lasso(alpha=1.0, max_iter=10000) # Increase max_iter
lasso.fit(X_train_scaled, y_train)
y_pred_lasso = lasso.predict(X_test_scaled)
mse_lasso = mean_squared_error(y_test, y_pred_lasso)
# Ridge Regression
ridge = Ridge(alpha=1.0) # You can adjust the regularization strength (alpha)
ridge.fit(X_train, y_train)
y_pred_ridge = ridge.predict(X_test)
mse_ridge = mean_squared_error(y_test, y_pred_ridge)
print("Linear Regression MSE:", mse_lr)
print("Lasso Regression MSE:", mse_lasso)
print("Ridge Regression MSE:", mse_ridge)
# In[17]:
# Split your dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Initialize and train the Linear Regression model
linear_reg = LinearRegression()
linear_reg.fit(X_train, y_train)
# Make predictions on the test set
y_pred = linear_reg.predict(X_test)
# Evaluate the model
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
# Print the evaluation metrics
print("Mean Squared Error:", mse)
print("R-squared (R2):", r2)
# In[18]:
# Initialize and train the Random Forest Regressor model
random_forest_reg = RandomForestRegressor(n_estimators=100, random_state=42) # Adjust hyperparameters as needed
random_forest_reg.fit(X_train, y_train)
# Make predictions on the test set
y_pred_rf = random_forest_reg.predict(X_test)
# Evaluate the Random Forest Regressor model
mse_rf = mean_squared_error(y_test, y_pred_rf)
r2_rf = r2_score(y_test, y_pred_rf)
# Print the evaluation metrics
print("Random Forest Mean Squared Error:", mse_rf)
print("Random Forest R-squared (R2):", r2_rf)
# #### Evaluation Metrics
# In[19]:
y_true = y_test
# Mean Absolute Error (MAE)
mae = mean_absolute_error(y_true, y_pred)
# Mean Squared Error (MSE)
mse = mean_squared_error(y_true, y_pred)
# Root Mean Squared Error (RMSE)
rmse = np.sqrt(mse)
# R-squared (R2)
r2 = r2_score(y_true, y_pred)
# Mean Absolute Percentage Error (MAPE)
mape = mean_absolute_percentage_error(y_true, y_pred)
# Residual Plot
residuals = y_true - y_pred
plt.scatter(y_pred, residuals)
plt.xlabel("Predicted Values")
plt.ylabel("Residuals")
plt.title("Residual Plot")
plt.show()
# Q-Q Plot
import statsmodels.api as sm
sm.qqplot(residuals, line="s")
plt.title("Q-Q Plot")
plt.show()
# #### Interpretations
#
# Accuracy: The model correctly predicted all instances in the dataset.
# Precision:For class 0, the precision is 1.00, which means that all instances predicted as class 0 were correct.
# For class 1, the precision is also 1.00, indicating that all instances predicted as class 1 were correct.
# Recall:A recall of 1.00 for both classes indicates that the model correctly identified all instances of both classes.
# F1-Score:F1-score of 1.00 for both classes suggests a perfect balance between precision and recall for both classes
# Support:For class 0, there are 517 instances, and for class 1, there are 27 instances.
#
# Linear Regression MSE: 0.047088619540719426
# For the Linear Regression model, the MSE is approximately 0.0471.
# This value indicates that, on average, the squared difference between the actual and predicted values is 0.0471.
# A lower MSE suggests that the model's predictions are closer to the actual values, which is a good sign of model performance.
# Lasso Regression MSE: 0.04759665096507353
# For the Lasso Regression model, the MSE is slightly higher, approximately 0.0476.
# This means that the Lasso model's predictions have a slightly higher average squared difference from the actual values compared to the Linear Regression model.
# The Lasso Regression model introduces L1 regularization, which can lead to feature selection and result in slightly different predictive performance compared to linear regression.
# Ridge Regression MSE: 0.04717483706282427
# For the Ridge Regression model, the MSE is approximately 0.0472.
# The Ridge model's performance falls between that of Linear Regression and Lasso Regression.
# Mean Squared Error (MSE):the MSE is approximately 0.0471. This means that, on average, the squared difference between the actual and predicted values is 0.0471.
# A lower MSE indicates that the model's predictions are closer to the actual values.
#