Kaggle competition: Categorical Feature Encoding Challenge
<SOS> metric: AUC Kaggle data source: categorical-feature-encoding-challenge<merge>
!pip install target_encoding
def merge_news_on_index(news_train, index_df):
news_train['news_index'] = news_train.index.copy()
news_unstack = index_df.merge(news_train, how='left', on='news_index')
 news_unstack.drop(['news_index', 'assetCodes'], axis=1, inplace=True)
 return news_unstack<merge>
import pandas as pd
from target_encoding import TargetEncoderClassifier, TargetEncoder
from sklearn.model_selection import StratifiedKFold, cross_val_score
from sklearn.preprocessing import LabelEncoder
from sklearn.linear_model import LinearRegression, LogisticRegression
news_unstack = merge_news_on_index(news_train, index_df)
 del news_train, index_df
gc.collect()
news_unstack.head(3 )<data_type_conversions>
train=pd.read_csv(".. /input/cat-in-the-dat/train.csv")
 test=pd.read_csv(".. /input/cat-in-the-dat/test.csv")
 sample_submission = pd.read_csv('.. /input/cat-in-the-dat/sample_submission.csv')
 cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=42 )
def group_news(news_frame):
news_frame['date'] = news_frame.time.dt.date
aggregations = ['mean']
gp = news_frame.groupby(['assetCode', 'date'] ).agg(aggregations)
 gp.columns = pd.Index(["{}_{}".format(e[0], e[1])for e in gp.columns.tolist() ])
 gp.reset_index(inplace=True)
 gp.drop(['sourceId_mean'],axis=1, inplace=True)
 float_cols = {c: 'float32' for c in gp.columns if c not in ['assetCode', 'date']}
return gp.astype(float_cols )<groupby>
len_uniques = []
for c in train.columns.drop(['id', 'target']):
le = LabelEncoder()
le.fit(pd.concat([train[c], test[c]]))
train[c] = le.transform(train[c])
 test[c] = le.transform(test[c])
 print(c, len(le.classes_))
len_uniques.append(len(le.classes_))
X = train.drop(['target', 'id'], axis=1)
 y = train['target']
news_agg = group_news(news_unstack)
 del news_unstack; gc.collect()
news_agg.head(3 )<feature_engineering>
ALPHA = 75
MAX_UNIQUE = max(len_uniques)
 FEATURES_COUNT = X.shape[1]
def process_date(df):
df['date'] = df['time'].dt.date
df['month'] = df['time'].dt.month
df['dayofweek'] = df['time'].dt.dayofweek
return df<define_variables>
lin = LogisticRegression()
score = cross_val_score(lin, X_train, y, scoring='roc_auc', cv=cv)
 print(f'score: {score.mean() :.4}, std: {score.std() :.4}')
 lin.fit(X_train, y)
 pred_lin = lin.predict_proba(X_test)[:,1]
def process_ma(df,columns=['open','close','volume'], windows=[10, 20, 60]):
ma_columns = []
ma_dev_columns = []
drop_list = []
for col in columns:
for window in windows:
ma_column = 'ma_{0}_{1}'.format(col,window)
 ma_dev_column = 'ma_dev_{0}_{1}'.format(col,window)
 ma_lag_column = 'ma_lag_{0}_{1}'.format(col,window)
 ma_lag_rate_column = 'ma_lag_rate_{0}_{1}'.format(col,window)
 std_column = 'std_{0}_{1}'.format(col,window)
 ma_columns.append(ma_column)
 ma_dev_columns.append(ma_dev_column)
 df[ma_column] = df.groupby('assetCode')[col].apply(lambda x: x.rolling(window ).mean())
 df[ma_dev_column] = df[col] / df[ma_column] - 1
df[std_column] = df.groupby('assetCode')[col].apply(lambda x: x.rolling(window ).std())
 df[ma_lag_column] = df.groupby('assetCode')[ma_column].shift()
df[ma_lag_rate_column] =(df[ma_column] - df[ma_lag_column])/ df[ma_lag_column]
drop_list.append(ma_column)
 drop_list.append(ma_lag_column)
 df.drop(drop_list,axis=1, inplace=True)
 return df<categorify>
sample_submission['target'] = pred_enc + pred_lin
sample_submission.to_csv('submission.csv', index=False )