text_plain_output_1.png
from sklearn.ensemble import RandomForestRegressor

from sklearn.metrics import mean_absolute_error

from sklearn.metrics import mean_absolute_error

from sklearn.metrics import mean_absolute_error

from sklearn.preprocessing import Imputer

import pandas as pd

import pandas as pd
main_file_path = '../input/train.csv'
df = pd.read_csv(main_file_path)

price = df.SalePrice

from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_absolute_error
forest_model = RandomForestRegressor()
forest_model.fit(train_X, train_y)
preds = forest_model.predict(val_X)

cols_with_missing = [col for col in df.columns if df[col].isnull().any()]
cols_with_missing

def score_dataset(Xtrain, Xtest, ytrain, ytest):
    forest_model = RandomForestRegressor()
    forest_model.fit(Xtrain, ytrain)
    preds = forest_model.predict(Xtest)
    return mean_absolute_error(ytest, preds)

cols_with_missing = [col for col in X_train.columns if X_train[col].isnull().any()]
reduced_X_train = X_train.drop(cols_with_missing, axis=1)
reduced_X_test = X_test.drop(cols_with_missing, axis=1)

from sklearn.preprocessing import Imputer
my_imputer = Imputer()
imputed_X_train = my_imputer.fit_transform(X_train)
imputed_X_test = my_imputer.transform(X_test)

imputed_X_train_plus = X_train.copy()
imputed_X_test_plus = X_test.copy()
cols_with_missing = (col for col in X_train.columns if X_train[col].isnull().any())
for col in cols_with_missing:
    imputed_X_train_plus[col + '_was_missing'] = imputed_X_train_plus[col].isnull()
    imputed_X_test_plus[col + '_was_missing'] = imputed_X_test_plus[col].isnull()
my_imputer = Imputer()
imputed_X_train_plus = my_imputer.fit_transform(imputed_X_train_plus)
imputed_X_test_plus = my_imputer.transform(imputed_X_test_plus)
print('Mean Absolute Error from Imputation while Track What Was Imputed:')
print(score_dataset(imputed_X_train_plus, imputed_X_test_plus, y_train, y_test))