image_output_1.png
import numpy as np # linear algebra

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

train = pd.read_csv('../input/train.csv')
test = pd.read_csv('../input/test.csv')

train.reset_index(drop=True, inplace=True)
train['SalePrice'] = np.log1p(train['SalePrice'])
y = train['SalePrice'].reset_index(drop=True)

train = train.drop(['Id', 'SalePrice'], axis=1)
test = test.drop(['Id'], axis=1)
x = pd.concat([train, test]).reset_index(drop=True)

x['MSSubClass'] = x['MSSubClass'].apply(str)
x['YrSold'] = x['YrSold'].astype(str)
x['MoSold'] = x['MoSold'].astype(str)
x['Functional'] = x['Functional'].fillna('Typ')
x['Electrical'] = x['Electrical'].fillna('SBrkr')
x['KitchenQual'] = x['KitchenQual'].fillna('TA')
x['Exterior1st'] = x['Exterior1st'].fillna(x['Exterior1st'].mode()[0])
x['Exterior2nd'] = x['Exterior2nd'].fillna(x['Exterior2nd'].mode()[0])
x['SaleType'] = x['SaleType'].fillna(x['SaleType'].mode()[0])
for col in ('GarageYrBlt', 'GarageArea', 'GarageCars'):
    x[col] = x[col].fillna(0)
for col in ['GarageType', 'GarageFinish', 'GarageQual', 'GarageCond']:
    x[col] = x[col].fillna('None')
for col in ('BsmtFinSF1', 'BsmtFinSF2', 'BsmtUnfSF', 'TotalBsmtSF', 'BsmtFullBath', 'BsmtHalfBath'):
    x[col] = x[col].fillna(0)
for col in ('BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2'):
    x[col] = x[col].fillna('None')
objects = []
for i in x.columns:
    if x[i].dtype == object:
        objects.append(i)
x.update(x[objects].fillna('None'))
numeric_dtypes = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']
numerics = []
for i in x.columns:
    if x[i].dtype in numeric_dtypes:
        numerics.append(i)
x.update(x[numerics].fillna(0))

x['total_sf'] = x['TotalBsmtSF'] + x['BsmtFinSF1'] + x['BsmtFinSF2'] + x['1stFlrSF'] + x['2ndFlrSF']
x['total_bathrooms'] = x['FullBath'] + 0.5 * x['HalfBath'] + x['BsmtFullBath'] + 0.5 * x['BsmtHalfBath']
x['total_porch_sf'] = x['OpenPorchSF'] + x['3SsnPorch'] + x['EnclosedPorch'] + x['ScreenPorch'] + x['WoodDeckSF']
x['hasPool'] = x['PoolArea'].apply(lambda x: 1 if x > 0 else 0)
x['has2ndFloor'] = x['2ndFlrSF'].apply(lambda x: 1 if x > 0 else 0)
x['hasGarage'] = x['GarageArea'].apply(lambda x: 1 if x > 0 else 0)
x['hasBasement'] = x['TotalBsmtSF'].apply(lambda x: 1 if x > 0 else 0)
x['hasFireplace'] = x['Fireplaces'].apply(lambda x: 1 if x > 0 else 0)
x.describe()