@@ -627,3 +627,42 @@ def test_target_encode_out(self):
627
627
enc .fit (X , y )
628
628
self .verify_numeric (enc .transform (X_t ))
629
629
self .verify_numeric (enc .transform (X_t , y_t ))
630
+
631
+ def test_fit_HaveConstructorSetSmoothingAndMinSamplesLeaf_ExpectUsedInFit (self ):
632
+ """
633
+
634
+ :return:
635
+ """
636
+ k = 2
637
+ f = 10
638
+ binary_cat_example = pd .DataFrame (
639
+ {'Trend' : ['UP' , 'UP' , 'DOWN' , 'FLAT' , 'DOWN' , 'UP' , 'DOWN' , 'FLAT' , 'FLAT' , 'FLAT' ],
640
+ 'target' : [1 , 1 , 0 , 0 , 1 , 0 , 0 , 0 , 1 , 1 ]})
641
+ encoder = encoders .TargetEncoder (cols = ['Trend' ], min_samples_leaf = k , smoothing = f )
642
+
643
+ encoder .fit (binary_cat_example , binary_cat_example ['target' ])
644
+ trend_mapping = encoder .mapping [0 ]['mapping' ]
645
+
646
+ self .assertAlmostEquals (0.4125 , trend_mapping ['DOWN' ]['smoothing' ], delta = 1e-4 )
647
+ self .assertEqual (0.5 , trend_mapping ['FLAT' ]['smoothing' ])
648
+ self .assertAlmostEquals (0.5874 , trend_mapping ['UP' ]['smoothing' ], delta = 1e-4 )
649
+
650
+ def test_fit_transform_HaveConstructorSetSmoothingAndMinSamplesLeaf_ExpectCorrectValueInResult (self ):
651
+ """
652
+
653
+ :return:
654
+ """
655
+ k = 2
656
+ f = 10
657
+ binary_cat_example = pd .DataFrame (
658
+ {'Trend' : ['UP' , 'UP' , 'DOWN' , 'FLAT' , 'DOWN' , 'UP' , 'DOWN' , 'FLAT' , 'FLAT' , 'FLAT' ],
659
+ 'target' : [1 , 1 , 0 , 0 , 1 , 0 , 0 , 0 , 1 , 1 ]})
660
+ encoder = encoders .TargetEncoder (cols = ['Trend' ], min_samples_leaf = k , smoothing = f )
661
+
662
+ result = encoder .fit_transform (binary_cat_example , binary_cat_example ['target' ])
663
+ values = result ['Trend' ].values
664
+
665
+ self .assertAlmostEquals (0.5874 , values [0 ], delta = 1e-4 )
666
+ self .assertAlmostEquals (0.5874 , values [1 ], delta = 1e-4 )
667
+ self .assertAlmostEquals (0.4125 , values [2 ], delta = 1e-4 )
668
+ self .assertEqual (0.5 , values [3 ])
0 commit comments