image_output_2.png image_output_1.png
from sklearn.metrics import classification_report, explained_variance_score, r2_score, max_error

from sklearn.model_selection import train_test_split

import matplotlib.pyplot as plt

import numpy as np

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

train = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv', index_col='Id')
test = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv', index_col='Id')

def fill_na_in_data(dataset):
    df = dataset
    for column in df.columns:
        column_type = df[column].dtype
        if column_type == 'int64':
            df[column] = df[column].fillna(0)
        elif column_type == 'object':
            df[column] = df[column].fillna('None')
        elif column_type == 'float64':
            df[column] = df[column].fillna(df[column].median())
    return df
train = fill_na_in_data(train)
test = fill_na_in_data(test)

def map_non_float_values(dataset):
    for cat_column in dataset.dtypes.loc[dataset.dtypes == 'O'].index:
        dataset[cat_column] = dataset[cat_column].astype('category')
        dataset[cat_column + '_cat'] = dataset[cat_column].cat.codes
    return dataset
train = map_non_float_values(train)
test = map_non_float_values(test)

X = train.loc[:, train.dtypes != 'category'].drop('SalePrice', axis=1)
y = train.SalePrice
X_val = test.loc[:, test.dtypes != 'category']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
X_train = torch.Tensor(X_train.values)
X_test = torch.Tensor(X_test.values)
y_train = torch.Tensor(y_train.values)
y_test = torch.Tensor(y_test.values)
X_val = torch.Tensor(X_val.values)

class HouseNet(torch.nn.Module):
    def __init__(self, hidden_neurons):
        super(HouseNet, self).__init__()
        self.fc1 = torch.nn.Linear(79, hidden_neurons)
        self.ac1 = torch.nn.ReLU()
        self.bn1 = torch.nn.BatchNorm1d(hidden_neurons)
        self.fc2 = torch.nn.Linear(hidden_neurons, hidden_neurons)
        self.ac2 = torch.nn.Sigmoid()
        self.bn2 = torch.nn.BatchNorm1d(hidden_neurons)
        self.fc3 = torch.nn.Linear(hidden_neurons, hidden_neurons)
        self.ac3 = torch.nn.Sigmoid()
        self.fc4 = torch.nn.Linear(hidden_neurons, 1)
    def forward(self, x):
        x = self.fc1(x)
        x = self.ac1(x)
        x = self.bn1(x)
        x = self.fc2(x)
        x = self.ac2(x)
        x = self.bn2(x)
        x = self.fc3(x)
        x = self.fc3(x)
        x = self.fc4(x)
        return x

loss = torch.nn.MSELoss()
house_net = HouseNet(50)
optimizer = torch.optim.Adam(house_net.parameters(), lr=0.001, weight_decay=1e-05)

batch_size = 50
r2 = []
test_loss_history = []
val_loss_history_avg = []
for epoch in range(50):
    batch_loss_history = []
    order = np.random.permutation(len(X_train))
    for start_index in range(0, len(X_train), batch_size):
        optimizer.zero_grad()
        house_net.train()
        batch_indexes = order[start_index:start_index + batch_size]
        X_batch = X_train[batch_indexes]
        y_batch = y_train[batch_indexes]
        preds = house_net.forward(X_batch)
        loss_value = loss(preds.view(preds.size()[0]), y_batch)
        batch_loss_history.append(loss_value)
        loss_value.backward()
        optimizer.step()
    house_net.eval()
    val_loss_history_avg.append(sum(batch_loss_history) / len(batch_loss_history))
    test_preds = house_net.forward(X_test)
    test_loss_history.append(loss(test_preds.view(test_preds.size()[0]), y_test))
    r2.append(r2_score(list(y_test), list(test_preds)))

plt.plot(test_loss_history, label='test')
plt.plot(val_loss_history_avg, label='train loss(avg)')
plt.legend()
plt.show()
plt.plot(r2)
plt.show()