Kaggle competition: Two Sigma: Using News to Predict Stock Movements
def create_VGG16_model() :
pretrained_model = tf.keras.applications.VGG16(weights = 'imagenet', include_top = False, input_shape = [*IMAGE_SIZE, 3])
 pretrained_model.trainable = True
model = tf.keras.Sequential([
pretrained_model,
tf.keras.layers.GlobalAveragePooling2D() ,
tf.keras.layers.Dense(len(CLASSES), activation = 'softmax')
 ])
 model.compile(optimizer = 'adam', loss = 'sparse_categorical_crossentropy', metrics = ['sparse_categorical_accuracy'])
 return model<choose_model_class>
env = twosigmanews.make_env() 
(marketdf, newsdf)= env.get_training_data()
print('preparing data...')
 def prepare_data(marketdf, newsdf):
marketdf['time'] = marketdf.time.dt.strftime("%Y%m%d" ).astype(int)
 marketdf['bartrend'] = marketdf['close'] / marketdf['open']
marketdf['average'] =(marketdf['close'] + marketdf['open'])/2
marketdf['pricevolume'] = marketdf['volume'] * marketdf['close']
newsdf['time'] = newsdf.time.dt.strftime("%Y%m%d" ).astype(int)
 newsdf['assetCode'] = newsdf['assetCodes'].map(lambda x: list(eval(x)) [0])
 newsdf['position'] = newsdf['firstMentionSentence'] / newsdf['sentenceCount']
newsdf['coverage'] = newsdf['sentimentWordCount'] / newsdf['wordCount']
marketdf = marketdf.loc[marketdf['time'] > 20120000]
droplist = ['sourceTimestamp','firstCreated','sourceId','headline','takeSequence','provider','firstMentionSentence',
'sentenceCount','bodySize','headlineTag','marketCommentary','subjects','audiences','sentimentClass',
'assetName', 'assetCodes','urgency','wordCount','sentimentWordCount']
newsdf.drop(droplist, axis=1, inplace=True)
 marketdf.drop(['assetName', 'volume'], axis=1, inplace=True)
 newsgp = newsdf.groupby(['time','assetCode'], sort=False ).aggregate(np.mean ).reset_index()
return pd.merge(marketdf, newsgp, how='left', on=['time', 'assetCode'], copy=False)
 cdf = prepare_data(marketdf, newsdf)
 del marketdf, newsdf
def create_Xception_model() :
pretrained_model = tf.keras.applications.Xception(include_top = False, input_shape = [*IMAGE_SIZE, 3])
 pretrained_model.trainable = True
model = tf.keras.Sequential([
pretrained_model,
tf.keras.layers.GlobalAveragePooling2D() ,
tf.keras.layers.Dense(len(CLASSES), activation = 'softmax')
 ])
 model.compile(optimizer = 'adam', loss = 'sparse_categorical_crossentropy', metrics = ['sparse_categorical_accuracy'])
 return model<choose_model_class>
print('building training set...')
 targetcols = ['returnsOpenNextMktres10']
traincols = [col for col in cdf.columns if col not in ['time', 'assetCode', 'universe'] + targetcols]
dates = cdf['time'].unique()
train = range(len(dates)) [:int(0.85*len(dates)) ]
val = range(len(dates)) [int(0.85*len(dates)) :]
cdf[targetcols[0]] =(cdf[targetcols[0]] > 0 ).astype(int)
 Xt = cdf[traincols].fillna(0 ).loc[cdf['time'].isin(dates[train])].values
Yt = cdf[targetcols].fillna(0 ).loc[cdf['time'].isin(dates[train])].values
Xv = cdf[traincols].fillna(0 ).loc[cdf['time'].isin(dates[val])].values
Yv = cdf[targetcols].fillna(0 ).loc[cdf['time'].isin(dates[val])].values
print(Xt.shape, Xv.shape )
def create_DenseNet_model() :
pretrained_model = tf.keras.applications.DenseNet201(weights = 'imagenet', include_top = False, input_shape = [*IMAGE_SIZE, 3])
 pretrained_model.trainable = True
model = tf.keras.Sequential([
pretrained_model,
tf.keras.layers.GlobalAveragePooling2D() ,
tf.keras.layers.Dense(len(CLASSES), activation = 'softmax')
 ])
 model.compile(optimizer = 'adam', loss = 'sparse_categorical_crossentropy', metrics = ['sparse_categorical_accuracy'])
 return model<choose_model_class>
print('Training XGBoost')
 xgbmodel = xgb.XGBClassifier(scale_pos_weight=100,
n_estimators=200,
max_depth=100, objective='binary:logistic')
 xgbmodel.fit(Xt, Yt[:,0],
eval_set=[(Xt, Yt[:,0]),(Xv, Yv[:,0])],
eval_metric='logloss',early_stopping_rounds=10)
def create_EfficientNet_model() :
pretrained_model = efficientnet.EfficientNetB7(weights = 'noisy-student', include_top = False, input_shape = [*IMAGE_SIZE, 3])
 pretrained_model.trainable = True
model = tf.keras.Sequential([
pretrained_model,
tf.keras.layers.GlobalAveragePooling2D() ,
tf.keras.layers.Dense(len(CLASSES), activation = 'softmax')
 ])
 model.compile(optimizer = 'adam', loss = 'sparse_categorical_crossentropy', metrics = ['sparse_categorical_accuracy'])
 return model<choose_model_class>
<choose_model_class><EOS> 
print("generating predictions...")
 preddays = env.get_prediction_days()
for marketdf, newsdf, predtemplatedf in preddays:
cdf = prepare_data(marketdf, newsdf)
 Xp = cdf[traincols].fillna(0 ).values
preds = xgbmodel.predict(Xp)* 2-1
predsdf = pd.DataFrame({'ast':cdf['assetCode'],'conf':preds})
 predtemplatedf['confidenceValue'][predtemplatedf['assetCode'].isin(predsdf.ast)] = predsdf['conf'].values
env.predict(predtemplatedf)
 env.write_submission_file()