Kaggle competition: Amazon.com - Employee Access Challenge
<SOS> metric: AUC Kaggle data source: amazon.com-employee-access-challenge<define_variables>
train, test = amazon()
print(train.shape, test.shape)
 target = "ACTION"
col4train = [x for x in train.columns if x not in [target, "ROLE_TITLE"]]
y = train[target].values
print("Optimal number of features : %d" % selector.n_features_ )<define_search_space>
def get_model() :
params = {
"n_estimators":300,
"n_jobs": 3,
"random_state":5436,
}
return ExtraTreesClassifier(**params)
 def validate_model(model, data):
skf = StratifiedKFold(n_splits=5, random_state = 4141, shuffle = True)
 stats = cross_validate(
 model, data[0], data[1],
groups=None, scoring='roc_auc',
cv=skf, n_jobs=None, return_train_score = True 
)
 stats = pd.DataFrame(stats)
 return stats.describe().transpose()
def transform_dataset(train, test, func, func_params = {}):
dataset = pd.concat([train, test], ignore_index = True)
 dataset = func(dataset, **func_params)
 if isinstance(dataset, pd.DataFrame):
new_train = dataset.iloc[:train.shape[0],:].reset_index(drop = True)
 new_test = dataset.iloc[train.shape[0]:,:].reset_index(drop = True)
 else:
new_train = dataset[:train.shape[0]]
new_test = dataset[train.shape[0]:]
return new_train, new_test
threshold = [0.001, 0.01, 0.05, 0.1 , 0.5]<compute_test_metric>
new_train, new_test = transform_dataset(
 train[col4train], test[col4train],
assign_rnd_integer, {"number_of_times":5} 
)
 print(new_train.shape, new_test.shape)
 new_train.head(5 )
print("Maximum accuracy score is :", np.max(np.array(scores_sfm)) )<compute_test_metric>
validate_model(
 model = get_model() ,
data = [new_train.values, y]
)
print("Optimal threshold :", threshold[np.argmax(np.array(scores_sfm)) ] )<features_selection>
new_train, new_test = transform_dataset(
 train[col4train], test[col4train],
assign_rnd_integer, {"number_of_times":1} 
)
 print(new_train.shape, new_test.shape)
 validate_model(
 model = get_model() ,
data = [new_train.values, y]
)
selector = sklearn.feature_selection.SelectKBest(k= 11)
 selector.fit(features, target)
 lgb_selected_features = selector.get_support()<train_model>
new_train, new_test = transform_dataset(
 train[col4train], test[col4train],
assign_rnd_integer, {"number_of_times":10} 
)
 print(new_train.shape, new_test.shape)
 validate_model(
 model = get_model() ,
data = [new_train.values, y]
)
lgboost = lgb.LGBMClassifier()
lgboost.fit(features.loc[:,lgb_selected_features], target )<compute_train_metric>
def one_hot(dataset):
ohe = OneHotEncoder(sparse=True, dtype=np.float32, handle_unknown='ignore')
 return ohe.fit_transform(dataset.values )
y_scores_lgb = lgboost.predict_proba(x_test.loc[:,lgb_selected_features])[:, 1]
lgb_fpr, lgb_tpr, lgb_thresholds = sklearn.metrics.roc_curve(y_test, y_scores_lgb)
 lgb_auc = sklearn.metrics.auc(x=lgb_fpr, y=lgb_tpr )<compute_test_metric>
new_train, new_test = transform_dataset(
 train[col4train], test[col4train],
one_hot)
 print(new_train.shape, new_test.shape )
lgb_acc = lgboost.score(x_test.loc[:,lgb_selected_features], y_test )<compute_test_metric>
validate_model(
 model = get_model() ,
data = [new_train, y]
)
print('Area Under Curve: {}, Accuracy: {}'.format(lgb_auc, lgb_acc))<choose_model_class>
def extract_col_interaction(dataset, col1, col2, tfidf = True):
data = dataset.groupby([col1])[col2].agg(lambda x: " ".join(list([str(y)for y in x])))
 if tfidf:
