Kaggle competition: Walmart Recruiting - Store Sales Forecasting
data, models = man_data, man_models<categorify>
train=pd.read_csv("/kaggle/input/walmart-recruiting-store-sales-forecasting/train.csv.zip")
 train.head()
def normalizer(dataset):
output_data = dataset[0].copy()
train_data = dataset[1].copy()
test_data = dataset[2].copy()
skew = train_data.skew().abs()
features = skew[skew >.7].index.values
with warnings.catch_warnings() :
warnings.simplefilter("ignore")
 feature_transformer = PowerTransformer(method='yeo-johnson')
 train_data = pd.DataFrame(feature_transformer.fit_transform(train_data), columns=train_data.columns.values)
 test_data = pd.DataFrame(feature_transformer.transform(test_data), columns=test_data.columns.values)
 output_data = pd.Series(np.log1p(output_data.values.reshape(-1,1)).reshape(-1))
return(output_data, train_data, test_data, dataset[3], dataset[4])
 new_data, new_models = validateModels(data, models, all_selector, normalizer )<prepare_output>
test=pd.read_csv("/kaggle/input/walmart-recruiting-store-sales-forecasting/test.csv.zip")
 test.head()
data, models = new_data, new_models<concatenate>
y=train["Weekly_Sales"]
def outlier_remover(dataset):
output_data = dataset[0].copy()
train_data = dataset[1].copy()
outliers = []
outliers.extend(train_data[(train_output < 12.5)&(train_data.GrLivArea > 3)].index.values)
 outliers.extend(train_data[(train_output < 12.5)&(train_data.GarageArea > 3)].index.values)
 outliers.extend(train_data[(train_output < 11)&(train_data.OpenPorchSF > 0.5)].index.values)
 outliers.extend(train_data[(train_output < 12.5)&(train_data['1stFlrSF'] > 4)].index.values)
 train_data = train_data.drop(list(set(outliers)) , axis=0)
 output_data = output_data.drop(list(set(outliers)) , axis=0)
 train_rows, dev_rows = train_dev_split(train_data)
 return(output_data, train_data, dataset[2], train_rows, dev_rows)
 new_data, new_models = validateModels(data, models, all_selector, outlier_remover)
<prepare_output>
train["Date"]=train["Date"].astype("datetime64")
 test["Date"]=test["Date"].astype("datetime64")
 train.dtypes
data, models = new_data, new_models<train_on_grid>
train["month"]=train["Date"].dt.month
test["month"]=test["Date"].dt.month
train["day"]=train["Date"].dt.day
test["day"]=test["Date"].dt.day
train["year"]=train["Date"].dt.year
test["year"]=test["Date"].dt.year
def tune(data, models, params):
p = {}
results = pd.DataFrame(columns=['model', 'result_index', 'mean_test_score', 'std_test_score', 'mean_train_score', 'std_train_score'])
 for(k, v)in params.items() :
if models[k] is not None:
model, selector = models[k] 
(output_data, train_data, test_data, _, _)= data
search = GridSearchCV(model, v, scoring='r2', cv=5, iid=False)
 with warnings.catch_warnings() :
warnings.simplefilter("ignore")
 search.fit(selector(output_data, train_data), output_data)
 size = len(search.cv_results_['mean_test_score'])
 p[k] = search.cv_results_['params']
d_f = pd.DataFrame({'model' : [k] * size,
'result_index' : range(size),
'mean_test_score' : search.cv_results_['mean_test_score'],
'std_test_score' : search.cv_results_['std_test_score'],
'mean_train_score' : search.cv_results_['mean_train_score'],
'std_train_score' : search.cv_results_['std_train_score']})
 d_f.head(4)
 results = pd.concat([results, d_f], axis=0)
 return p, results
def applyParams(models, params):
def proces_params(ps):
result = {}
for k, v in ps.items() :
result[k] = v[0]
return result
for(k, v)in params.items() :
if models[k] is not None:
model, _ = models[k]
ps = proces_params(v)
 model.set_params(**ps )<train_model>
store=pd.read_csv("/kaggle/input/walmart-recruiting-store-sales-forecasting/stores.csv")
 store.head()
lr_params = {'fit_intercept': [True, False],
'n_jobs': [-1],
'normalize': [True, False]}
fitting_params = {
'linear_regression' : lr_params
}<train_model>
train=pd.merge(train,store)
 test=pd.merge(test,store )
params, results = tune(data, models, fitting_params )<sort_values>
train["Type"]=train["Type"].replace({"A":1,"B":2,"C":3})
 test["Type"]=test["Type"].replace({"A":1,"B":2,"C":3} )
results.sort_values(by='mean_test_score', ascending=False ).head(32 )<compute_test_metric>
train=train.drop(["Weekly_Sales","Date"],axis=1)
 train.head()
applyParams(models, fitting_params )<set_options>
tesst=test.drop(["Date"],axis=1)
 tesst.head()
%matplotlib inline<load_from_csv>
rf=RandomForestRegressor(n_estimators=100, n_jobs=-1)
 rf.fit(train,y )
train_df=pd.read_csv(".. /input/train.csv")
 test_df=pd.read_csv(".. /input/test.csv")
 sample_df=pd.read_csv(".. /input/sample_submission.csv" )<count_values>
value=rf.predict(tesst)
 value
train_df.target.value_counts()<filter>
sample=pd.read_csv("/kaggle/input/walmart-recruiting-store-sales-forecasting/sampleSubmission.csv.zip")
 sample.head()
null_data = train_df[train_df.isnull().any(axis=1)]
null_data<data_type_conversions>
sample["Weekly_Sales"]=value
<init_hyperparams><EOS> 
sample.to_csv("20200309.csv",index=False )