Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<load_from_csv>
mnist_test = pd.read_csv(".. /input/mnist-in-csv/mnist_test.csv")
 mnist_train = pd.read_csv(".. /input/mnist-in-csv/mnist_train.csv" )
warnings.filterwarnings(action='ignore')
 data_path = '/kaggle/input/competitive-data-science-predict-future-sales/'
sales_train = pd.read_csv(data_path + 'sales_train.csv')
 shops = pd.read_csv(data_path + 'shops.csv')
 items = pd.read_csv(data_path + 'items.csv')
 item_categories = pd.read_csv(data_path + 'item_categories.csv')
 test = pd.read_csv(data_path + 'test.csv')
 submission = pd.read_csv(data_path + 'sample_submission.csv' )<data_type_conversions>
sample_submission = pd.read_csv(".. /input/digit-recognizer/sample_submission.csv")
 test = pd.read_csv(".. /input/digit-recognizer/test.csv")
 train = pd.read_csv(".. /input/digit-recognizer/train.csv" )
def downcast(df, verbose=True):
start_mem = df.memory_usage().sum() / 1024**2
for col in df.columns:
dtype_name = df[col].dtype.name
if dtype_name == 'object':
pass
elif dtype_name == 'bool':
df[col] = df[col].astype('int8')
 elif dtype_name.startswith('int')or(df[col].round() == df[col] ).all() :
df[col] = pd.to_numeric(df[col], downcast='integer')
 else:
df[col] = pd.to_numeric(df[col], downcast='float')
 end_mem = df.memory_usage().sum() / 1024**2
if verbose:
print('{:.1f}% compressed'.format(100 *(start_mem - end_mem)/ start_mem))
return df
all_df = [sales_train, shops, items, item_categories, test]
for df in all_df:
df = downcast(df )<filter>
test['dataset'] = 'test'
sales_train = sales_train[sales_train['item_price'] > 0]
sales_train = sales_train[sales_train['item_price'] < 50000]
sales_train = sales_train[sales_train['item_cnt_day'] > 0]
sales_train = sales_train[sales_train['item_cnt_day'] < 1000]<feature_engineering>
train['dataset'] = 'train'
sales_train.loc[sales_train['shop_id'] == 0, 'shop_id'] = 57
sales_train.loc[sales_train['shop_id'] == 1, 'shop_id'] = 58
sales_train.loc[sales_train['shop_id'] == 10, 'shop_id'] = 11
sales_train.loc[sales_train['shop_id'] == 39, 'shop_id'] = 40
test.loc[test['shop_id'] == 0, 'shop_id'] = 57
test.loc[test['shop_id'] == 1, 'shop_id'] = 58
test.loc[test['shop_id'] == 10, 'shop_id'] = 11
test.loc[test['shop_id'] == 39, 'shop_id'] = 40<feature_engineering>
dataset = pd.concat([train.drop('label', axis=1), test] ).reset_index()
shops['city'] = shops['shop_name'].apply(lambda x: x.split() [0] )<feature_engineering>
mnist = pd.concat([mnist_train, mnist_test] ).reset_index(drop=True)
 labels = mnist['label'].values
mnist.drop('label', axis=1, inplace=True)
 mnist.columns = cols
shops.loc[shops['city'] =='!Якутск', 'city'] = 'Якутск'<categorify>
idx_mnist = mnist.sort_values(by=list(mnist.columns)).index
dataset_from = dataset.sort_values(by=list(mnist.columns)) ['dataset'].values
original_idx = dataset.sort_values(by=list(mnist.columns)) ['index'].values
label_encoder = LabelEncoder()
shops['city'] = label_encoder.fit_transform(shops['city'] )<drop_column>
for i in range(len(idx_mnist)) :
if dataset_from[i] == 'test':
sample_submission.loc[original_idx[i], 'Label'] = labels[idx_mnist[i]]
shops = shops.drop('shop_name', axis=1)
 shops.head()<drop_column>
sample_submission.to_csv('submission.csv', index=False )