vectorizer = TfidfVectorizer(tokenizer=lambda x: x.split(" "))
else:
vectorizer = CountVectorizer(tokenizer=lambda x: x.split(" "))
data_X = vectorizer.fit_transform(data)
 dim_red = TruncatedSVD(n_components=1, random_state = 5115)
 data_X = dim_red.fit_transform(data_X)
 result = pd.DataFrame()
result[col1] = data.index.values
result[col1+"_{}_svd".format(col2)] = data_X.ravel()
return result
def get_col_interactions_svd(dataset, tfidf = True):
new_dataset = pd.DataFrame()
for col1,col2 in itertools.permutations(dataset.columns, 2):
data = extract_col_interaction(dataset, col1,col2, tfidf)
 col_name = [x for x in data.columns if "svd" in x][0]
new_dataset[col_name] = dataset[[col1]].merge(data, on = col1, how = 'left')[col_name]
return new_dataset
v = ens.VotingClassifier(estimators=[ 
('lr', lr),('NB', nb),('KNN', knn),('SVM', svm),('DT', dt), 
('RF', rf),('BG', bg),('AdaBoost', ada),('GBM', gb), 
('XGBM', xgboost),('LightGBM', lgboost)],
voting='soft',
weights= [1,1,1, 1.25, 1.25, 1.25, 1.25, 1.25, 1.75, 1.5, 1.5] )<features_selection>
validate_model(
 model = get_model() ,
data = [new_train.values, y]
)
selector = sklearn.feature_selection.SelectKBest(k= 11)
 selector.fit(features, target)
 voting_selected_features = selector.get_support()<train_model>
def get_freq_encoding(dataset):
new_dataset = pd.DataFrame()
for c in dataset.columns:
data = dataset.groupby([c] ).size().reset_index()
new_dataset[c+"_freq"] = dataset[[c]].merge(data, on = c, how = "left")[0]
return new_dataset
v.fit(features.loc[:, voting_selected_features], target )<compute_train_metric>
new_train, new_test = transform_dataset(
 train[col4train], test[col4train],
get_freq_encoding 
)
 print(new_train.shape, new_test.shape)
 new_train.head(5 )
y_scores_v = v.predict_proba(features.loc[:, voting_selected_features])[:, 1]
v_fpr, v_tpr, v_thresholds = sklearn.metrics.roc_curve(target, y_scores_v)
 v_auc = sklearn.metrics.auc(x=v_fpr, y=v_tpr )<compute_test_metric>
validate_model(
 model = get_model() ,
data = [new_train.values, y]
)
v_acc = v.score(x_test.loc[:,voting_selected_features], y_test )<compute_test_metric>
new_train1, new_test1 = transform_dataset(
 train[col4train], test[col4train], get_freq_encoding 
)
 new_train2, new_test2 = transform_dataset(
 train[col4train], test[col4train], get_col_interactions_svd 
)
 new_train3, new_test3 = transform_dataset(
 train[col4train], test[col4train],
assign_rnd_integer, {"number_of_times":10} 
)
 new_train = pd.concat([new_train1, new_train2, new_train3], axis = 1)
 new_test = pd.concat([new_test1, new_test2, new_test3], axis = 1)
 print(new_train.shape, new_test.shape )
print('Area Under Curve: {}, Accuracy: {}'.format(v_auc, v_acc))<create_dataframe>
validate_model(
 model = get_model() ,
data = [new_train.values, y]
)
<predict_on_test><EOS> 
model = get_model()
model.fit(new_train.values, y)
 predictions = model.predict_proba(new_test)[:,1]
submit = pd.DataFrame()
submit["Id"] = test["id"]
submit["ACTION"] = predictions
submit.to_csv("submission.csv", index = False )