Module Expr Str Import alias identifier ImportFrom alias ImportFrom alias ImportFrom alias ImportFrom alias ImportFrom alias Assign NameStore Call AttributeLoad AttributeLoad NameLoad attr attr Num Assign NameStore Call NameLoad Assign TupleStore NameStore NameStore TupleLoad AttributeLoad NameLoad attr AttributeLoad NameLoad attr Assign NameStore SubscriptLoad AttributeLoad NameLoad attr Index Num Assign NameStore SubscriptLoad AttributeLoad NameLoad attr Index Num Assign NameStore Call NameLoad keyword Num keyword Num Assign NameStore Call AttributeLoad Call NameLoad NameLoad NameLoad NameLoad attr Print BinOpMod Str NameLoad Assign NameStore Num Assign NameStore Call NameLoad BinOpMult NameLoad NameLoad Assign NameStore Call AttributeLoad NameLoad attr TupleLoad Call AttributeLoad NameLoad attr BinOpSub NameLoad NameLoad keyword AttributeLoad NameLoad attr Call AttributeLoad NameLoad attr NameLoad keyword AttributeLoad NameLoad attr Expr Call AttributeLoad NameLoad attr NameLoad Assign NameStore Call AttributeLoad NameLoad attr Num NameLoad NameLoad Assign NameStore SubscriptLoad NameLoad ExtSlice Index UnaryOpInvert NameLoad Slice Assign NameStore SubscriptLoad NameLoad Index UnaryOpInvert NameLoad Assign NameStore Call NameLoad keyword Num keyword Num Assign NameStore Call AttributeLoad Call NameLoad NameLoad NameLoad NameLoad attr Print BinOpMod Str NameLoad Assign NameStore Call AttributeLoad NameLoad attr Assign SubscriptStore NameLoad Index TupleLoad SubscriptLoad Call AttributeLoad NameLoad attr NameLoad Index Num NameLoad Num Assign NameStore Call AttributeLoad NameLoad attr Assign NameStore Call NameLoad ListLoad TupleLoad Str Call NameLoad keyword Num keyword Str keyword Num TupleLoad Str Call NameLoad keyword Num keyword Num Assign NameStore Call AttributeLoad Call NameLoad NameLoad NameLoad NameLoad attr Print BinOpMod Str NameLoad Assign NameStore Call NameLoad ListLoad TupleLoad Str Call NameLoad keyword Num keyword Str keyword Num keyword NameLoad TupleLoad Str Call NameLoad keyword Num keyword Num Assign NameStore Call AttributeLoad Call NameLoad NameLoad NameLoad NameLoad attr Print BinOpMod Str NameLoad
N/A N/A 
======================================================
Imputing missing values before building an estimator
======================================================

This example shows that imputing the missing values can give better results
than discarding the samples containing any missing value.
Imputing does not always improve the predictions, so please check via cross-validation.
Sometimes dropping rows or using marker values is more effective.

In this example, we artificially mark some of the elements in complete
dataset as missing. Then we estimate performance using the complete dataset,
dataset without the missing samples, after imputation without the indicator
matrix and imputation with the indicator matrix for the missing values.

Missing values can be replaced by the mean, the median or the most frequent
value using the ``strategy`` hyper-parameter.
The median is a more robust estimator for data with high magnitude variables
which could dominate results (otherwise known as a 'long tail').

Script output::

  Score with the complete dataset = 0.56
  Score without the samples containing missing values = 0.48
  Score after imputation of the missing values = 0.55
  Score after imputation with indicator features = 0.57

In this case, imputing helps the classifier get close to the original score.
  
 N/A numpy np sklearn.datasets load_boston sklearn.ensemble RandomForestRegressor sklearn.pipeline Pipeline sklearn.preprocessing Imputer sklearn.model_selection cross_val_score N/A rng N/A N/A N/A np random RandomState 0 N/A dataset N/A load_boston N/A N/A X_full y_full N/A N/A dataset data N/A dataset target N/A n_samples N/A N/A X_full shape N/A 0 N/A n_features N/A N/A X_full shape N/A 1 N/A estimator N/A RandomForestRegressor random_state 0 n_estimators 100 N/A score N/A N/A N/A cross_val_score estimator X_full y_full mean N/A N/A Score with the complete dataset = %.2f score N/A missing_rate 0.75 N/A n_missing_samples N/A int N/A n_samples missing_rate N/A missing_samples N/A N/A np hstack N/A N/A N/A np zeros N/A n_samples n_missing_samples dtype N/A np bool N/A N/A np ones n_missing_samples dtype N/A np bool N/A N/A N/A rng shuffle missing_samples N/A missing_features N/A N/A rng randint 0 n_features n_missing_samples N/A X_filtered N/A X_full N/A N/A N/A missing_samples N/A N/A y_filtered N/A y_full N/A N/A missing_samples N/A estimator N/A RandomForestRegressor random_state 0 n_estimators 100 N/A score N/A N/A N/A cross_val_score estimator X_filtered y_filtered mean N/A N/A Score without the samples containing missing values = %.2f score N/A X_missing N/A N/A X_full copy N/A N/A X_missing N/A N/A N/A N/A N/A np where missing_samples N/A 0 missing_features 0 N/A y_missing N/A N/A y_full copy N/A estimator N/A Pipeline N/A N/A imputer N/A Imputer missing_values 0 strategy mean axis 0 N/A forest N/A RandomForestRegressor random_state 0 n_estimators 100 N/A score N/A N/A N/A cross_val_score estimator X_missing y_missing mean N/A N/A Score after imputation of the missing values = %.2f score N/A estimator N/A Pipeline N/A N/A imputer N/A Imputer missing_values 0 strategy mean axis 0 add_indicator_features True N/A forest N/A RandomForestRegressor random_state 0 n_estimators 100 N/A score N/A N/A N/A cross_val_score estimator X_missing y_missing mean N/A N/A Score after imputation with indicator features = %.2f score