@@ -367,14 +367,13 @@ def check_indicator(X, expected_imputed_features, axis):
367
367
imputer_with_in = clone (imputer ).set_params (add_indicator_features = True )
368
368
Xt = imputer .fit_transform (X )
369
369
Xt_with_in = imputer_with_in .fit_transform (X )
370
- imputed_features_mask = X [:, expected_imputed_features ] == - 1
370
+ imputed_features_mask = X == - 1
371
371
n_features_new = Xt .shape [1 ]
372
- n_imputed_features = len (imputer_with_in .imputed_features_ )
373
- assert_array_equal (imputer .imputed_features_ , expected_imputed_features )
374
- assert_array_equal (imputer_with_in .imputed_features_ ,
375
- expected_imputed_features )
376
- assert_equal (Xt_with_in .shape ,
377
- (n_samples , n_features_new + n_imputed_features ))
372
+ if axis == 0 :
373
+ assert_array_equal (imputer .imputed_features_ , expected_imputed_features )
374
+ assert_array_equal (imputer_with_in .imputed_features_ ,
375
+ expected_imputed_features )
376
+ assert_equal (Xt_with_in .shape , (n_samples , n_features + n_features_new ))
378
377
assert_array_equal (Xt_with_in , np .hstack ((Xt , imputed_features_mask )))
379
378
imputer_with_in = clone (imputer ).set_params (add_indicator_features = True )
380
379
assert_array_equal (Xt_with_in ,
@@ -406,3 +405,55 @@ def test_indicator_features():
406
405
])
407
406
check_indicator (X , np .array ([0 , 3 ]), axis = 0 )
408
407
check_indicator (X , np .array ([0 , 1 , 3 ]), axis = 1 )
408
+
409
+ def test_indicator_features_shape ():
410
+
411
+ X1 = np .array ([
412
+ [- 1 , - 1 , 2 , 3 ],
413
+ [4 , - 1 , 6 , - 1 ],
414
+ [8 , - 1 , 10 , 11 ],
415
+ [12 , - 1 , - 1 , 15 ],
416
+ [16 , - 1 , 18 , 19 ]
417
+ ])
418
+
419
+ X2 = np .array ([
420
+ [- 1 , - 1 , 1 , 3 ],
421
+ [4 , - 1 , 0 , - 1 ],
422
+ [8 , - 1 , 1 , 0 ],
423
+ [0 , - 1 , 0 , 15 ],
424
+ [16 , - 1 , 1 , 19 ]
425
+ ])
426
+
427
+ n_samples , n_features = X1 .shape
428
+ imputer = Imputer (missing_values = - 1 , strategy = 'mean' , axis = 0 )
429
+ imputer_with_in = clone (imputer ).set_params (add_indicator_features = True )
430
+ imputer .fit (X1 )
431
+ imputer_with_in .fit (X1 )
432
+ Xt = imputer .transform (X2 )
433
+ Xt_with_in = imputer_with_in .transform (X2 )
434
+ imputed_features_mask = X1 == - 1
435
+ n_features_new = Xt .shape [1 ]
436
+ # assert_array_equal(imputer.imputed_features_, expected_imputed_features)
437
+ # assert_array_equal(imputer_with_in.imputed_features_,
438
+ # expected_imputed_features)
439
+ assert_equal (Xt_with_in .shape , (n_samples , n_features + n_features_new ))
440
+ assert_array_equal (Xt_with_in , np .hstack ((Xt , imputed_features_mask )))
441
+ assert_array_equal (Xt_with_in ,
442
+ imputer_with_in .transform (sparse .csc_matrix (X2 )).A )
443
+ assert_array_equal (Xt_with_in ,
444
+ imputer_with_in .transform (sparse .csr_matrix (X2 )).A )
445
+
446
+ imputer = Imputer (missing_values = - 1 , strategy = 'mean' , axis = 1 )
447
+ imputer_with_in = clone (imputer ).set_params (add_indicator_features = True )
448
+ imputer .fit (X1 )
449
+ imputer_with_in .fit (X1 )
450
+ Xt = imputer .transform (X2 )
451
+ Xt_with_in = imputer_with_in .transform (X2 )
452
+ imputed_features_mask = X2 == - 1
453
+ n_features_new = Xt .shape [1 ]
454
+ assert_equal (Xt_with_in .shape , (n_samples , n_features + n_features_new ))
455
+ assert_array_equal (Xt_with_in , np .hstack ((Xt , imputed_features_mask )))
456
+ assert_array_equal (Xt_with_in ,
457
+ imputer_with_in .transform (sparse .csc_matrix (X2 )).A )
458
+ assert_array_equal (Xt_with_in ,
459
+ imputer_with_in .transform (sparse .csr_matrix (X2 )).A )
0 commit comments