Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<train_model>
import numpy
import pandas as pd
import torch
import torch.nn as nn
x_test = np.zeros(( len(test), img_height, img_width, 3), dtype='float32')
 for i, filepath in tqdm(enumerate(test['filepath'])) :
img = keras.preprocessing.image.load_img(os.path.join(data_dir, filepath), target_size =(img_height, img_width))
x = keras.preprocessing.image.img_to_array(img)
 x_test[i] = x
print('test Images shape: {} size: {:,}'.format(x_test.shape, x_test.size))<predict_on_test>
train_data = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/train.csv")
 test_data = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/test.csv" )
one_hot_prediction = model.predict(x_test)
 one_hot_prediction[0:2]<prepare_output>
train_data.iloc[0:4, [1,2,3,-1,-2]]
preds = np.argmax(one_hot_prediction, axis=1)
 print(preds.shape)
 print(preds)
 prediction = [class_names[i] for i in preds]
print(len(prediction))
print(prediction )<prepare_output>
all_features = pd.concat(( train_data.iloc[:,1:-1], test_data.iloc[:,1:]))
my_submission = pd.DataFrame({'file': test['file'], 'species': prediction})
 print(my_submission.head(5))
print(my_submission.shape )<save_to_csv>
all_features.dtypes[all_features.dtypes == 'int64']
my_submission.to_csv('output.csv', index=False )<import_modules>
numeric_features = all_features.dtypes[all_features.dtypes != 'object'].index
all_features[numeric_features]
from fastai import *
from fastai.vision import *
import matplotlib.pyplot as plt<define_variables>
def normalise(x):
return(( x - x.mean())/x.std() )
path = Path('input/train/' )<load_pretrained>
all_features[numeric_features] = all_features[numeric_features].apply(
 lambda x:(x - x.mean())/(x.std()))
np.random.seed(42)
 data = ImageDataBunch.from_folder(path,test='.. /test', ds_tfms=get_transforms() ,valid_pct=0.25,size=299,bs=32,num_workers=0)
 data.normalize(imagenet_stats )<define_variables>
all_features[numeric_features] = all_features[numeric_features].fillna(0 )
data.show_batch(rows=3,figsize=(7,6))<choose_model_class>
all_features = pd.get_dummies(all_features, dummy_na=True)
 all_features
learn = create_cnn(data,models.resnet50,metrics=error_rate )<find_best_params>
n_train = len(train_data)
 train_features = torch.tensor(all_features[:n_train].values, dtype=torch.float32)
 test_features = torch.tensor(all_features[n_train:].values, dtype=torch.float32)
 train_labels = torch.tensor(train_data.iloc[:,-1].values, dtype=torch.float32)
learn.lr_find()<train_model>
loss = nn.MSELoss()
in_features = train_features.shape[1]
out_features = 1
def get_net() :
net = nn.Sequential(nn.Linear(in_features, out_features))
return net
learn.fit_one_cycle(6,slice(1e-2))<save_model>
def log_rmse(net, features, labels):
clipped_preds = torch.clamp(net(features), 1, float('inf'))
rmse = torch.sqrt(loss(torch.log(clipped_preds), torch.log(labels)))
 return rmse.item()
learn.save('stg-1' )<find_best_params>
def load_array(data_array, batch_size):
in_dataset = torch.utils.data.TensorDataset(*data_array)
 in_dataloader = torch.utils.data.DataLoader(in_dataset, shuffle=True, batch_size=batch_size)
 return in_dataloader
learn.lr_find()<train_model>
def init_weights(m):
if type(m)==nn.Linear or type(m)==nn.Conv2d:
nn.init.xavier_uniform_(m.weight )
learn.unfreeze()
learn.fit_one_cycle(4,max_lr=slice(1e-4,1e-3))<load_pretrained>
def accuracy(y_hat, y):
return(torch.argmax(y_hat, dim=1)==y ).sum().float().mean()
data = ImageDataBunch.from_folder(path,test='.. /test', ds_tfms=get_transforms() ,valid_pct=0.25,size=350,bs=32,num_workers=0)
 data.normalize(imagenet_stats )<compute_train_metric>
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu' )
interp = ClassificationInterpretation.from_learner(learn)
 print(metrics.classification_report(interp.y_true.numpy() , interp.pred_class.numpy() ,target_names =data.classes))<save_model>
