Kaggle competition: Digit Recognizer
ids = []
countries = []
test_id = test['id']<categorify>
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D
from keras.optimizers import RMSprop,Adam
from keras.preprocessing.image import ImageDataGenerator
for i in range(len(test_id)) :
idx = test_id[i]
ids += [idx] * 5
countries += le.inverse_transform(np.argsort(y_pred[i])[::-1])[:5].tolist()<save_to_csv>
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from mlxtend.plotting import plot_confusion_matrix
submission = pd.DataFrame(np.column_stack(( ids, countries)) , columns=['id', 'country'])
 submission.to_csv('submission.csv',index=False )<set_options>
train_data = pd.read_csv('.. /input/train.csv')
 test_data = pd.read_csv('.. /input/test.csv' )
pd.set_option('display.max_columns', 500)
 warnings.filterwarnings("ignore" )<load_from_csv>
X_train = train_data.copy()
y_train = train_data['label']
del X_train['label']
X_test = test_data.copy()
y_train = to_categorical(y_train, num_classes = 10 )
%%time
train_cudf = cudf.read_csv('/kaggle/input/jane-street-market-prediction/train.csv')
 train = train_cudf.to_pandas()
del train_cudf
features = pd.read_csv('.. /input/jane-street-market-prediction/features.csv')
 example_test = pd.read_csv('.. /input/jane-street-market-prediction/example_test.csv')
 sample_prediction_df = pd.read_csv('.. /input/jane-street-market-prediction/example_sample_submission.csv')
 print("Data is loaded!" )<count_missing_values>
X_train = X_train.astype('float32')/255
X_test = X_test.astype('float32')/255
missing_values_count = train.isnull().sum()
print(missing_values_count)
 total_cells = np.product(train.shape)
 total_missing = missing_values_count.sum()
print("% of missing data = ",(total_missing/total_cells)* 100 )<prepare_x_and_y>
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size = 0.1, random_state=2020 )
train = train[train['weight'] != 0]
train = train.query('date > 85' ).reset_index(drop = True)
 train = train.astype({c: np.float32 for c in train.select_dtypes(include='float64' ).columns})
 train['action'] =(( train['weight'].values * train['resp'].values)> 0 ).astype('int')
 train.fillna(train.mean() ,inplace=True)
 cols = [c for c in train.columns if 'feature' in c]
X_train = train.loc[:, train.columns.str.contains('feature')]
y_train = train.loc[:, 'action']<drop_column>
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu', input_shape =(28,28,1)))
 model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(10, activation = "softmax"))
del x, y, train, features, example_test, sample_prediction_df<import_modules>
optimizer = RMSprop(lr=0.001,rho=0.9, epsilon=1e-08, decay=0.0 )
print("XGBoost version:", xgb.__version__ )<choose_model_class>
model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'] )
clf = xgb.XGBClassifier(
 n_estimators=500,
max_depth=11,
learning_rate=0.05,
subsample=0.9,
colsample_bytree=0.7,
missing=-999,
random_state=2020,
tree_method='gpu_hist'
)<train_model>
epochs = 50
batch_size = 378
%time clf.fit(X_train, y_train )<predict_on_test>
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(X_train )
TRAINING = True
start_time = time.time()
if TRAINING:
env = janestreet.make_env()
th = 0.5
for(test_df, pred_df)in tqdm(env.iter_test()):
if test_df['weight'].item() > 0:
x_tt = test_df.loc[:, test_df.columns.str.contains('feature')].values
if np.isnan(x_tt[:, 1:].sum()):
x_tt[:, 1:] = np.nan_to_num(x_tt[:, 1:])+ np.isnan(x_tt[:, 1:])
 pred = clf.predict(x_tt)
 pred_df.action = np.where(pred >= th, 1, 0 ).astype(int)
 else:
pred_df.action = 0
env.predict(pred_df)
 print(f"took: {time.time() - start_time} seconds" )<feature_engineering>
history = model.fit_generator(datagen.flow(X_train, y_train, batch_size=batch_size),
epochs = epochs, validation_data =(X_val, y_val),
steps_per_epoch=X_train.shape[0] // batch_size )
train = pd.read_csv('.. /input/jane-street-market-prediction/train.csv')
 train = train.query('weight>0' ).reset_index(drop=True)
 train.fillna(train.median() ,inplace=True)
 train['feature_stock_id_sum'] = train['feature_41'] + train['feature_42'] + train['feature_43']
train['feature_1_2_cross'] = train['feature_1']/(train['feature_2']+1e-5)
 NUM_TRAIN_EXAMPLES = len(train )<feature_engineering>
y_test = model.predict(X_test )
features = [c for c in train.columns if 'feature' in c]
f_mean = np.nanmean(train[features[1:]].values,axis=0 )<define_variables>
y_test_classes = np.argmax(y_test, axis = 1 )
PATH ='.. /input/neutralizing2' 
<define_variables>
num = range(1, len(y_test)+1)
 output = pd.DataFrame({'ImageId': num,
'Label': y_test_classes})
 output.to_csv('submission.csv', index=False )
<choose_model_class><EOS> 
y_val_pred = model.predict(X_val )