Predicting on a single row with Rossmann Data

Just an example, maybe someone find usefull:

from fastai.structured import *
from fastai.column_data import *

PATH='data/rossmann/'

table_names = ['train','store']
cat_vars = ['Store', 'DayOfWeek','StateHoliday','SchoolHoliday']
contin_vars = ['CompetitionDistance']

train, store = [pd.read_csv(f'{PATH}{fname}.csv', low_memory=False) for fname in table_names]

joined_train = train.merge(store, how='inner', on="Store")
joined_train = joined_train[cat_vars + contin_vars + ['Sales']].copy()
for v in cat_vars: joined_train[v] = joined_train[v].astype('category').cat.as_ordered()
joined_train = joined_train[joined_train['Sales'] != 0]
df_train, y, nas, mapper = proc_df(joined_train,'Sales',do_scale=True)

yl = np.log(y).astype(np.float32)
idxs = get_cv_idxs(len(df_train))
cat_sz = [(c, len(joined_train[c].cat.categories)+1) for c in cat_vars]
emb_szs = [(c, min(50, (c+1)//2)) for _,c in cat_sz]
n_contin = len(df_train.columns) - len(cat_vars)

md = ColumnarModelData.from_data_frame(PATH, idxs, df_train, yl, cat_vars, bs=256)

m = md.get_learner(emb_szs, n_contin, 0.04, 1, [1000,500], [0.001,0.01])

m.fit(1e-3, 1)

#Read test dataset
test = pd.read_csv(f'{PATH}test.csv', low_memory=False)

joined_test = test.merge(store,how='inner', on="Store")
joined_test = joined_test[cat_vars + contin_vars].copy()
apply_cats(joined_test, joined_train)
df_test, _, _, _ = proc_df(joined_test, None, do_scale=True, na_dict=nas, mapper=mapper)

#Single record 
test_record = df_test.iloc[0] 

cat = test_record[cat_vars].values.astype(np.int64)[None]
contin = test_record.drop(cat_vars).values.astype(np.float32)[None]

#Prediction
model = m.model
model.eval()
prediction = to_np(model(V(cat), V(contin)))
print(f"Sales prediction: {prediction}")
4 Likes