net = get_net()
net
learn.save('stg-2' )<prepare_output>
test_labels = None
batch_size= 64
learning_rate = 0.03
lr = learning_rate
weight_decay = 0
num_epochs = 100
learn.data=data<train_model>
train_dataloader = load_array(( train_features, train_labels), batch_size)
 with torch.no_grad() :
for X, y in train_dataloader:
print(X.shape)
 print(X, "
")
 print(len(y))
print(y, "
")
 y_hat = net(X)
 print(y_hat)
 print(accuracy(y_hat, y))
break
learn.unfreeze()
learn.fit_one_cycle(4,max_lr=slice(1e-5,1e-4))<predict_on_test>
def train(net, train_features, train_labels, test_features, test_labels, num_epochs, learning_rate, weight_decay,batch_size):
train_ls, test_ls = [], []
net = net.to(device)
 net.apply(init_weights)
 train_acc = []
optimizer = torch.optim.Adam(net.parameters() , lr = learning_rate, weight_decay=weight_decay)
 train_dataloader = load_array(( train_features, train_labels),batch_size)
 for epoch in range(num_epochs):
curr_acc = 0
numer = 0
for X, y in train_dataloader:
X = X.to(device)
 y = y.to(device)
 y_hat = net(X)
 l = loss(y_hat, y.unsqueeze(1))
curr_acc += accuracy(y_hat, y)
 optimizer.zero_grad()
l.backward()
optimizer.step()
numer += len(y)
 train_ls.append(log_rmse(net, train_features, train_labels))
train_acc.append(curr_acc/ numer)
 if test_labels is not None:
test_ls.append(log_rmse(net, test_features, test_labels))
print(f'for epoch {epoch} rmse: {train_ls[-1]}, accuracy : {train_acc[-1]}')
 return train_ls, test_ls
preds,y=learn.get_preds(ds_type=DatasetType.Test )<prepare_output>
!pip install -U d2l
preds = np.argmax(preds, axis = 1)
 preds_classes = [data.classes[i] for i in preds]<save_to_csv>
train_and_pred(train_features, test_features, train_labels, test_data,
num_epochs, lr, weight_decay, batch_size)
submission = pd.DataFrame({ 'file': os.listdir('input/test'), 'species': preds_classes })
 submission.to_csv('test_classification_results.csv', index=False )<split>
def get_net() :
net = nn.Sequential(nn.Linear(in_features, 256), nn.ReLU() , nn.Linear(256,out_features))
return net
net = get_net()
net
%matplotlib inline
def get_skf_indexes(df, target, kfold=4):
X = df.values
y = df[target].values
skf = StratifiedKFold(n_splits=4);
skf.get_n_splits(X, y);
indexes = [[],[]]
for train_index, test_index in skf.split(X, y):
indexes[0].append(train_index)
 indexes[1].append(test_index)
 return indexes
def output_results(clf, x_test, listing, fname):
preds = clf.predict_proba(x_test)
 preds = pd.DataFrame(preds)
 cols = ['low', 'medium', 'high']
preds.columns = cols
preds['listing_id'] = listing
preds.to_csv(fname, index=None)
 print(preds[cols].mean().values)
 def basic_preprocess(df_train, df_test, n_min=50, precision=3):
df_train['y'] = 0.0
df_train.loc[df_train.interest_level=='medium', 'y'] = 1.0
df_train.loc[df_train.interest_level=='high', 'y'] = 2.0
df_train['num_latitude'] = df_train.latitude.values
df_test['num_latitude'] = df_test.latitude.values
df_train['num_longitude'] = df_train.longitude.values
df_test['num_longitude'] = df_test.longitude.values
x = np.sqrt(((df_train.latitude - df_train.latitude.median())**2)+(df_train.longitude - df_train.longitude.median())**2)
 df_train['num_dist_from_center'] = x.values
x = np.sqrt(((df_test.latitude - df_train.latitude.median())**2)+(df_test.longitude - df_train.longitude.median())**2)
 df_test['num_dist_from_center'] = x.values
df_train['pos'] = df_train.longitude.round(precision ).astype(str)+ '_' + df_train.latitude.round(precision ).astype(str)
 df_test['pos'] = df_test.longitude.round(precision ).astype(str)+ '_' + df_test.latitude.round(precision ).astype(str)
 OutlierAggregated =(df_train.bedrooms > 4 ).astype(float)
 OutlierAggregated2 =(df_test.bedrooms > 4 ).astype(float)
 OutlierAggregated +=(df_train.bathrooms > 3 ).astype(float)
 OutlierAggregated2 +=(df_test.bathrooms > 3 ).astype(float)
 OutlierAggregated +=(df_train.bathrooms < 1 ).astype(float)
 OutlierAggregated2 +=(df_test.bathrooms < 1 ).astype(float)
 x = np.abs(( df_train.price - df_train.price.median())/df_train.price.std())> 0.30
OutlierAggregated += x.astype(float)
 x2 = np.abs(( df_test.price - df_train.price.median())/df_train.price.std())> 0.30
OutlierAggregated2 += x2.astype(float)
 x = np.log1p(df_train.price/(df_train.bedrooms.clip(1,3)+ df_train.bathrooms.clip(1,2)))> 8.2
OutlierAggregated += x.astype(float)
 x2 = np.log1p(df_test.price/(df_test.bedrooms.clip(1,3)+ df_test.bathrooms.clip(1,2)))> 8.2
OutlierAggregated2 += x2.astype(float)
 x = np.sqrt(((df_train.latitude - df_train.latitude.median())**2)+(df_train.longitude - df_train.longitude.median())**2)> 0.30
OutlierAggregated += x.astype(float)
 x2 = np.sqrt(((df_test.latitude - df_train.latitude.median())**2)+(df_test.longitude - df_train.longitude.median())**2)> 0.30
OutlierAggregated2 += x2.astype(float)
 df_train['num_OutlierAggregated'] = OutlierAggregated.values
df_test['num_OutlierAggregated'] = OutlierAggregated2.values
x = df_train.groupby('pos')['y'].aggregate(['count', 'mean'])
 d = x.loc[x['count'] >= n_min, 'mean'].to_dict()
impute = df_train.y.mean()
df_train['num_pos'] = df_train.pos.apply(lambda x: d.get(x, impute))
df_test['num_pos'] = df_test.pos.apply(lambda x: d.get(x, impute))
vals = df_train['pos'].value_counts()
dvals = vals.to_dict()
df_train['num_pos_density'] = df_train['pos'].apply(lambda x: dvals.get(x, vals.min()))
df_test['num_pos_density'] = df_test['pos'].apply(lambda x: dvals.get(x, vals.min()))
df_train['num_building_null'] =(df_train.building_id=='0' ).astype(float)
 df_test['num_building_null'] =(df_test.building_id=='0' ).astype(float)
 x = df_train.groupby('building_id')['y'].aggregate(['count', 'mean'])
 d = x.loc[x['count'] >= n_min, 'mean'].to_dict()
impute = df_train.y.mean()
df_train['num_building_id'] = df_train.building_id.apply(lambda x: d.get(x, impute))
df_test['num_building_id'] = df_test.building_id.apply(lambda x: d.get(x, impute))
d = np.log1p(df_train.building_id.value_counts() ).to_dict()
impute = np.min(np.array(list(d.values())))
 df_train['num_fbuilding'] = df_train.building_id.apply(lambda x: d.get(x, impute))
df_test['num_fbuilding'] = df_test.building_id.apply(lambda x: d.get(x, impute))
x = df_train.groupby('manager_id')['y'].aggregate(['count', 'mean'])
 d = x.loc[x['count'] >= n_min, 'mean'].to_dict()
impute = df_train.y.mean()
df_train['num_manager'] = df_train.manager_id.apply(lambda x: d.get(x, impute))
df_test['num_manager'] = df_test.manager_id.apply(lambda x: d.get(x, impute))
d = np.log1p(df_train.manager_id.value_counts() ).to_dict()
impute = np.min(np.array(list(d.values())))
 df_train['num_fmanager'] = df_train.manager_id.apply(lambda x: d.get(x, impute))
df_test['num_fmanager'] = df_test.manager_id.apply(lambda x: d.get(x, impute))
df_train['created'] = pd.to_datetime(df_train.created)
 df_train['num_created_weekday'] = df_train.created.dt.dayofweek.astype(float)
 df_train['num_created_weekofyear'] = df_train.created.dt.weekofyear
df_test['created'] = pd.to_datetime(df_test.created)
 df_test['num_created_weekday'] = df_test.created.dt.dayofweek
df_test['num_created_weekofyear'] = df_test.created.dt.weekofyear
df_train['num_bathrooms'] = df_train.bathrooms.clip_upper(4)
 df_test['num_bathrooms'] = df_test.bathrooms.clip_upper(4)
 df_train['num_bedrooms'] = df_train.bedrooms.clip_upper(5)
 df_test['num_bedrooms'] = df_test.bedrooms.clip_upper(5)
 df_train['num_price'] = df_train.price.clip_upper(10000)
 df_test['num_price'] = df_test.price.clip_upper(10000)
 bins = df_train.price.quantile(np.arange(0.05, 1, 0.05))
df_train['num_price_q'] = np.digitize(df_train.price, bins)
 df_test['num_price_q'] = np.digitize(df_test.price, bins)
 df_train['num_priceXroom'] =(df_train.price /(1 + df_train.bedrooms.clip(1, 4)+ 0.5*df_train.bathrooms.clip(0, 2)) ).values
df_test['num_priceXroom'] =(df_test.price /(1 + df_test.bedrooms.clip(1, 4)+ 0.5*df_test.bathrooms.clip(0, 2)) ).values
df_train['num_even_bathrooms'] =(( np.round(df_train.bathrooms)- df_train.bathrooms)==0 ).astype(float)
 df_test['num_even_bathrooms'] =(( np.round(df_test.bathrooms)- df_test.bathrooms)==0 ).astype(float)
 df_train['num_features'] = df_train.features.apply(lambda x: len(x))
df_test['num_features'] = df_test.features.apply(lambda x: len(x))
df_train['num_photos'] = df_train.photos.apply(lambda x: len(x))
df_test['num_photos'] = df_test.photos.apply(lambda x: len(x))
df_train['num_desc_length'] = df_train.description.str.split(' ' ).str.len()
df_test['num_desc_length'] = df_test.description.str.split(' ' ).str.len()
df_train['num_desc_length_null'] =(df_train.description.str.len() ==0 ).astype(float)
 df_test['num_desc_length_null'] =(df_test.description.str.len() ==0 ).astype(float)
 bows = {'nofee': ['no fee', 'no-fee', 'no fee', 'nofee', 'no_fee'],
'lowfee': ['reduced_fee', 'low_fee','reduced fee', 'low fee'],
'furnished': ['furnished'],
'parquet': ['parquet', 'hardwood'],
'concierge': ['concierge', 'doorman', 'housekeep','in_super'],
'prewar': ['prewar', 'pre_war', 'pre war', 'pre-war'],
'laundry': ['laundry', 'lndry'],
'health': ['health', 'gym', 'fitness', 'training'],
'transport': ['train', 'subway', 'transport'],
'parking': ['parking'],
'utilities': ['utilities', 'heat water', 'water included']
}
for fname, bow in bows.items() :
x1 = df_train.description.str.lower().apply(lambda x: np.sum([1 for i in bow if i in x]))
x2 = df_train.features.apply(lambda x: np.sum([1 for i in bow if i in ' '.join(x ).lower() ]))
df_train['num_'+fname] =(( x1 + x2)> 0 ).astype(float ).values
x1 = df_test.description.str.lower().apply(lambda x: np.sum([1 for i in bow if i in x]))
x2 = df_test.features.apply(lambda x: np.sum([1 for i in bow if i in ' '.join(x ).lower() ]))
df_test['num_'+fname] =(( x1 + x2)> 0 ).astype(float ).values
return df_train, df_test<data_type_conversions>
train_and_pred(train_features, test_features, train_labels, test_data,
num_epochs, lr, weight_decay, batch_size )
df = pd.read_json('.. /input/train.json')
 df_test = pd.read_json('.. /input/test.json')
 df['created'] = pd.to_datetime(df.created)
 df_test['created'] = pd.to_datetime(df_test.created )<feature_engineering>
def get_net() :
net = nn.Sequential(nn.Linear(in_features, 256), nn.ReLU() ,nn.Linear(256,64), nn.ReLU() , nn.Linear(64,out_features))
return net
net = get_net()
net
<train_on_grid><EOS> 
train_and_pred(train_features, test_features, train_labels, test_data,
num_epochs, lr, weight_decay, batch_size )