-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkaggle_test.py
More file actions
358 lines (263 loc) · 11.4 KB
/
Copy pathkaggle_test.py
File metadata and controls
358 lines (263 loc) · 11.4 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# -*- coding: utf-8 -*-
""" Created on Mon Feb 19 22:13:35 2024 @author: deepak """
""" https://www.kaggle.com/code/chinmayadatt/obesity-risk-prediction-multi-class-0-92160 """
import os;
import tensorflow as tf
import random as rn
os.listdir('/kaggle/input')
os.environ['PYTHONHASHSEED'] = '51'
rn.seed(89)
tf.random.set_seed(40)
import warnings
warnings.filterwarnings("ignore")
import numpy as np, pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.pipeline import make_pipeline, Pipeline
from sklearn.preprocessing import StandardScaler, MinMaxScaler
from category_encoders import OneHotEncoder, CatBoostEncoder, MEstimateEncoder
from sklearn.model_selection import StratifiedGroupKFold
from xgboost import XGBClassifier
from catboost import CatBoostClassifier
from lightgbm import LGBMClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import RidgeClassifier, LogisticRegression
from sklearn import set_config
import os
from sklearn.preprocessing import FunctionTransformer
from sklearn.model_selection import StratifiedKFold
import optuna
from sklearn.compose import ColumnTransformer
from prettytable import PrettyTable
from sklearn.compose import make_column_transformer
from sklearn.base import clone
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.metrics import accuracy_score
import optuna
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
# Set Prameters for Reproduciblity
pd.set_option("display.max_rows",100)
FILE_PATH = "/kaggle/input/playground-series-s4e2/"
TARGET = "NObeyesdad"
n_splits = 9
RANDOM_SEED = 73
# load all data
train = pd.read_csv(os.path.join(FILE_PATH, "train.csv"))
test = pd.read_csv(os.path.join(FILE_PATH, "test.csv"))
sample_sub = pd.read_csv(os.path.join(FILE_PATH, "sample_submission.csv"))
train_org = pd.read_csv("/kaggle/input/obesity-or-cvd-risk-classifyregressorcluster/ObesityDataSet.csv")
def prettify_df(df):
table = PrettyTable()
table.field_names = df.columns
for row in df.values:
table.add_row(row)
print(table)
train.head(10)
# Train Data
print("Train Data")
print(f"Total number of rows: {len(train)}")
print(f"Total number of columns: {train.shape[1]}\n")
# Test Data
print("Test Data")
print(f"Total number of rows: {len(test)}")
print(f"Total number of columns:{test.shape[1]}")
# check null and unique count
# FHWO: family_history_with_overweight
train_copy = train.rename(columns={"family_history_with_overweight":"FHWO"})
tmp = pd.DataFrame(index=train_copy.columns)
tmp['count'] = train_copy.count()
tmp['dtype'] = train_copy.dtypes
tmp['nunique'] = train_copy.nunique()
tmp['%nunique'] = (tmp['nunique']/len(train_copy))*100
tmp['%null'] = (train_copy.isnull().sum()/len(train_copy))*100
tmp['min'] = train_copy.min()
tmp['max'] = train_copy.max()
tmp
tmp.reset_index(inplace=True)
tmp = tmp.rename(columns = {"index":"Column Name"})
tmp = tmp.round(3)
prettify_df(tmp)
del tmp, train_copy
# Target Distribution with Gender
pd.set_option('display.float_format', '{:.2f}'.format)
tmp = pd.DataFrame(train.groupby([TARGET,'Gender'])["id"].agg('count'))
tmp.columns = ['Count']
train[TARGET].value_counts()
tmp = pd.merge(tmp,train[TARGET].value_counts(),left_index=True, right_index=True)
tmp.columns = ['gender_count','target_class_count']
tmp['%gender_count'] = tmp['gender_count']/tmp['target_class_count']
tmp["%target_class_count"] = tmp['target_class_count']/len(train)
tmp = tmp[['gender_count','%gender_count','target_class_count','%target_class_count']]
print("Target Distribution with Gender")
tmp
raw_num_cols = list(train.select_dtypes("float").columns)
raw_cat_cols = list(train.columns.drop(raw_num_cols+[TARGET]))
full_form = dict({'FAVC' : "Frequent consumption of high caloric food",
'FCVC' : "Frequency of consumption of vegetables",
'NCP' :"Number of main meal",
'CAEC': "Consumption of food between meals",
'CH2O': "Consumption of water daily",
'SCC': "Calories consumption monitoring",
'FAF': "Physical activity frequency",
'TUE': "Time using technology devices",
'CALC': "Consumption of alcohol" ,
'MTRANS' : "Transportation used"})
fig, axs = plt.subplots(1,2,figsize = (12,5))
plt.suptitle("Target Distribution")
sns.histplot(binwidth=0.5,x=TARGET,data=train,hue='Gender',palette="dark",ax=axs[0],discrete=True)
axs[0].tick_params(axis='x', rotation=60)
axs[1].pie(
train[TARGET].value_counts(),
shadow = True,
explode=[.1 for i in range(train[TARGET].nunique())],
labels = train[TARGET].value_counts().index,
autopct='%1.f%%',
)
plt.tight_layout()
plt.show()
fig,axs = plt.subplots(len(raw_num_cols),1,figsize=(12,len(raw_num_cols)*2.5),sharex=False)
for i, col in enumerate(raw_num_cols):
sns.violinplot(x=TARGET, y=col,hue="Gender", data=train,ax = axs[i], split=False)
if col in full_form.keys():
axs[i].set_ylabel(full_form[col])
plt.tight_layout()
plt.show()
_,axs = plt.subplots(int(len(raw_cat_cols)-1),2,figsize=(12,len(raw_cat_cols)*3),width_ratios=[1, 4])
for i,col in enumerate(raw_cat_cols[1:]):
sns.countplot(y=col,data=train,palette="bright",ax=axs[i,0])
sns.countplot(x=col,data=train,hue=TARGET,palette="bright",ax=axs[i,1])
if col in full_form.keys():
axs[i,0].set_ylabel(full_form[col])
plt.tight_layout()
plt.show()
tmp = train[raw_num_cols].corr("pearson")
sns.heatmap(tmp,annot=True,cmap ="crest")
sns.jointplot(data=train, x="Height", y="Weight", hue=TARGET,height=6)
sns.jointplot(data=train, x="Age", y="Height", hue=TARGET,height=6)
from sklearn.decomposition import PCA
from sklearn.cluster import KMeans
#PCA
pca = PCA(n_components=2)
pca_top_2 = pca.fit_transform(train[raw_num_cols])
tmp = pd.DataFrame(data = pca_top_2, columns = ['pca_1','pca_2'])
tmp['TARGET'] = train[TARGET]
fig,axs = plt.subplots(2,1,figsize = (12,6))
sns.scatterplot(data=tmp, y="pca_1", x="pca_2", hue='TARGET',ax=axs[0])
axs[0].set_title("Top 2 Principal Components")
#KMeans
kmeans = KMeans(7,random_state=RANDOM_SEED)
kmeans.fit(tmp[['pca_1','pca_2']])
sns.scatterplot( y= tmp['pca_1'],x = tmp['pca_2'],c = kmeans.labels_,cmap='viridis', marker='o', edgecolor='k', s=50, alpha=0.8,ax = axs[1])
axs[1].set_title("Kmean Clustring on First 2 Principal Components")
plt.tight_layout()
plt.show()
# In age_rounder, height_rounder func we multiply values
# by some value this sometimes improve model CV score
# In Extract features we combine features to get new features
def age_rounder(x):
x_copy = x.copy()
x_copy['Age'] = (x_copy['Age']*100).astype(np.uint16)
return x_copy
def height_rounder(x):
x_copy = x.copy()
x_copy['Height'] = (x_copy['Height']*100).astype(np.uint16)
return x_copy
def extract_features(x):
x_copy = x.copy()
x_copy['BMI'] = (x_copy['Weight']/x_copy['Height']**2)
# x_copy['PseudoTarget'] = pd.cut(x_copy['BMI'],bins = [0,18.4,24.9,29,34.9,39.9,100],labels = [0,1,2,3,4,5],)
return x_copy
def col_rounder(x):
x_copy = x.copy()
cols_to_round = ['FCVC',"NCP","CH2O","FAF","TUE"]
for col in cols_to_round:
x_copy[col] = round(x_copy[col])
x_copy[col] = x_copy[col].astype('int')
return x_copy
AgeRounder = FunctionTransformer(age_rounder)
HeightRounder = FunctionTransformer(height_rounder)
ExtractFeatures = FunctionTransformer(extract_features)
ColumnRounder = FunctionTransformer(col_rounder)
# Using FeatureDropper we can drop columns. This is
# important if we want to pass different set of features
# for different models
from sklearn.base import BaseEstimator, TransformerMixin
class FeatureDropper(BaseEstimator, TransformerMixin):
def __init__(self, cols):
self.cols = cols
def fit(self,x,y):
return self
def transform(self, x):
return x.drop(self.cols, axis = 1)
# In cross_val_model we cross vaidate models using
# Stratified K-Fold.
# Encoding target values with int
target_mapping = {
'Insufficient_Weight':0,
'Normal_Weight':1,
'Overweight_Level_I':2,
'Overweight_Level_II':3,
'Obesity_Type_I':4,
'Obesity_Type_II':5 ,
'Obesity_Type_III':6
}
# Define a method for Cross validation here we are using StartifiedKFold
skf = StratifiedKFold(n_splits=n_splits)
def cross_val_model(estimators,cv = skf, verbose = True):
'''
estimators : pipeline consists preprocessing, encoder & model
cv : Method for cross validation (default: StratifiedKfold)
verbose : print train/valid score (yes/no)
'''
X = train.copy()
y = X.pop(TARGET)
y = y.map(target_mapping)
test_predictions = np.zeros((len(test),7))
valid_predictions = np.zeros((len(X),7))
val_scores, train_scores = [],[]
for fold, (train_ind, valid_ind) in enumerate(skf.split(X,y)):
model = clone(estimators)
#define train set
X_train = X.iloc[train_ind]
y_train = y.iloc[train_ind]
#define valid set
X_valid = X.iloc[valid_ind]
y_valid = y.iloc[valid_ind]
model.fit(X_train, y_train)
if verbose:
print("-" * 100)
print(f"Fold: {fold}")
print(f"Train Accuracy Score:-{accuracy_score(y_true=y_train,y_pred=model.predict(X_train))}")
print(f"Valid Accuracy Score:-{accuracy_score(y_true=y_valid,y_pred=model.predict(X_valid))}")
print("-" * 100)
test_predictions += model.predict_proba(test)/cv.get_n_splits()
valid_predictions[valid_ind] = model.predict_proba(X_valid)
val_scores.append(accuracy_score(y_true=y_valid,y_pred=model.predict(X_valid)))
if verbose:
print(f"Average Mean Accuracy Score:- {np.array(val_scores).mean()}")
return val_scores, valid_predictions, test_predictions
#Combine Orignal & Synthetic Data
train.drop(['id'],axis = 1, inplace = True)
test_ids = test['id']
test.drop(['id'],axis = 1, inplace=True)
train = pd.concat([train,train_org],axis = 0)
train = train.drop_duplicates()
train.reset_index(drop=True, inplace=True)
# empty dataframe to store score, & train / test predictions.
score_list, oof_list, predict_list = pd.DataFrame(), pd.DataFrame(), pd.DataFrame()
# Define Random Forest Model Pipeline
RFC = make_pipeline(
ExtractFeatures,
MEstimateEncoder(cols=['Gender','family_history_with_overweight','FAVC','CAEC',
'SMOKE','SCC','CALC','MTRANS']),
RandomForestClassifier(random_state=RANDOM_SEED)
)
# Execute Random Forest Pipeline
val_scores,val_predictions,test_predictions = cross_val_model(RFC)
# Save train/test predictions in dataframes
for k,v in target_mapping.items():
oof_list[f"rfc_{k}"] = val_predictions[:,v]
for k,v in target_mapping.items():
predict_list[f"rfc_{k}"] = test_predictions[:,v]
# 0.8975337326149792
# 0.9049682643904575