text_plain_output_1.png
import numpy as np # linear algebra

import pandas as pd

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import torch

import torch

import torch

import torch.nn as nn

import torch.nn as nn

df = pd.read_csv('/kaggle/input/advancehousepriceprediction/train.csv', usecols=['SalePrice', 'MSSubClass', 'MSZoning', 'LotFrontage', 'LotArea', 'Street', 'YearBuilt', 'LotShape', '1stFlrSF', '2ndFlrSF']).dropna()

df.shape

df.drop('YearBuilt', axis=1, inplace=True)

df.columns

cat_features = ['MSSubClass', 'MSZoning', 'Street', 'LotShape']
out_features = 'SalePrice'

cat_features = np.stack([df['MSSubClass'], df['MSZoning'], df['Street'], df['LotShape']], 1)
cat_features

import torch
cat_features = torch.tensor(cat_features, dtype=torch.int64)
cat_features

cont_features = []
for i in df.columns:
    cont_features.append(i)

cont_values = np.stack([df[i].values for i in cont_features], axis=1)
cont_values = torch.tensor(cont_values, dtype=torch.float)
cont_values

cont_values.dtype

y = torch.tensor(df['SalePrice'].values, dtype=torch.float).reshape(-1, 1)
y

(cat_features.shape, cont_values.shape, y.shape)

df.shape

cat_dims = [len(df[col].unique()) for col in ['MSSubClass', 'MSZoning', 'Street', 'LotShape']]

embedding_dim = [(x, min(50, (x + 1) // 2)) for x in cat_dims]

import torch
import torch.nn as nn
import torch.nn.functional as F
embed_representation = nn.ModuleList([nn.Embedding(inp, out) for inp, out in embedding_dim])
embed_representation

pd.set_option('display.max_rows', 500)
embedding_val = []
for i, e in enumerate(embed_representation):
    embedding_val.append(e(cat_features[:, i]))

z = torch.cat(embedding_val, 1)
z

dropout = nn.Dropout(0.4)

import torch
import torch.nn as nn
import torch.nn.functional as F
class FeedForwardNN(nn.Module):
    def __init__(self, embedding_dim, n_cont, out_sz, layers, p=0.5):
        super().__init__()
        self.embeds = nn.ModuleList([nn.Embedding(inp, out) for inp, out in embedding_dim])
        self.emb_drop = nn.Dropout(p)
        self.bn_cont = nn.BatchNorm1d(n_cont)
        layerlist = []
        n_emb = sum((out for inp, out in embedding_dim))
        n_in = n_emb + n_cont
        for i in layers:
            layerlist.append(nn.Linear(n_in, i))
            layerlist.append(nn.ReLU(inplace=True))
            layerlist.append(nn.BatchNorm1d(i))
            layerlist.append(nn.Dropout(p))
            n_in = i
        layerlist.append(nn.Linear(layers[-1], out_sz))
        self.layers = nn.Sequential(*layerlist)
    def forward(self, x_cat, x_cont):
        embeddings = []
        for i, e in enumerate(self.embeds):
            embeddings.append(e(x_cat[:, i]))
        x = torch.cat(embeddings, 1)
        x = self.emb_drop(x)
        x_cont = self.bn_cont(x_cont)
        x = torch.cat([x, x_cont], 1)
        x = self.layers(x)
        return x

torch.manual_seed(100)
model = FeedForwardNN(embedding_dim, len(cont_features), 1, [100, 50], p=0.4)

model