`L i dZddlmZmZddlZddlZddlZddlm Z ddl m Z ddl m Z mZmZddlmZmZddlmZmZmZmZmZmZmZmZdd lmZdd lmZm Z dd l!m"Z"m#Z#m$Z$dd l%m&Z&m'Z'dd l(m)Z)ddl*m+Z+m,Z,ddl-m.Z.ddl/m0Z0m1Z1ddl2m3Z3m4Z4m5Z5m6Z6m7Z7ddl8m9Z9m:Z:ddl;mZ>m?Z?ddl@mAZAmBZBeZgd?Zhd@ZidAZjdBZkdCZldDZmdEZndFZoejjdGeedHIdfeedHIdfeedfee1dfgdJZpe dKejjdLeedHMdHNeedHMdHNgdOZqejjdPe5dQdQfe4dRdSfe3dRdQfge dKdTZrejjdLeedHMdHNeedHMdHNgdUZsy)WzE Testing for the bagging ensemble module (sklearn.ensemble.bagging). )cycleproductN)config_context) BaseEstimator) load_diabetes load_irismake_hastie_10_2)DummyClassifierDummyRegressor)AdaBoostClassifierAdaBoostRegressorBaggingClassifierBaggingRegressorHistGradientBoostingClassifierHistGradientBoostingRegressorRandomForestClassifierRandomForestRegressor) SelectKBest)LogisticRegression Perceptron) GridSearchCV ParameterGridtrain_test_split)KNeighborsClassifierKNeighborsRegressor) make_pipeline)FunctionTransformerscale)SparseRandomProjection)SVCSVR)"ConsumingClassifierWithOnlyPredict)ConsumingClassifierWithoutPredictLogProba&ConsumingClassifierWithoutPredictProba _Registrycheck_recorded_metadata)DecisionTreeClassifierDecisionTreeRegressor)check_random_state)assert_array_almost_equalassert_array_equal)CSC_CONTAINERSCSR_CONTAINERSctd}ttjtj|\}}}}t ddgddgddgddgd }dt td td ttg}t|t|D]3\}}td||d d|j||j|5y)Nr random_state??TF max_samples max_features bootstrapbootstrap_featuresmax_iter) max_depth) estimatorr0 n_estimators)r)ririsdatatargetrr rr'rr ziprrfitpredict) rngX_trainX_testy_trainy_testgrid estimatorsparamsr?s i/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sklearn/ensemble/tests/test_bagging.pytest_classificationrQAs Q C'7 4;;S($GVWf :F#'-   D B+  J!uZ'890     #gw  0z sparse_container, params, methodr1r=Tr5r2r4Fr7r8r9r6r8r9)rG predict_probapredict_log_probadecision_functionclGddt}td}tttj tj |\}}}}||} ||} td |dddd |j| |} t| || } td |dddd |j||} t| ||}t| |t| }| jDcgc]}|j}}t|Dcgc]}||k( c}sJycc}wcc}w) Nc"eZdZdZfdZxZS)-test_sparse_classification..CustomSVC7SVC variant that records the nature of the training setcHt|||t||_|SNsuperrFtype data_type_selfXy __class__s rPrFz1test_sparse_classification..CustomSVC.fit! GK1 "1gDOKrR__name__ __module__ __qualname____doc__rF __classcell__rfs@rP CustomSVCrZ|E  rRrorr/linearovr)kerneldecision_function_shaper3r?r0rA)r r)rrrBrCrDrrFgetattrr*r` estimators_raall)sparse_containerrOmethodrorHrIrJrKrLX_train_sparse X_test_sparsesparse_classifiersparse_resultsdense_classifier dense_results sparse_typeitypests rPtest_sparse_classificationrbsK4C Q C'7 dii$++C($GVWf&g.N$V,M)8UK  c.'"  8W.7 FN)8UK  c'7  6G,f5f=Mnm<~&K#4#@#@ AaQ\\ AE A %0Q[ 0 11 1 B0s 8D, D1cttd}ttjddtjdd|\}}}}t ddgddgddgddgd}dt tttfD]6}|D]/}td ||d |j||j|18y) Nr2r/r1r2TFr5rurA) r)rdiabetesrCrDrr r(rr!rrFrG)rHrIrJrKrLrMr?rOs rPtest_regressionrs Q C'7 crHOOCR0s($GVWf : #J#'-   D      F  Mys Mf M Q Q gfo  rRryctd}ttjddtjdd|\}}}}Gddt }ddddd d d ddd dd dd ddd dg}||}||} |D]} t d|dd| j||} | j| } t d|dd| j||j|} t|}| jDcgc]}|j}}t| | t|Dcgc]}||k( c}sJt| | ycc}wcc}w)Nrrr/c"eZdZdZfdZxZS))test_sparse_regression..CustomSVRr[cHt|||t||_|Sr]r^rbs rPrFz-test_sparse_regression..CustomSVR.fitrgrRrhrns@rP CustomSVRrrprRrr1r=Tr5r2r4FrSrTr3rurA)r)rrrCrDr!rrFrGr`rwrar*rx)ryrHrIrJrKrLrparameter_setsr{r|rOr}r~rrrrrs rPtest_sparse_regressionrs Q C'7 crHOOCR0s($GVWfC"&  "&  dK$eLN"&g.N$V,M A, k 5; #ng & +22=A  My{ Mf M S' " WV_  >* '8'D'DE!EE!.-@e4A$4555!.-@'AF5s <E & E ceZdZdZdZy)DummySizeEstimatorc`|jd|_tj||_yNr)shapetraining_size_joblibhashtraining_hash_rcrdres rPrFzDummySizeEstimator.fits"ggaj$kk!nrRcFtj|jdSr)nponesrrcrds rPrGzDummySizeEstimator.predictswwqwwqz""rRNrirjrkrFrGrArRrPrrs -#rRrc td}ttjtj|\}}}}t j ||}tt dd|j ||}|j|||j||k(sJtt dd|j ||}|j|||j||kDsJttdj ||}g}|jD];}|j|jdk(sJ|j|j=tt!|t|k(sJy)Nrr/r2F)r?r6r8r0T)r?r8)r)rrrCrDr(rFrscorerrwrrappendrlenset)rHrIrJrKrLr?ensemble training_hashs rPtest_bootstrap_samplesrsw Q C'7 xS($GVWf&'++GW=I ')   c'7   ??7G ,w0P PP P ')   c'7   ??7G ,x~~gw/O OO O  *<*>$OSSHM))7 ''7==+;;;;Y5567 s=! "c-&8 88 8rRc`td}ttjtj|\}}}}t t dd|j||}|jD]D}tjjdtj|jdk(rDJt t dd|j||}|jD]D}tjjdtj|jdkDrDJy)Nrr/r2F)r?r7r9r0r3T) r)rrrCrDrr(rFestimators_features_rrunique)rHrIrJrKrLrfeaturess rPtest_bootstrap_featuresr*s Q C'7 xS($GVWf ')    c'7  11F}}""1%8)<)B)B1)EEEEF ')   c'7  11E}}""1% ((;(A(A!(DDDDErRc td}ttjtj|\}}}}t j dd5tt|j||}tt j|j|dt jt|t|j|t j|j!|tt#|d j||}tt j|j|dt jt|t|j|t j|j!|dddy#1swYyxYw) Nrr/ignore)divideinvalidrur3)axis)r?r0r6)r)rrBrCrDrerrstaterr'rFr*sumrUrrexprVrrHrIrJrKrLrs rPtest_probabilityrFsY Q C'7 4;;S($GVWf Hh 7 $,.S #gw   " FF8))&1 :BGGCK#C,,C5 ctd}ttjtj|\}}}}t t ddd|j||}|j||}t||jz dksJd}tjt|5t t d dd|}|j||dddy#1swYyxYw) Nrr/rTrrrrr3)r)rrrCrDrr(rFrrrrrr) rHrIrJrKrLrrrregrs rPtest_oob_score_regressionrs Q C'7 xS($GVWf ')    c'7 66*J zCNN* +c 11 1 F  k 2#+-   '"###s /+C##C,cPtd}ttjtj|\}}}}t t ddd|j||}t j||}t|j||j|y)Nrr/r3F)r?r@r8r9r0) r)rrrCrDrrrFr*rG)rHrIrJrKrLclf1clf2s rPtest_single_estimatorrs Q C'7 xS($GVWf %'     c'7    $ $Wg 6Ddll62DLL4HIrRctjtj}}t}t t |j ||drJy)NrW)rBrCrDr'hasattrrrF)rdrebases rP test_errorrsB 99dkkqA ! #D(.221a8:MNN NNrRcttjtjd\}}}}t t ddj ||}|j|}|jd|j|}t||t t ddj ||}|j|}t||t tdddj ||}|j|}|jd|j|} t|| t tdddj ||}|j|} t|| y) Nrr/n_jobsr0r3rrr)rt) rrBrCrDrr'rFrU set_paramsr*r rW) rIrJrKrLry1y2y3 decisions1 decisions2 decisions3s rPtest_parallel_classificationrsj'7 4;;Q($GVWf!  c'7    'B q!    'Bb"%   c'7     'Bb"%! E*11 c'7 ++F3J q!++F3Jj*5  E*11 c'7 ++F3Jj*5rRctd}ttjtj|\}}}}t t ddj||}|jd|j|}|jd|j|}t||t t ddj||}|j|}t||y)Nrr/rrr3rr=) r)rrrCrDrr(rFrrGr*) rHrIrJrKrLrrrrs rPtest_parallel_regressionrs Q C'7 xS($GVWf 5 7PQRVVH q!   & !B q!   & !Bb"% 5 7PQRVVH   & !Bb"%rRctjtj}}d||dk(<ddd}tt t |dj ||y)Nr3r=)r3r=)r@ estimator__Croc_auc)scoring)rBrCrDrrr rF)rdre parameterss rPtest_gridsearchrsP 99dkkqAAa1fI#)&AJ"35):yIMMaQRSrRctd}ttjtj|\}}}}t dddj ||}t|jtsJt tddj ||}t|jtsJt tddj ||}t|jtsJttjtj|\}}}}tdddj ||}t|jtsJttddj ||}t|jtsJttddj ||}t|jtsJy)Nrr/rr)r)rrBrCrDrrF isinstance estimator_r'rrrr(r!rs rPtest_estimatorrs Q C(8 4;;S($GVWf!aa@DDWgVH h))+A BB B   c'7  h))+A BB B aaHLLH h)): 66 6(8 xS($GVWf QQ?CCGWUH h))+@ AA A 5 7PQRVVH h))+@ AA Aaa@DDWgVH h))3 // /rRctttdtd}|j t j t jt|djddjtsJy)Nr3)kr=)r7r) rrrr'rFrBrCrDrstepsr0intr?s rPtest_bagging_with_pipelinerHsd!kA&(>(@APQIMM$))T[[) il((,Q/<rRrc tt}td}|jtj tj jtj tjt5|jtj tj |jdtj jddddy#1swYyxYw)Nr )size) sample_weight) rrr)rFrBrCrDrGrraises ValueErrorrandintr)r?rHs rP1test_bagging_sample_weight_unsupported_but_passedrYs!"4"67I Q C MM$))T[[)11$))< z "  II KK++b 0B+D     s AC))C2ctdd\}}d}dD]G}|t||d}n|j||j||t ||k(rGJtd|d }|j||t |Dcgc]}|j c}t |Dcgc]}|j c}k(sJycc}wcc}w) Nr:r3 n_samplesr0)rrT)r@r0 warm_startr@rF)r rrrFrrr0)r0rdreclf_wsr@ clf_no_wstrees rPtest_warm_startr fs bq 9DAq F+ >&) QUF   <  8 1a6{l***+"luIMM!Q f5d!!5 6#'01t  1;  51s C *C ctdd\}}tdd}|j|||jdt j t 5|j||dddy#1swYyxYw) Nr:r3rrT)r@rr4r)r rrFrrrrrdrers rP$test_warm_start_smaller_n_estimatorsrsg bq 9DAq t s- t$AAr{{1~o HrRc tjgdgddtjdgdtjdgdtj dgg}tjgdtjgdgdgdgdgdgg}|D]}t }t t t|}|j||j|t|}|j||j|}|j|jk(sJt }t |}tjt5|j||dddt|}tjt5|j||dddy#1swYLxYw#1swY1xYw)Nr3rrr=Nr=rK)r=rrrr)r=r3 )rrK)rarraynaninfr(rrrGrFrGrrrrr)rdy_valuesre regressorpipelinebagging_regressory_hats rP*test_bagging_regressor_with_missing_inputsrVDs    N N O   A !     H()+  !4W!=yI Q""1%,X6!%%a+33A6ww%++%%%*+  + ]]: &  LLA  ,X6 ]]: & (  ! !!Q ' ( ((   ( (s F+ F7+F4 7G c ttjgdgddtjdgdtjdgdtj dgg}tjgd}t }t t t|}|j||j|t|}|j|||j|}|j|jk(sJ|j||j|t }t |}tjt 5|j||dddt|}tjt 5|j||dddy#1swYIxYw#1swYyxYw)NrIrJr=rK)rrKrKrKrK)rrNrOrPr'rrrGrFrGrrrVrUrrr)rdre classifierrSbagging_classifierrUs rP+test_bagging_classifier_with_missing_inputsrZmsh    N N O   A !A')J09:FH LLAq!*841a  & &q )E 77ekk !! !((+$$Q'()JZ(H z " Q*84 z "%q!$%%%%sF"F."F+.F7ctjddgddgg}tjddg}ttdd}|j ||y)Nr3r=rr4rg333333?)r7r0)rrNrrrFrs rPtest_bagging_small_max_featuresr\sR 1a&1a&!"A !QA 2 43UVWG KK1rRcXtjj|}|jdd}tjd}Gddt }t |dd}|j||t|jdj|jdy)N r4ceZdZdZdZy)8test_bagging_get_estimators_indices..MyEstimatorz7An estimator which stores y indices information at fit.c||_yr])_sample_indicesrs rPrFz.MyEstimator.fits #$D rRN)rirjrkrlrFrArRrP MyEstimatorr`s E %rRrcr3r)r?r@r0) rrandom RandomStaterandnaranger(rrFr+rwrbr")global_random_seedrHrdrercrs rP#test_bagging_get_estimators_indicesris ))   2 3C "aA " A%+% []QR SCGGAqMsq)993;R;RST;UVrRzbagging, expected_allow_nanr3r;cV|jjj|k(sJy)z*Check that bagging inherits allow_nan tag.N)__sklearn_tags__ input_tags allow_nan)rexpected_allow_nans rPtest_bagging_allow_nan_tagros(  # # % 0 0 : :>P PP PrR)enable_metadata_routingmodelr)r?r@c`|jtjtjy)zAMake sure that metadata routing works with non-default estimator.NrFrBrCrDrqs rP"test_bagging_with_metadata_routingrus IIdii%rRzsub_estimator, caller, calleerGrVrUctjddgddgddgg}gd}dgd}}t}||}d |zd z} t|| d d t | } | j ||t| |tjddgddgddgg||t |sJ|D]}t|||||y)aTest that metadata routing works in `BaggingClassifier` with dynamic selection of the sub-estimator's methods. Here we test only specific test cases, where sub-estimator methods are not present and are not tested with `ConsumingClassifier` (which possesses all the methods) in sklearn/tests/test_metaestimators_metadata_routing.py: `BaggingClassifier.predict()` dynamically routes to `predict` if the sub-estimator doesn't have `predict_proba` and `BaggingClassifier.predict_log_proba()` dynamically routes to `predict_proba` if the sub-estimator doesn't have `predict_log_proba`, or to `predict`, if it doesn't have it. rr=r3r4rK)r3r=ra)registryset__requestT)rmetadatarr)rdrr{)objrzparentrr{N)rrNr%rvrrFrr&) sub_estimatorcallercalleerdrerr{rxr?set_callee_requestrs rP3test_metadata_routing_with_dynamic_method_selectionrs0 1a&1a&1a&)*AA c38M{Hx0I&:5*GI)*M)4G KK1GGV ((QFQFQF+ ,# x== '   rRc`|jtjtjy)z^Make sure that we still can use an estimator that does not implement the metadata routing.Nrsrts rP-test_bagging_without_support_metadata_routingrs IIdii%rR)*)trl itertoolsrrrnumpyrrsklearnr sklearn.basersklearn.datasetsrrr sklearn.dummyr r sklearn.ensembler r rrrrrrsklearn.feature_selectionrsklearn.linear_modelrrsklearn.model_selectionrrrsklearn.neighborsrrsklearn.pipelinersklearn.preprocessingrrsklearn.random_projectionr sklearn.svmr r!%sklearn.tests.metadata_routing_commonr"r#r$r%r& sklearn.treer'r( sklearn.utilsr)sklearn.utils._testingr*r+sklearn.utils.fixesr,r-rHrB permutationrDrpermrCrrQmark parametrizerrrrrrrrrrrrrrrrrrr rrrrrr r,r4r7rBrGrVrZr\rirorurrrArRrPrs%  "&GG9   2?QQG*<< G,P>{ t{{''( IIdO kk$  ? x++, d# //$'0B& ' # !!&*   # !!&*   U$ Od% P O%.&2/.&2R8+^n-LM5AN5Ap##'9TE8  F"&J!#HJ(O&6R&4 T)0XC>>  44$&& # H%5PN6 / 4 &(R%@W*! 91E FM 7C DdK -/ 0%8 #% %( QQ- ,!<1  +;!  & .& # /IF 5   ,-@)L -# . # T (a8  #4!#DSTU  & &rR