Kaggle competition: Categorical Feature Encoding Challenge II
<SOS> metric: AUC Kaggle data source: categorical-feature-encoding-challenge-ii<choose_model_class>
%matplotlib inline
test_features = pd.read_csv(".. /input/cat-in-the-dat-ii/test.csv")
 train_set = pd.read_csv(".. /input/cat-in-the-dat-ii/train.csv")
 train_targets = train_set.target
train_features = train_set.drop(['target'], axis=1)
 percentage = train_targets.mean() * 100
print("The percentage of ones in the training target is {:.2f}%".format(percentage))
train_features.head()
start_time = time.time()
print('Building tfidf score matrix for the vocab and and start time is:' , 
(time.strftime("%Hhrs:%Mm:%Ss", time.gmtime(( start_time)))))
 noOfocurance = 10
vectorizer = TfidfVectorizer(analyzer=lambda x: x, min_df=noOfocurance)
 matrix = vectorizer.fit_transform([x for x in sentences])
 tfidf = dict(zip(vectorizer.get_feature_names() , vectorizer.idf_))
print(' vocab size(%d )...' %(len(tfidf)))
 end_time = time.time()
print('Completed tfidf score matrix for the vocab and time taken::', 
(time.strftime("%Hhrs:%Mm:%Ss",(time.gmtime(( end_time - start_time))))))<categorify>
columns = [col for col in train_features.columns if col != 'id']
df = train_features[columns]
train_encoded = pd.DataFrame()
skf = StratifiedKFold(n_splits=5,shuffle=True ).split(df, train_targets)
 for tr_in,fold_in in skf:
encoder = ce.WOEEncoder(cols=columns, handle_missing='return_nan')
 encoder.fit(df.iloc[tr_in,:], train_targets.iloc[tr_in])
 train_encoded = train_encoded.append(encoder.transform(df.iloc[fold_in,:]),ignore_index=False)
 train_encoded = train_encoded.sort_index()
encoder = ce.WOEEncoder(cols=columns, handle_missing='return_nan', handle_unknown='return_nan')
 encoder.fit(df, train_targets)
 test_encoded = encoder.transform(test_features[columns])
 train_encoded.head()
def getBrand(text):
global globvar
global nameString
global nameDesc
quit = False
if(nameDesc):
nameText = nameString[globvar]
else:
nameText = descString[globvar]
globvar += 1
words = nameText.split()
rtext ='0000aaaa'
for word in words:
brandFound = brandNames.get(word, 'NO')
 if brandFound != 'NO':
rtext = brandFound
quit = True
break
if not quit:
for brand in brandMultiNames.keys() :
result = ''
result = re.search('\\b'+brand+'\\b', nameText)
 if(result):
rtext = brand
quit = True
break
return(rtext )<feature_engineering>
new_train_encoded = train_encoded.copy()
new_test_encoded = test_encoded.copy()
for currently_encoding in columns:
tr_null = train_encoded.loc[train_encoded[currently_encoding].isnull() ].copy()
tr_not_null = train_encoded.loc[~train_encoded[currently_encoding].isnull() ].copy()
ts_null = test_encoded.loc[test_encoded[currently_encoding].isnull() ]
ts_not_null = test_encoded.loc[~test_encoded[currently_encoding].isnull() ]
temp_tr_feat = tr_not_null.drop(currently_encoding, axis=1 ).fillna(0)
 temp_tr_targ = tr_not_null[currently_encoding]
temp_val_feat = tr_null.drop(currently_encoding, axis=1 ).fillna(0)
 temp_test_feat = ts_null.drop(currently_encoding, axis=1 ).fillna(0)
 parameters = {
'n_estimators':30,
'max_depth':15,
'learning_rate':0.05,
'reg_lambda':0.03,
'reg_alpha':0.03,
'random_state':1728
}
regressor = XGBRegressor(**parameters)
 regressor.fit(temp_tr_feat, temp_tr_targ)
 temp_pred = pd.Series(regressor.predict(temp_val_feat))
temp_pred.index = temp_val_feat.index
new_train_encoded.loc[train_encoded[currently_encoding].isnull() , [currently_encoding]] = temp_pred
temp_pred = pd.Series(regressor.predict(temp_test_feat))
temp_pred.index = temp_test_feat.index
new_test_encoded.loc[test_encoded[currently_encoding].isnull() , [currently_encoding]] = temp_pred
new_train_encoded.info()
<prepare_x_and_y><EOS> 
regressor = LogisticRegression(solver='lbfgs', max_iter=1000, C=0.6)
 regressor.fit(new_train_encoded, train_targets)
 probabilities = [pair[1] for pair in regressor.predict_proba(new_test_encoded)]
output = pd.DataFrame({'id': test_features['id'],
'target': probabilities})
 output.to_csv('submission.csv', index=False)
 output.describe()