Filling Missing values using Imputator

Imputation fills in the missing values with some number. For instance, we can fill in the mean value along each column.
The imputed value won't be exactly right in most cases, but it usually leads to more accurate models than you would get from dropping the column entirely.

pandas:
Pandas Dataframe
Functions: read_csv(), select_dtypes()
scikit-learn:
sklearn?
Functions: train_test_split(), RandomForestRegressor()
Terms: axis

# melb_data.csv
'Suburb', 'Price', 'Address', 'Rooms', 'Type', 'Method', 'SellerG', 'Date', ...
1480000, 1035000, 1465000, 850000, 1600000, ...

# test.py
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_absolute_error
from sklearn.impute import SimpleImputer

# Machine Learning\Libraries\Pandas\Functions.html\read_csv
data_frame = pd.read_csv('../input/melbourne-housing-snapshot/melb_data.csv')

# Copy Price Coloumn into y, that is what we want to predict(house prices).
y_dataframe_price = data_frame.Price

# Machine Learning\Libraries\Pandas\Functions.html#drop
melb_predictors = data_frame.drop(['Price'], axis=1)

# Machine Learning\Libraries\Pandas\Functions.html#select_dtypes
X_dataframe_without_objects = melb_predictors.select_dtypes(exclude=['object'])


# Machine Learning\Libraries\scikit-learn\Functions.html#train_test_split
X_train, X_valid, y_train, y_valid = train_test_split(
    X_dataframe_without_objects, y_dataframe_price, train_size=0.8, test_size=0.2, random_state=0
)

# # Machine Learning\Libraries\scikit-learn\Functions.html#SimpleImputer
my_imputer = SimpleImputer()

# Machine Learning\Libraries\scikit-learn\Functions.html#SimpleImputer
imputed_X_train = pd.DataFrame(my_imputer.fit_transform(X_train))
imputed_X_valid = pd.DataFrame(my_imputer.transform(X_valid))

# Restore the original column names.
# After imputation, pandas columns are lost because the output becomes a plain array.
imputed_X_train.columns = X_train.columns
imputed_X_valid.columns = X_valid.columns

# Function to train and evaluate a model.
def score_dataset(X_train, X_valid, y_train, y_valid):
    # Create a Random Forest Regressor. n_estimators=10 means the model uses 10 decision trees.
    randomforest_model = RandomForestRegressor(n_estimators=10, random_state=0)

    # Machine Learning\Libraries\scikit-learn\Functions.html#fit
    randomforest_model.fit(X_train, y_train)

    # Machine Learning\Libraries\scikit-learn\Functions.html#predict
    preds = randomforest_model.predict(X_valid)

    # Machine Learning\Libraries\scikit-learn\Functions.html#mean_absolute_error
    return mean_absolute_error(y_valid, preds)

print("MAE from Approach 2 (Imputation):")
print(score_dataset(imputed_X_train, imputed_X_valid, y_train, y_valid))