o
    ?Hh5                    @   s  d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	 ddl
ZddlZddlmZ ddlmZ ddlZddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZmZ ddlm Z  ddl!m"Z" ddl#m$Z$ ddl%m&Z& ddl'm(Z( ddl)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/ ddl0m1Z1m2Z2 ddl3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z; ddl<m=Z= ddl>m?Z?m@Z@mAZAmBZBmCZCmDZD ddlEmFZFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQmRZRmSZSmTZTmUZUmVZVmWZWmXZXmYZYmZZZm[Z[m\Z\m]Z]m^Z^m_Z_m`Z` dd Zadd Zbejcddejeedefddg gd d! Zhd"d# Zid$d% Zjd&d' Zkejcdd(ejld)fejmd*fejmd)fgejcdd+ejnejogd,d- Zpejcdd.ejld/d0d1fejldd0d2fejld3d0d4fejldd*d2fejmd/d0d5fejmdd0d6fejmd7d0d8fejmd/d9d:fejmd/d;d5fg	ejcdd+ejnejogd<d= Zqejcdd>g d?ejcdd+ejnejogd@dA ZrdBdC ZsejcddDetd;ejmggdEfetd;ejmggdEfetd;ejlggdFfejtd;ejmggeudGdHfgejcddId0d)gdJdK ZvdLdM ZwejcxdNdOdP ZyejcdddQdRgdSdTggejtdQdRgdSdTggdUdGejtdQdRgdSdTggdVdGdWdXgdYdZggejtdWdXgdYdZggd[dGgd\d] Zzejcdd^g d_ejcdd`ej{ej|gej{fej|ej|fdaej|fgdbdc Z}ddde Z~dfdg Zdhdi Zdjdk Zdldm Zdndo Zdpdq Zejg drdsdtdu Zdvdw Zdxdy Zdzd{ Zd|d} Zd~d Zdd Zdd Zdd Zdd Zdd Zejcjddedeegg dddd Zdd Zdd Zdd Zdd Zejcddej|ej{fej|ddafejcddddd Zdd Zdd ZG dd dZG dd dZdd Zejcddd0d)gdd Zejcjdddedddd0idd0dfdedddd0idd0dfdedddd0igdgd0dfdedddd0idgd0dfdedddd0iddefdedddd0iddefgg dddd Zejcdd+ejnejoejejejejejejgdd Zdd Zdd Zejcddg ddd Zejcddd;dedddedăfddejdddedŃfddejdddedƃfd;deefdddedǃfd;dedddedɃfddeddded̃fddedddedσfddeddded҃fddedddedՃfddeddded׃fddedddedكfgddۄ Zdetd;dgdd/fetd;dgetd;dgdd/fdetddgedfdetddged/fejtd;dgej{dGejtd;dgej{dGedfejtd;dgej|dGejtd;dgej|dGedfdetddgedfdZejcjddee ee dejcddd0d)gdd Zdedfdedfdedfejtddgej{dGedfejtddgej|dGedfdZejcjddee ee ddd Zdd Zejcddejtejoejgdd Zejcddejtejoejgdd Zejcddejoejgd d Zdd Zdd Zdd Zejcdddd;d	ggd
d Zejcddg ddd Zejcddg ddd Zejcddddejfddejfddejfddejfddejfddejfd d!ejfd"d!ejfd#d$ejfd%d ejfd&d'ejfd(d)ejfd*d+ejfd,d-ejfd.d/ejfgd0d1 Zejcdd2g d3d4d5 Zejcjddg d6g d7g d8g d9d:d;id;did<d	iggg d=dejcdd2g d>d?d@ Zejcjddg dAg dBddCdD ZejcjddEeededdd:d;gd<dFgggg dGddHdI Z̐dJdK ZejcddLg dMdNdO Zejcdd2g dPdQdR ZϐdSdT ZАdUdV ZejcddLdWedW d fdXedY d fdZedZ d fgd[d\ ZҐd]d^ ZӐd_d` Zejcjddad:d;gdbfdcd:d;ggddfgdedfgddgdh ZG didj djeZ֐dkdl Zאdmdn Zؐdodp Zejcddqg drdsdt Zڐdudv Zېdwdx Zejcddydd;dd;dejmgg dzgd{d| Ze;d}d~ Zejcddg dejcddd0d)gdd Zߐdd ZejcddeAe@ e? eB ejcddg ddd Zejcddetd;etdgd;dgd	dgggdd Zdd Zdd Zdd Zdd Zdd Ze;dd Zdd ZdS (  z$Tests for input validation functions    N)product)
itemgetter)NamedTemporaryFile)importorskip)config_context)dependent_packages)BaseEstimator)
make_blobs)RandomForestRegressor)NotFittedErrorPositiveSpectrumWarning)ARDRegression)EstimatorWithFit)KNeighborsClassifier)_sparse_random_matrix)SVR)_safe_indexingas_float_arraycheck_arraycheck_symmetric	check_X_y
deprecated)MockDataFrame_MockEstimatorOnOffPrediction)SkipTest
TempMemmap_convert_containerassert_allcloseassert_allclose_dense_sparseassert_array_equalcreate_memmap_backed_data'skip_if_array_api_compat_not_configured)_NotAnArray)COO_CONTAINERSCSC_CONTAINERSCSR_CONTAINERSDIA_CONTAINERSDOK_CONTAINERSparse_version)FLOAT_DTYPES_allclose_dense_sparse_check_feature_names_in_check_method_params_check_psd_eigenvalues_check_response_method_check_sample_weight_check_y_deprecate_positional_args_estimator_has_get_feature_names
_is_fitted_is_pandas_df_is_polars_df_num_features_num_samples_to_object_arrayassert_all_finitecheck_consistent_lengthcheck_is_fittedcheck_memorycheck_non_negativecheck_random_statecheck_scalarcolumn_or_1dhas_fit_parametervalidate_datac                  C   s   t d tjjju sJ t tjtjjju sJ tjd} t dd| dks+J tjd} t | | u s9J tjd} t dd| dksMJ tt	 t d W d    d S 1 sbw   Y  d S )N*   d   +   zsome invalid seed)
r?   nprandommtrand_randRandomStaterandintpytestraises
ValueError)rng_42 rQ   c/home/air/sanwanet/gpt-api/venv/lib/python3.10/site-packages/sklearn/utils/tests/test_validation.pytest_make_rng[   s   
"rS   c                  C   s  t jdt jd} | t jdt jd } t| dd}|jt jks J | t j} t| dd}t| dd| us6J |jt j	ks>J t
t jt jt jt jt jt jg}|D ]}| |} t| }|jt jksbJ qO| t} t| dd}|jt j	ksvJ t jdt jd} t| dd| u sJ t | } t t| ddsJ tt d tddd	d
 g}|D ]}t|dd}t j|d< t | rJ qd S )N)   
   dtyperU   FcopyT)rT         皙?)densityr   r   )rG   onesint32aranger   rW   float32astypeint64float64boolint8int16uint8uint16uint32objectasfortranarray	isfortransp
csc_matrixtoarrayr   nanisnanany)XX2tested_dtypesrW   matricesMNrQ   rQ   rR   test_as_float_arraym   s8   



r{   ru   rU   rZ   rU   rZ   c                 C   s.   t j| d< t j| d< t| dd}t||  d S )N)r[   r   )      	allow-nanensure_all_finite)rG   rr   r   r   )ru   X_convertedrQ   rQ   rR   test_as_float_array_nan   s   

r   c                  C   sD   t ddd} tt| t jrJ ttt| t jr J d S )N   rT      )rG   ra   reshape
isinstancer   matrixro   rp   ru   rQ   rQ   rR   test_np_matrix   s   r   c                  C   s   dd } t dd:}tj|dtjd}d|d d < ttj| fD ]}||}d|d d < t| |  d|d d < qW d    d S 1 sFw   Y  d S )	Nc                 S   s   t | ddS )NFrX   )r   )xrQ   rQ   rR   <lambda>   s    ztest_memmap.<locals>.<lambda>zsklearn-test)prefixrU   rU   )shaperW   r   r~   )r   rG   memmaprb   r   asarrayr   ravel)asflttmpry   fru   rQ   rQ   rR   test_memmap   s   "r   c                  C   s   t d} | | jfD ]+}dD ]&}t|d|d}|jd sJ t|d|d}|jd s,J |r4||us4J qq
t| } | jd d d | _| jjd rLJ d S )	N)rU   r[   )TFC)orderrY   C_CONTIGUOUSFF_CONTIGUOUS)rG   r_   Tr   flagsro   
csr_matrixdata)ru   ArY   BrQ   rQ   rR   test_ordering   s   

r   zvalue, ensure_all_finiteFr   retypec                 C   s@   |t dddt}| |d< t||dd}t|| d S )Nr   rZ   r^   T)r   accept_sparse)rG   ra   r   rc   floatr   r   )valuer   r   ru   	X_checkedrQ   rQ   rR   (test_check_array_ensure_all_finite_valid   s   r   z/value, input_name, ensure_all_finite, match_msg TzInput contains infinityzInput X contains infinitysample_weightz%Input sample_weight contains infinityInput contains NaNzInput X contains NaNyzInput y contains NaNz	allow-infz1ensure_all_finite should be a bool or 'allow-nan'r~   c                 C   sj   |t dddt j}| |d< tjt|d t|||dd W d    d S 1 s.w   Y  d S )Nr   rZ   r^   matchT)
input_namer   r   )	rG   ra   r   rc   re   rM   rN   rO   r   )r   r   r   	match_msgr   ru   rQ   rQ   rR   *test_check_array_ensure_all_finite_invalid   s   "r   r   )ru   r   r   c                 C   s*  |t dddt j}t j|d< t }d|jj d}t	j
td|  dd}t||| d	d
 W d    n1 s=w   Y  | dkrQ||jjd v sPJ n
||jjd vs[J | dkrt	j
td|  dd}t |t |jd  W d    n1 sw   Y  ||jjd v sJ d S d S )Nr   rZ   r^   
aX   does not accept missing values encoded as NaN natively. For supervised learning, you might want to consider sklearn.ensemble.HistGradientBoostingClassifier and Regressor which accept missing values encoded as NaNs natively. Alternatively, it is possible to preprocess the data, for instance by using an imputer transformer in a pipeline or drop samples with missing values. See https://scikit-learn.org/stable/modules/impute.html You can find a list of all estimators that handle NaN values at the following page: https://scikit-learn.org/stable/modules/impute.html#estimators-that-handle-nan-valueszInput z contains NaNr   T)	estimatorr   r   ru   r   )rG   ra   r   rc   re   rr   r   	__class____name__rM   rN   rO   r   r   argsfitr_   r   )r   r   r   r   extended_msgctxrQ   rQ   rR   0test_check_array_links_to_imputer_doc_only_for_X   s.   
r   c                  C   s   t jddt jggtdj} t| d dd}| |u sJ t| d dd}| |u s'J tjtdd t| d d	d W d    d S 1 sAw   Y  d S )
NabrV   r   rW   r   Fr   r   T)	rG   arrayrr   rl   r   r   rM   rN   rO   )ru   r   rQ   rQ   rR   )test_check_array_ensure_all_finite_object"  s   "r   z
X, err_msgInput contains NaN.z5Input contains infinity or a value too large for.*intrV   z#cannot convert float NaN to integerr   c                 C   sB   t jt|d t| t|d W d    d S 1 sw   Y  d S )Nr   r   )rM   rN   rO   r   int)ru   err_msgr   rQ   rQ   rR   8test_check_array_ensure_all_finite_object_unsafe_casting/  s   "r   c                  C   sh   t d} | g d}dt| d}t jt|d t|dd W d   dS 1 s-w   Y  dS )	z
    Check that we raise a proper error message when passing a Series and we expect a
    2-dimensional container.

    Non-regression test for:
    https://github.com/scikit-learn/scikit-learn/issues/27498
    pandasr~   rZ   rT   z+Expected a 2-dimensional container but got z	 instead.r   T	ensure_2dN)rM   r   SeriestyperN   rO   r   )pdsermsgrQ   rQ   rR   test_check_array_series_err_msgK  s   
"r   z3ignore:Can't check dok sparse matrix for nan or infc                     s>  ddgddgg} t | }tt t| W d    n1 s!w   Y  tg ddd}|jdks5J tjtdd	 tg dd
d W d    n1 sOw   Y  tjtdd	 tdd
d W d    n1 slw   Y  tt drt	t
t | }|jdkrtjtdd	 t|d
d
d W d    n1 sw   Y  tdddd}tt t| W d    n1 sw   Y  t|d
d tdddd  d} t} t} |||g}tjtttjd ttg}	g d}
d
dg}t||	|
|D ]\} }}}t| |||d}|d ur"|j|ks!J n	|j| jks+J |dkrA|jd s8J |jd r@J n|dkrV|jd sNJ |jd rVJ |rb| |us`J q| j|jkr|jd | jd kr|jd | jd kr| |u sJ qg }| fddtt t t D  ||d tj|d tj g ddgddgg}dd |	D }t||||D ]a\} }}}t| |||d}|d ur|j|ksJ n	|j| jksJ | j!|v r| j!|j!ksJ n
|j!|d ksJ |r| |usJ q| j|jkr%| j!|j!kr%| |u s%J qtddgddgg}t"|tj#s:J tt t|$  W d    n	1 sQw   Y  t|$ d
d t%|}t|}t"|tj#soJ tddgd dgg}d!}tjt|d	 t|d
d"t& d# W d    d S 1 sw   Y  d S )$Nr~   rZ   rT   r   )r   r~   rZ   Fr   z'Expected 2D array, got 1D array insteadr   Tz+Expected 2D array, got scalar array insteadrU   	csr_arrayzExpected 2D input, got)r   r      allow_ndr   r   )r   r   N)rW   r   rY   r   r   c                    s   g | ]}| qS rQ   rQ   ).0sparse_containerX_CrQ   rR   
<listcomp>  s    z$test_check_array.<locals>.<listcomp>r   csrcoodokc                 S   s   g | ]}|t ur|qS rQ   )rl   )r   dtrQ   rQ   rR   r     s    rW   r   rY   z<Negative values in data passed to X in RandomForestRegressorru   )ensure_non_negativer   r   )'ro   r   rM   rN   	TypeErrorr   ndimrO   hasattrnextiterr   rG   ra   r   rY   rc   r   r   r`   rb   rf   rl   r   rW   r   extendr%   r$   r#   r'   rd   re   formatr   ndarraytolistr"   r
   )ru   X_csrX_array
sparse_rowX_ndimX_FX_intX_floatXsdtypesorderscopysrW   r   rY   r   accept_sparsesnon_object_dtypesr   X_dense
X_no_arrayresultX_negr   rQ   r   rR   test_check_arrayZ  s   











&	
$r   1234US   1   2   3   4V1c                 C   sD   d}t jt|d t| dd W d   dS 1 sw   Y  dS )zmTest that check_array errors when it receives an array of bytes/string
    while a numeric dtype is required.z>dtype='numeric' is not compatible with arrays of bytes/stringsr   numericrV   NrM   rN   rO   r   )ru   expected_msgrQ   rQ   rR   test_check_array_numeric_error  s   "r  pd_dtype)Int8Int16UInt8UInt16Float32Float64zdtype, expected_dtyper  c                 C   s  t d}| dv rt jddd}tdddtjtjgtjtjdd	d
gg dgj}|j|| g dd}|d d|d< t|d|d}t	|| |j
|ksOJ t|d|d}t	|| |j
|ksbJ d}t jt|d t|dd W d    d S 1 s}w   Y  d S )Nr   >   r  r  z1.2
minversionr~   rZ   rT   r   r   r}   )r~   rZ   rT   r   r[   r   r   c)rW   columnsr  r   r   )r   rW   Fr   r   Tr   )rM   r   rG   r   rr   r   	DataFramerc   r   r   rW   rN   rO   )r	  rW   expected_dtyper   X_npru   r   r   rQ   rQ   rR   "test_check_array_pandas_na_support  s(   
(

"r  c                  C   s   t d} | jdd| jgdd}d}t jt|d t|dd	d
 W d   n1 s+w   Y  t|d	d	d
}t|ddtj	g |j
tjksHJ t|d	d	tjd}t|ddtj	g |j
tjksbJ dS )z4Check check_array is correct with pd.NA in a series.r   r~   rZ   Int64rV   r   r   TF)r   r   N)r   r   rW   )rM   r   r   NArN   rO   r   r   rG   rr   rW   re   rb   )r   X_int64r   X_outrQ   rQ   rR   *test_check_array_panadas_na_support_series  s   

r  c                  C   s  t d} tjg dg dg dgtjd}| |}t|jtjks%J t|tdjtjks1J |	dtj
i}t|jtj
tjtjf t|jtjksOJ t|tdjtjks[J |	dtji}t|jtjksmJ t|tdjtjksyJ |	dtj
i}t|jtjksJ t|tdjtjksJ |	tj}t|jtjksJ t|tdjtjksJ | d| g di}t|jtjksJ t|tdjtjksJ d S )	Nr   r   r   r[   r}      r   	   rV   r   rZ   cat_col)rM   r   rG   r   rb   r  r   rW   r)   rc   float16r   r   rh   re   Categoricalrd   )r   ru   X_dfcat_dfrQ   rQ   rR   %test_check_array_pandas_dtype_casting.  s*   
"
r'  c                  C   sj   t ddgddgddgddgg} t| }t|}|j| jks!J t|t jd}|jt t jks3J d S )Ng?gffffff?g333333?g      ?g?r\   rV   )rG   r   r   r   rW   rb   )arrmock_dfchecked_arrrQ   rQ   rR   "test_check_array_on_mock_dataframeS  s   "r+  c                  C   sF   g dg dg dg} t | jjdksJ t | ddjjdks!J d S )Nr   r  r  iFr   )r   rW   kindr   rQ   rQ   rR    test_check_array_dtype_stability\  s   r.  c            
      C   s  g dg dg dg} t j| t jd}t j| t jd}t|}t|}tj|t jd}||g}|||g}t	  t
d |D ]}t|t jdd}	|	jt jksTJ qB|D ]7}t|t jt jgdd}	|	jt jkslJ |	|u srJ t|t jt jgdd	gdd
}	|	jt jksJ |	|usJ qWt|t jt jgdd	gdd
}	|	jt jksJ |	|usJ |	jdksJ W d    d S 1 sw   Y  d S )Nr   r  r  rV   errorT)rW   r   r   r   r   F)rG   r   rb   rd   ro   r   rp   r`   warningscatch_warningssimplefilterr   re   rW   r   )

X_int_list	X_float32r  X_csr_float32X_csc_float32X_csc_int32integer_datafloat32_dataru   r   rQ   rQ   rR   test_check_array_dtype_warningc  sJ   






"r:  c                  C   sV  ddgddgg} t | }t }d}tjt|d t|dd W d    n1 s*w   Y  d	}tjt|d t||d W d    n1 sIw   Y  d
}tjt|d t|g d W d    n1 shw   Y  tjt|d t|dd W d    n1 sw   Y  tjtdd t||gd W d    d S 1 sw   Y  d S )Nr~   rZ   rT   r   ziSparse data was passed, but dense data is required. Use '.toarray\(\)' to convert to a dense numpy array.r   Fr   zjParameter 'accept_sparse' should be a string, boolean or list of strings. You provided 'accept_sparse=.*'.z]When providing 'accept_sparse' as a tuple or list, it must contain at least one string value.rQ   r   )ro   r   r   rM   rN   r   r   rO   )ru   r   invalid_typer   rQ   rQ   rR   -test_check_array_accept_sparse_type_exception  s0   
"r=  c                  C   sP   ddgddgg} t | }t|dd t|dd t|dgd t|dd d S )	Nr~   rZ   rT   r   Tr;  r   )r   )ro   r   r   )ru   r   rQ   rQ   rR   +test_check_array_accept_sparse_no_exception  s   
r>  )r   cscr   bsr)paramsc                 c   s    t jdd| jd}| jdkr/t|dr tdd |jD |_n|jd|_|jd|_n|j	d|_	|j
d|_
|V  d S )	N   rU   )r   r   coordsc                 s   s    | ]}| d V  qdS )rd   N)rc   )r   vrQ   rQ   rR   	<genexpr>  s    zX_64bit.<locals>.<genexpr>rd   )ro   randparamr   tuplerC  rowrc   colindicesindptr)requestru   rQ   rQ   rR   X_64bit  s   


rN  c                 C   s   t | ddd d S )NT)accept_large_sparser   )r   )rN  rQ   rQ   rR   1test_check_array_accept_large_sparse_no_exception  s   rP  c                 C   sF   d}t jt|d t| ddd W d    d S 1 sw   Y  d S )NzbOnly sparse matrices with 32-bit integer indices are accepted. Got int64 indices. Please do reportr   TF)r   rO  r  )rN  r   rQ   rQ   rR   4test_check_array_accept_large_sparse_raise_exception  s
   "rQ  c                  C   sz  d} t jt| d tg g W d    n1 sw   Y  d} t jt| d tg dd W d    n1 s8w   Y  td} t jt| d tddd W d    n1 sZw   Y  td}td	}d
} t jt| d t	||dd W d    n1 sw   Y  t jt| d t	||ddd W d    n1 sw   Y  td}td}d} t jt| d t	||dd W d    n1 sw   Y  t jt| d t	||ddd W d    n1 sw   Y  t
ddd}td}d} t jt| d t	|| W d    n	1 sw   Y  td}td}t	||dd\}}t|| t|| d S )NzC0 feature\(s\) \(shape=\(1, 0\)\) while a minimum of 1 is required.r   z@0 sample\(s\) \(shape=\(0,\)\) while a minimum of 1 is required.Fr   zgInput should have at least 1 dimension i.e. satisfy `len(x.shape) > 0`, got scalar `array(42)` instead.rD   )r~   rU   r~   zC1 sample\(s\) \(shape=\(1, 10\)\) while a minimum of 2 is required.rZ   )ensure_min_samples)rR  r   r|   zD2 feature\(s\) \(shape=\(10, 2\)\) while a minimum of 3 is required.rT   )ensure_min_featuresT)rS  r   r   rU   zD0 feature\(s\) \(shape=\(10, 0\)\) while a minimum of 1 is required.)rU   r      rT  r   )rM   rN   rO   r   reescaper   rG   r_   r   emptyr   r   )r   ru   r   r   	y_checkedrQ   rQ   rR   2test_check_array_min_samples_and_features_messages  sV   







rY  c                  C   sZ  t g dg dg} tjtdd t|  W d    n1 s!w   Y  g dg dg} tjtdd t|  W d    n1 sDw   Y  d} tjtdd t|  W d    n1 saw   Y  t g dt g dg} tjtdd t|  W d    n1 sw   Y  t g dt g df} tjtdd t|  W d    n1 sw   Y  tt g dg dg} tjtdd t|  W d    n1 sw   Y  tddgddgg} tjtdd t|  W d    n	1 sw   Y  t g d}tjtdd t	| W d    d S 1 s&w   Y  d S )	N      ?       @      @      @      @      @       @      @      @      @      @      @zComplex data not supportedr   )rZ  r^  r   r[  )r[  r\  r]  r_  r`  ra  )
rG   r   rM   rN   rO   r   r   ro   
coo_matrixr0   )ru   r   rQ   rQ   rR   #test_check_array_complex_data_error  s@   







$rc  c                  C   s^   t tdrJ t tdsJ t tdsJ t t dsJ G dd d} t | ds-J dd S )Nr   c                   @   s   e Zd ZeddddZdS )z@test_has_fit_parameter.<locals>.TestClassWithDeprecatedFitMethodz7Deprecated for the purpose of testing has_fit_parameterNc                 S      d S NrQ   )selfru   r   r   rQ   rQ   rR   r   M     zDtest_has_fit_parameter.<locals>.TestClassWithDeprecatedFitMethod.fitre  )r   
__module____qualname__r   r   rQ   rQ   rQ   rR    TestClassWithDeprecatedFitMethodL  s    rj  z=has_fit_parameter fails for class with deprecated fit method.)rB   r   r
   r   )rj  rQ   rQ   rR   test_has_fit_parameterF  s   rk  c               	   C   s\  t ddgddgg} t d}t ddgddgg}|t|t|t|t|t|t	|d}t
t t| W d    n1 sKw   Y  | D ]W\}}t
t t| W d    n1 slw   Y  t
t t|dd W d    n1 sw   Y  t|dd}t|r|j|ksJ t| |  qTt||  qTd S )	Nr   r~   rZ   )denser   r   r?  r   lilr@  T)raise_exceptionF)raise_warning)rG   r   r_   ro   
dok_matrixr   rp   rb  
lil_matrix
bsr_matrixrM   rN   rO   r   itemswarnsUserWarningissparser   r   rq   )arr_symarr_badarr_asymtest_arrays
arr_formatr(  outputrQ   rQ   rR   test_check_symmetricV  s6   



r}  c                  C   sV   G dd dt } tt t|   W d    n1 sw   Y  t|    d S )Nc                   @      e Zd Zdd Zdd ZdS )z6test_check_is_fitted_with_is_fitted.<locals>.Estimatorc                 [   s
   d| _ | S )NT)r4   rf  kwargsrQ   rQ   rR   r   {  s   z:test_check_is_fitted_with_is_fitted.<locals>.Estimator.fitc                 S   s   t | do| jS )Nr4   )r   r4   rf  rQ   rQ   rR   __sklearn_is_fitted__  s   zLtest_check_is_fitted_with_is_fitted.<locals>.Estimator.__sklearn_is_fitted__N)r   rh  ri  r   r  rQ   rQ   rQ   rR   	Estimatorz  s    r  )r   rM   rN   r   r<   r   )r  rQ   rQ   rR   #test_check_is_fitted_with_is_fittedy  s
   r  c                  C   s   G dd dt } t|   dS )z;Check that check_is_fitted passes for stateless estimators.c                       s$   e Zd Zdd Z fddZ  ZS )z:test_check_is_fitted_stateless.<locals>.StatelessEstimatorc                 [      | S re  rQ   r  rQ   rQ   rR   r        z>test_check_is_fitted_stateless.<locals>.StatelessEstimator.fitc                    s   t   }d|_|S )NF)super__sklearn_tags__requires_fit)rf  tagsr   rQ   rR   r    s   
zKtest_check_is_fitted_stateless.<locals>.StatelessEstimator.__sklearn_tags__)r   rh  ri  r   r  __classcell__rQ   rQ   r  rR   StatelessEstimator  s    r  N)r   r<   )r  rQ   rQ   rR   test_check_is_fitted_stateless  s   	r  c                  C   s  t t tt W d    n1 sw   Y  t t td W d    n1 s-w   Y  t } t }z4t t t|  W d    n1 sMw   Y  t t t| W d    n1 sfw   Y  W n tyw   J dw d}d}t jt|d t| |d W d    n1 sw   Y  d}d	}t jt|d t||d W d    n1 sw   Y  | j	t
   |j	t
   t| d u sJ t|d u sJ d S )
Nr   Fz&check_is_fitted failed with ValueErrorz!Random message %(name)s, %(name)sz+Random message ARDRegression, ARDRegressionr   )r   z"Another message %(name)s, %(name)szAnother message SVR, SVR)rM   rN   r   r<   r   r   r   rO   AttributeErrorr   r	   )ardsvrr   r   rQ   rQ   rR   test_check_is_fitted  sB   



r  c                  C   sr  G dd dt } d}|  }t|ddgdrJ tjt|d t|ddgd W d    n1 s1w   Y  t|ddgtdrAJ tjt|d t|ddgtd W d    n1 s\w   Y  t|ddgtdrlJ tjt|d t|ddgtd W d    n1 sw   Y  d	|_t|ddgdrJ tjt|d t|ddgd W d    n1 sw   Y  t|ddgtdrJ tjt|d t|ddgtd W d    n1 sw   Y  t|ddgtdsJ t|ddgtd d
|_	t|ddgdsJ t|ddgd t|ddgtdsJ t|ddgtd t|ddgtds.J t|ddgtd d S )Nc                   @      e Zd Zdd ZdS )z4test_check_is_fitted_attributes.<locals>.MyEstimatorc                 S   r  re  rQ   rf  ru   r   rQ   rQ   rR   r     r  z8test_check_is_fitted_attributes.<locals>.MyEstimator.fitN)r   rh  ri  r   rQ   rQ   rQ   rR   MyEstimator      r  z
not fitteda_b_)
attributesr   )r  
all_or_anyr   r   )
r   r4   rM   rN   r   r<   allrt   r  r  )r  r   estrQ   rQ   rR   test_check_is_fitted_attributes  sB   r  wrap)singlelistrH  )idsc                 C   s   t  }tjtdd t|| dg W d    n1 sw   Y  |jt   t|| dg tjtdd t|| dg W d    d S 1 sKw   Y  d S )Nzis not fitted yetr   coef_	coef_bad_)r   rM   rN   r   r<   r   r	   )r  r  rQ   rQ   rR   $test_check_is_fitted_with_attributes  s   "r  c                   C   s  t dgdgdgdgdg t ddgddgggddgddg t dgdtdgtd	 tjtd
d t ddgdg W d    n1 sGw   Y  tjtdd t ddgd W d    n1 sew   Y  tjtdd t ddgt	  W d    n1 sw   Y  tt t ddgtd W d    n1 sw   Y  tjtdd t ddgt
  W d    d S 1 sw   Y  d S )Nr~   rZ   rT   r   r[   r   r   )rZ   r~   rZ   zinconsistent numbers of samplesr   zgot <\w+ 'int'>zgot <\w+ 'object'>Expected sequence or array-like)r;   rG   r   ro   r   rM   rN   rO   r   rl   r
   rQ   rQ   rQ   rR   test_check_consistent_length  s$   ""r  c                  C   s\   z"dd l } tg dg dg dg}| j|g dd}t| W d S  ty-   tdw )Nr   r   r  r  )r   r   r   r  zPandas not found)r   rG   r   r  r;   ImportErrorr   )r   ru   r%  rQ   rQ   rR   "test_check_dataframe_fit_attribute  s   r  c                  C   s   t dt jg} tt t|  W d    n1 sw   Y  tjdd t|  tjdd tt t|  W d    d S 1 sFw   Y  d S )Nr   T)assume_finiteF)	rG   r   infrM   rN   rO   r:   sklearn
set_configr   rQ   rQ   rR   test_suppress_validation  s   

"r  c                  C   sp   t d} t| g ddd}t|tg d | g dd}t|d dd}t|tjg dtd d S )	Nr   r   Fr   r  category)rW   r   rV   )r   r   r   r   rG   r   rc   rl   )r   ressrQ   rQ   rR   test_check_array_series  s   r  rW   
bool_dtype)rf   booleanc                 C   s   |dkrt ddd}nt d}|jg dg d|jg d|dd	g d	d
}t|| d}|jtjks5J tjg dg dg dgtd}t	|| d S )Nr  r   1.0r  r   )r   r\    @TFTrV   )r   r   rf   r  )      ?        r  )       @r\   r  )      @r  r  )
r   r  r   r   rW   rG   re   r   r   r   )rW   r  r   dfr   expected_arrayrQ   rQ   rR   'test_check_dataframe_mixed_float_dtypes)  s    	r  c                  C   s   t d} | dg di}t|dd}|jtjksJ t|dgdgdgg | jg dg dd	dd
gd}t|dd}|jtjksDJ t|ddgddgddgg dS )z7Check that dataframe with bool return a boolean arrays.r   rf   r  NrV   TFr   )rf   r   r   r  r  r~   r   rZ   rT   )r   r  r   rW   rG   bool_r   rd   r   r  r   rQ   rQ   rR   #test_check_dataframe_with_only_boolK  s    r  c                  C   s^   t ddd} | d| jg dddi}t|dd}|jtjks"J t|d	gd
gd	gg dS )zFCheck that dataframe with boolean return a float array with dtype=Noner   r  r  rf   r  r  rV   NTF)r   r  r   r   rW   rG   re   r   r  rQ   rQ   rR   &test_check_dataframe_with_only_boolean^  s
   r  c                   @   r  )DummyMemoryc                 C      |S re  rQ   )rf  funcrQ   rQ   rR   cachei  r  zDummyMemory.cacheN)r   rh  ri  r  rQ   rQ   rQ   rR   r  h  r  r  c                   @      e Zd ZdS )WrongDummyMemoryNr   rh  ri  rQ   rQ   rQ   rR   r  m      r  c                  C   s   t d} | jdksJ t d } | jd u sJ t }t |} | |u s#J d}tjt|d t d W d    n1 s;w   Y  t }d|}tjt|d t | W d    d S 1 s_w   Y  d S )Ncache_directoryzf'memory' should be None, a string or have the same interface as joblib.Memory. Got memory='1' instead.r   r~   zg'memory' should be None, a string or have the same interface as joblib.Memory. Got memory='{}' instead.)r=   locationr  rM   rN   rO   r  r   )memorydummyr   rQ   rQ   rR   test_check_memoryq  s&   

"r  rY   c                 C   sp   t d}t|dd#}t|| d}t |||  ksJ |jd | ks&J W d    d S 1 s1w   Y  d S )N)r   r   r	mmap_moderX   	WRITEABLE)rG   r_   r   r   may_share_memoryr   )rY   ru   X_memmapr   rQ   rQ   rR   test_check_array_memmap  s   
"r  zOestimator_name, estimator_value, delegates, expected_result, expected_exception
estimator_SubEstimatorrQ   attribute_presentr   estimators_custom_estimatorno_estimatorattribute_absent)'fitted_estimator_with_default_delegates estimator_with_default_delegates#list_of_estimators_with_estimators_&custom_estimator_with_custom_delegates#no_estimator_with_default_delegates5estimator_with_default_delegates_but_absent_attributec                 C   s   |du r	t d}nt d|d}G dd d}| }t|| | |r=t| || W d   dS 1 s6w   Y  dS |||ksEJ dS )z
    Tests the _estimator_has function by verifying:
    - Functionality with default and custom delegates.
    - Raises ValueError if delegates are missing.
    - Raises AttributeError if the specified attribute is missing.
    Nr  )	delegatesc                   @   r  )z)test_estimator_has.<locals>.MockEstimatorNr  rQ   rQ   rQ   rR   MockEstimator  r  r  )r2   setattrrM   rN   )estimator_nameestimator_valuer  expected_resultexpected_exceptioncheckr  r   rQ   rQ   rR   test_estimator_has  s   E

"r  c                 C   s   t g dg dg dg dg}| |}t|d | ddgddgg}t|d d|d< | |}tjtdd t|d W d    d S 1 sIw   Y  d S )	N)r~   r~   r   r   )r   r   r   r   r   r   r   r^   z	Negative r   )rG   r   r>   rM   rN   rO   )r   r   ru   rQ   rQ   rR   test_check_non_negative  s   "

"r  c                  C   s   t d} d }d}tjt|d t| | W d    n1 s w   Y  d}tjt|d t| |t d W d    d S 1 sBw   Y  d S )NrZ   rZ   z;estimator requires y to be passed, but the target y is Noner   zGRandomForestRegressor requires y to be passed, but the target y is None)r   )rG   r_   rM   rN   rO   r   r
   ru   r   r   rQ   rQ   rR    test_check_X_y_informative_error  s   
"r  c                  C   st   G dd d} |  }t |t|ksJ G dd d}tjtdd t |  W d    d S 1 s3w   Y  d S )Nc                   @   r~  )zJtest_retrieve_samples_from_non_standard_shape.<locals>.TestNonNumericShapec                 S   
   d| _ d S N)znot numericr   r  rQ   rQ   rR   __init__     
zStest_retrieve_samples_from_non_standard_shape.<locals>.TestNonNumericShape.__init__c                 S   s   t g dS )Nr   )lenr  rQ   rQ   rR   __len__  s   zRtest_retrieve_samples_from_non_standard_shape.<locals>.TestNonNumericShape.__len__N)r   rh  ri  r  r  rQ   rQ   rQ   rR   TestNonNumericShape  s    r  c                   @   r  )zJtest_retrieve_samples_from_non_standard_shape.<locals>.TestNoLenWeirdShapec                 S   r  r  r  r  rQ   rQ   rR   r    r  zStest_retrieve_samples_from_non_standard_shape.<locals>.TestNoLenWeirdShape.__init__Nr   rh  ri  r  rQ   rQ   rQ   rR   TestNoLenWeirdShape  r  r  r  r   )r8   r  rM   rN   r   )r  ru   r  rQ   rQ   rR   -test_retrieve_samples_from_non_standard_shape  s   "r  r   )rZ   rT   g      @r[   c              	   C   sX   t   t d t| dtjdddd}W d   n1 sw   Y  || ks*J dS )zPTest that check_scalar returns no error/warning if valid inputs are
    providedr/  	test_namerZ   r[   bothtarget_typemin_valmax_valinclude_boundariesN)r0  r1  r2  r@   numbersReal)r   scalarrQ   rQ   rR   test_check_scalar_valid%  s   


r  zJx, target_name, target_type, min_val, max_val, include_boundaries, err_msg
test_name1r   neitherz1test_name1 must be an instance of float, not int.z6test_name1 must be an instance of float, not NoneType.z4test_name1 must be an instance of int, not NoneType.z9test_name1 must be an instance of {float, bool}, not int.
test_name2ztest_name2 == 1, must be > 2.r[   
test_name3ztest_name3 == 5, must be < 4.
test_name4rightztest_name4 == 2, must be > 2.
test_name5leftztest_name5 == 4, must be < 4.
test_name6zbad parameter valuezyUnknown value for `include_boundaries`: 'bad parameter value'. Possible values are: ('left', 'right', 'both', 'neither').
test_name7zT`include_boundaries`='left' without specifying explicitly `min_val` is inconsistent.
test_name8zU`include_boundaries`='right' without specifying explicitly `max_val` is inconsistent.c              	   C   sl   t t}t| |||||d W d   n1 sw   Y  t|jt|ks*J t|jt|s4J dS )zLTest that check_scalar returns the right error if a wrong input is
    givenr  N)rM   rN   	Exceptionr@   strr   r   r   )r   target_namer   r  r  r  r   raised_errorrQ   rQ   rR   test_check_scalar_invalid6  s   v	r  r  )r[   y        -C6
?zJThere are imaginary parts in eigenvalues \(1e\-05 of the maximum real part)r[   g-C6
gưz?There are negative eigenvalues \(1e\-06 of the maximum positiveg|۽z?There are negative eigenvalues \(1e\-10 of the maximum positive)r[   g-=z=the largest eigenvalue is more than 1e\+12 times the smallest)nominalnominal_np_arrayinsignificant_imagzinsignificant negzinsignificant neg float32zinsignificant neg float64zinsignificant posz(lambdas, expected_lambdas, w_type, w_msgenable_warningsc                 C   s   |sd }|d u r)t   t dt t| |d}W d    n1 s#w   Y  ntj||d t| |d}W d    n1 sAw   Y  t|| d S )Nr/  )r  r   )r0  r1  r2  r   r-   rM   rt  r   )lambdasexpected_lambdasw_typew_msgr  lambdas_fixedrQ   rQ   rR    test_check_psd_eigenvalues_valid  s    
r!  )r[   y              @z/There are significant imaginary parts in eigenv)r   z,All eigenvalues are negative \(maximum is -1)r[   r   z*There are significant negative eigenvaluesga2U0*3?ggh㈵>g|)significant_imagzall negativezsignificant negzsignificant neg float32zsignificant neg float64zlambdas, err_type, err_msgc                 C   s<   t j||d t|  W d    d S 1 sw   Y  d S )Nr   )rM   rN   r-   )r  err_typer   rQ   rQ   rR   "test_check_psd_eigenvalues_invalid  s   	
"r%  c                  C   s  t dd d d } | jd rJ t| t dd} | jd s!J td t dd} t| t d tdt dd} t| dt d  tjtd	d
 tt dt dd W d    n1 scw   Y  d}tjt|d
 tt dt dd W d    n1 sw   Y  t d}t jdt jd} t| |} | j	t jksJ t jdt
d}td ||j	d} | j	t jksJ t d}t t|} d| d< d}tjt|d
 t| |dd W d    d S 1 sw   Y  d S )NrU   rZ   r   )r[   r~   r   )r[   rZ   r[   r  z)Sample weights must be 1D array or scalarr   )rZ   r   r  z/sample_weight.shape == \(4,\), expected \(2,\)!r   rV   ir   z1Negative values in data passed to `sample_weight`T)r   )rG   r_   r   r/   r   rM   rN   rO   rb   rW   r   re   r8   )r   r   ru   r   rQ   rQ   rR   test_check_sample_weight   s:   


"r&  rq   c                 C   s6   t ddd}| || |}}t||sJ d S )Nr!  rT   rG   ra   r   r*   rq   baser   r   rQ   rQ   rR   !test_allclose_dense_sparse_equalsL  s   r*  c                 C   s:   t ddd}| || |d }}t||rJ d S )Nr!  rT   r~   r'  r(  rQ   rQ   rR   %test_allclose_dense_sparse_not_equalsS  s   r+  c                 C   s`   t ddd}| |d }d}tjt|d t|| W d    d S 1 s)w   Y  d S )Nr!  rT   r~   zFCan only compare two sparse matrices, not a sparse matrix and an arrayr   )rG   ra   r   rM   rN   rO   r*   )rq   r   r   r   rQ   rQ   rR    test_allclose_dense_sparse_raiseZ  s   "r,  c                  C   s,  t ddddd} tjtdd | ddd W d    n1 s"w   Y  tjtd	d | dddd
 W d    n1 s@w   Y  t ddddddd}tjtdd |dd W d    n1 shw   Y  t ddddd}tjtdd |dd W d    d S 1 sw   Y  d S )Nr~   r  dc                S   rd  re  rQ   r   r   r  r.  rQ   rQ   rR   f1e  rg  z=test_deprecate_positional_args_warns_for_function.<locals>.f1Pass c=3 as keyword argsr   rZ   rT   Pass c=3, d=4 as keyword argsr   )r   r  r.  c                S   rd  re  rQ   r/  rQ   rQ   rR   f2o  rg  z=test_deprecate_positional_args_warns_for_function.<locals>.f2zPass b=2 as keyword argsc                S   rd  re  rQ   r/  rQ   rQ   rR   f3w  rg  z=test_deprecate_positional_args_warns_for_function.<locals>.f3)r~   r1   rM   rt  FutureWarning)r0  r3  r4  rQ   rQ   rR   1test_deprecate_positional_args_warns_for_functiond  s$   "r7  c                  C   sP   t dddd } tjtdd | dd W d    d S 1 s!w   Y  d S )	N1.1)versionc                S   rd  re  rQ   r   r   rQ   rQ   rR   r0    rg  zEtest_deprecate_positional_args_warns_for_function_version.<locals>.f1z,From version 1.1 passing these as positionalr   r~   rZ   r5  )r0  rQ   rQ   rR   9test_deprecate_positional_args_warns_for_function_version  s   
"r;  c                  C   s  G dd d} t jtdd | ddd W d    n1 sw   Y  t jtdd | dddd	 W d    n1 s=w   Y  G d
d d}t jtdd |ddd W d    n1 saw   Y  t jtdd |dddd	 W d    d S 1 sw   Y  d S )Nc                   @   s    e Zd ZedddddZdS )z:test_deprecate_positional_args_warns_for_class.<locals>.A1r~   r-  c                S   rd  re  rQ   rf  r   r   r  r.  rQ   rQ   rR   r    rg  zCtest_deprecate_positional_args_warns_for_class.<locals>.A1.__init__Nr   rh  ri  r1   r  rQ   rQ   rQ   rR   A1  s    r>  r1  r   r~   rZ   rT   r2  r   c                   @   s"   e Zd ZeddddddZdS )z:test_deprecate_positional_args_warns_for_class.<locals>.A2r~   r-  c                S   rd  re  rQ   r<  rQ   rQ   rR   r    rg  zCtest_deprecate_positional_args_warns_for_class.<locals>.A2.__init__N)r~   r~   r=  rQ   rQ   rQ   rR   A2  s    r?  )rM   rt  r6  )r>  r?  rQ   rQ   rR   .test_deprecate_positional_args_warns_for_class  s   "r@  rK  rT   c                 C   s   t jdd}g dt g dtg djtg dddd d}t||| d}| d ur0| ntt	|j
d }d	D ]}|| || u sGJ q;|d
 t|d
 |ksUJ t|d t|d | t|d t|d | d S )Nr   rZ   )r~   rZ   rT   r   r~   xxx)r  r   
sparse-col
sparse-row
scalar-int
scalar-strNone)rA  rK  r   )rC  rD  rE  rF  r  r   rB  )rG   rH   randnr   ro   rp   r   r,   r  ranger   r   r   r   )rK  ru   _paramsr   indices_keyrQ   rQ   rR   test_check_method_params  s$   	rL  	sp_format)Tr   r?  r   r@  c                 C   sd   t d}tdd}|jj|}t|| d}| du rd} t|s$J |j	| ks+J t
|| d S )Nr   rU   rT   r;  Tr   )rM   r   r   r  sparsefrom_spmatrixr   ro   rv  r   r   )rM  r   sp_matsdfr   rQ   rQ   rR   "test_check_sparse_pandas_sp_format  s   

rR  zntype1, ntype2)	)
longdoubler#  )r#  rb   )rb   double)rh   r`   )r`   long)byterj   )ushortrk   )rk   uint64)ri   rg   c                 C   s   t d}||jjg d| dd|jjg d|ddd}t|jtdk rLd}t jt|d	 t	|d
dgd W d   dS 1 sEw   Y  dS t	|d
dgd dS )zcheck that we raise an error with dataframe having
    sparse extension arrays with unsupported mixed dtype
    and pandas version below 1.1. pandas versions 1.1 and
    above fixed this issue so no error will be raised.r   r   r~   r   r   rW   
fill_valuer~   r   r~   col1col2r8  z3Pandas DataFrame with mixed sparse extension arraysr   r   r?  r;  N)
rM   r   r  arraysSparseArrayr(   __version__rN   rO   r   )ntype1ntype2r   r  r   rQ   rQ   rR    test_check_pandas_sparse_invalid  s   
"re  z ntype1, ntype2, expected_subtyperT  rS  r  rb   re   rg   rV  shortrh   intcr`   intprU  r   rd   longlongint_ubyteri   rj   rW  uintcrk   uintrX  uintp	ulonglongc                 C   sd   t d}||jjg d| dd|jjg d|ddd}t|ddgd	}t|j|s0J d S )
Nr   rY  r   rZ  r\  r]  r   r?  r;  )	rM   r   r  r`  ra  r   rG   
issubdtyperW   )rc  rd  expected_subtyper   r  r(  rQ   rQ   rR   test_check_pandas_sparse_valid  s   
rr  constructor_name)r  rH  r   	dataframe
sparse_csr
sparse_cscc                 C   s.   g dg dg}t || }t|dksJ dS )z$Check _num_features for array-likes.r   r  rT   N)r   r7   )rs  ru   rQ   rQ   rR   test_num_features  s   
rw  r   r  )FTF)r  g333333@g      @r   r   r  )r   r  rf   r   dict)r  rH  r   seriesc                 C   s   t | |} |dkrd}n	|dkrd}n|}d| }t| dr'|td7 }nt| d tr3|d	7 }nt| d tr>|d
7 }tjt	|d t
|  W d    d S 1 sUw   Y  d S )Nr   znumpy.ndarrayry  zpandas.*Series5Unable to find the number of features from X of type r   z with shape (3,)r   z" where the samples are of type strz# where the samples are of type dictr   )r   r   rU  rV  r   r  rx  rM   rN   r   r7   )ru   rs  expected_type_namemessagerQ   rQ   rR   &test_num_features_errors_1d_containers   s"   



"r}  )r~   r   Fr  )r   r  rf   r   c                 C   sL   dt | j }tjt|d t|  W d    d S 1 sw   Y  d S )Nrz  r   )r   ri  rM   rN   r   r7   )ru   r   rQ   rQ   rR    test_num_features_errors_scalarsA  s   
"r~  namesr.  )zlist-intrH  default
MultiIndexc                 C   sv   t d}|jddgddgddgg| d}t  tdt t|} W d	   n1 s.w   Y  | d	u s9J d	S )
zGet feature names with pandas dataframes without warning.

    Column names with consistent dtypes will not warn, such as int or MultiIndex.
    r   r~   rZ   r   r[   r}   r  r/  N)rM   r   r  r0  r1  r2  r6  r3   )r  r   ru   rQ   rQ   rR   2test_get_feature_names_pandas_with_ints_no_warningH  s   

 

r  c                  C   sL   t d} dd tdD }| jg dg dg|d}t|}t|| dS )	z)Get feature names with pandas dataframes.r   c                 S   s   g | ]}d | qS )col_rQ   )r   r,  rQ   rQ   rR   r   ^  s    z1test_get_feature_names_pandas.<locals>.<listcomp>rT   r   r  r  N)rM   r   rH  r  r3   r   )r   r  ru   feature_namesrQ   rQ   rR   test_get_feature_names_pandas[  s
   
r  zconstructor_name, minversion))pyarrowz12.0.0)rt  z1.5.0)polarsz0.18.2c                 C   s>   g dg dg}g d}t || ||d}t|}t|| dS )z:Uses the dataframe exchange protocol to get feature names.r~   r   rZ   rT   rT   r}   )col_0col_1col_2)columns_namer  N)r   r3   r   )rs  r  r   r  r  r  rQ   rQ   rR   )test_get_feature_names_dataframe_protocole  s   r  )r  rt  r  c                 C   s>   t g dg dg| }| dv rt|rJ d S t|sJ d S )Nr  r  )r  r  )r   r5   )rs  r  rQ   rQ   rR   !test_is_pandas_df_other_librariesu  s   r  c                  C   sL   t d} | g dg}t|sJ ttg drJ tdr$J dS )z8Check behavior of is_pandas_df when pandas is installed.r   r   r~   N)rM   r   r  r5   rG   r   )r   r  rQ   rQ   rR   test_is_pandas_df~  s
   
r  c                 C   s&   t tg drJ t drJ dS )z1Check _is_pandas_df when pandas is not installed.r   r~   N)r5   rG   r   )hide_available_pandasrQ   rQ   rR   &test_is_pandas_df_pandas_not_installed  s   r  r  rt  r   r  c                 C   sB   t g dg dg| |d}| dv rt|rJ d S t|sJ d S )Nr  r  r  )r  rt  )r   r6   )rs  r  r  rQ   rQ   rR   !test_is_polars_df_other_libraries  s   	r  c                  C   s$   G dd d} |  }t |rJ dS )zACheck _is_polars_df for object that looks like a polars dataframec                   @   r  )zNtest_is_polars_df_for_duck_typed_polars_dataframe.<locals>.NotAPolarsDataFramec                 S   s   g d| _ d| _d S )Nr   	my_schemar  schemar  rQ   rQ   rR   r    s   

zWtest_is_polars_df_for_duck_typed_polars_dataframe.<locals>.NotAPolarsDataFrame.__init__Nr  rQ   rQ   rQ   rR   NotAPolarsDataFrame  r  r  Nr6   )r  not_a_polars_dfrQ   rQ   rR   1test_is_polars_df_for_duck_typed_polars_dataframe  s   r  c                  C   s.   t g dg dg} t| }|du sJ dS )z/Get feature names return None for numpy arrays.r   r  N)rG   r   r3   )ru   r  rQ   rQ   rR   test_get_feature_names_numpy  s   r  znames, dtypesz['int', 'str']pizzaz['list', 'str']zint-strzlist-strc                 C   sx   t d}|jddgddgddgg| d}td| d	}t jt|d
 t|} W d   dS 1 s5w   Y  dS )zAGet feature names errors when the feature names have mixed dtypesr   r~   rZ   r   r[   r}   r  z]Feature names are only supported if all input features have string names, but your input has a.   as feature name / column name types. If you want feature names to be stored and validated, you must convert them all to strings, by using X.columns = X.columns.astype(str) for example. Otherwise you can remove feature / column names from your input data, or convert them all to a non-string data type.r   N)rM   r   r  rU  rV  rN   r   r3   )r  r   r   ru   r   rQ   rQ   rR   %test_get_feature_names_invalid_dtypes  s   

 
"r  c                   @   s(   e Zd ZdddZdd ZdddZdS )	PassthroughTransformerNc                 C   s   t | |dd | S )NT)reset)rC   r  rQ   rQ   rR   r     s   zPassthroughTransformer.fitc                 C   r  re  rQ   )rf  ru   rQ   rQ   rR   	transform  r  z PassthroughTransformer.transformc                 C   s
   t | |S re  )r+   )rf  input_featuresrQ   rQ   rR   get_feature_names_out  r  z,PassthroughTransformer.get_feature_names_outre  )r   rh  ri  r   r  r  rQ   rQ   rQ   rR   r    s    
r  c                  C   s   t g dg} t | }| }t|g d ddg}tjtdd || W d   n1 s4w   Y  |`	tjtdd |  W d   dS 1 sRw   Y  dS )	z4Check behavior of check_feature_names_in for arrays.r  r  r  )x0x1x2x10r  z*input_features should have length equal tor   Nz Unable to generate feature names)
rG   r   r  r   r  r   rM   rN   rO   n_features_in_)ru   r  r  incorrect_len_namesrQ   rQ   rR   test_check_feature_names_in  s   
"r  c                  C   s   t d} g d}| jg dg|d}t |}| }t|g d t jtdd |g d W d   dS 1 s>w   Y  dS )	z?Check behavior of check_feature_names_in for pandas dataframes.r   r  r  r  zinput_features is not equal tor   )r  r  x3N)	rM   r   r  r  r   r  r   rN   rO   )r   r  r  r  rQ   rQ   rR   "test_check_feature_names_in_pandas  s   
"r  c                  C   sD   d} t jt| d tt d W d   dS 1 sw   Y  dS )z@Check the error message when passing an unknown response method.zKRandomForestRegressor has none of the following attributes: unknown_method.r   unknown_methodN)rM   rN   r  r.   r
   )r   rQ   rQ   rR   )test_check_response_method_unknown_method  s
   "r  response_method)decision_functionpredict_probapredictc                 C   sL   d|  d}t jt|d tt |  W d   dS 1 sw   Y  dS )zUCheck the error message when a response method is not supported by the
    estimator.z7EstimatorWithFit has none of the following attributes: .r   N)rM   rN   r  r.   r   )r  r   rQ   rQ   rR   8test_check_response_method_not_supported_response_method  s
   
"r  c                  C   s   dg} t | }d}ddg}dd| d}tjt|d t||| W d	   n1 s/w   Y  ddg}t|||}|dksEJ ddg} t | }g d
}t|||}|dks^J d	S )z0Check that we can pass a list of ordered method.r  mocking_datar  r  zD_MockEstimatorOnOffPrediction has none of the following attributes: z, r  r   N)r  r  r  )r   joinrM   rN   r  r.   )method_implementedmy_estimatorru   r  r   method_name_predictingrQ   rQ   rR   #test_check_response_method_list_str  s&   r  c                  C   sJ   t d} t| ddgdd}tddg}|j|jksJ t|| dS )zRegression test for gh-25145r   TFr   N)r   r   r   rG   r   rW   r   )r   r  expectedrQ   rQ   rR   #test_boolean_series_remains_boolean-  s
   r  input_values)r   r~   r   r~   r   r~   c                 C   sL   t d}|j| dd}t|ddddd}t|jjtjsJ t||  dS )zpCheck pandas array with extensions dtypes returns a numeric ndarray.

    Non-regression test for gh-25637.
    r   Int32rV   NF)rW   r   r   r   )	r   r   r   rG   rp  rW   r-  floatingr   )r  r   input_seriesr   rQ   rQ   rR   !test_pandas_array_returns_ndarray7  s   r  c               	   C   s(  t d} | j| jddgd| jdgg| jd}tdd# t jtdd	 t| W d
   n1 s3w   Y  W d
   n1 sBw   Y  | j| j	ddgd| j	dgg| jd}tdd, t jtdd	 t| W d
   n1 suw   Y  W d
   d
S W d
   d
S 1 sw   Y  d
S )z9Checks that Array API arrays checks non-finite correctly.array_api_strictr~   r   rT   rV   Tarray_api_dispatchr   r   Nzinfinity or a value too large)
rM   r   r   rr   rb   r   rN   rO   r   r  )xpX_nanX_infrQ   rQ   rR   )test_check_array_array_api_has_non_finiteJ  s    
$
$
"r  zextension_dtype, regular_dtype))r  rf   )r  rd   )r  re   )r  rl   include_objectc                 C   s   t d}||jg d|d|jg dddd}|r(|jg ddd|d	< |j|d
 | d}t|dd}t|dd}t|| dS )zKCheck pandas extension arrays give the same result as non-extension arrays.r   )r~   r   r~   r   rV   )r!  r   r   r}   rd   )r   r  r/  rl   r   r   )r   N)rM   r   r  r   assignrc   r   r   )extension_dtyperegular_dtyper  r   	X_regularX_extensionX_regular_checkedX_extension_checkedrQ   rQ   rR   $test_check_array_multiple_extensionsZ  s   
r  c                  C   s6   t d} | g dg dd}t|dksJ dS )zDUse the DataFrame interchange protocol to get n_samples from polars.r  r   r  r:  rT   N)rM   r   r  r8   )plr  rQ   rQ   rR   #test_num_samples_dataframe_protocoly  s   
r  r   output_format)r   r?  r   c                 C   s   | ddgddggt jd}t|dr|jt j|_n-t|dr.t|dr.|jt j|_nt|drHt|drH|jt j|_|jt j|_t	||d	}|d
krf|jj
t jks[J |jj
t jksdJ dS |jj
t jksoJ |jj
t jksxJ dS )zGCheck the consistency of the indices dtype with sparse matrices/arrays.r   r~   rV   offsetsrI  rJ  rK  rL  r;  r   N)rG   re   r   r  rc   r`   rI  rK  rL  r   rW   rJ  )r   r  ru   r   rQ   rQ   rR   1test_check_array_dia_to_int32_indexed_csr_csc_coo  s   
r  sequencec                 C   s:   t | }t|tjsJ |jjdksJ |jdksJ d S )NOr~   )r9   r   rG   r   rW   r-  r   )r  outrQ   rQ   rR   test_to_object_array  s   r  c                  C   s  dg dfdg dfdt dd fdg dfdg d	fdd
gdgdggfdg dg dgfdg dgfdd
d
gddgdd
ggfddd
gddgdd
ggfdg dgfdt ddfg} | D ],\}}|dv rrtt|t | q_tt t| W d    n1 sw   Y  q_d S )Nbinary)spameggr  )r   r~   r   r~   
continuousrU   g      4@
multiclassr   )r   r~   rZ   rZ   r   r~   rZ   rT   zmultilabel-indicatorrY  )r   r   r~   zmulticlass-multioutputr[   r   zcontinuous-multioutput   )r   rT   )r  r  r  )	rG   ra   r   r   rA   r   rM   rN   rO   )EXAMPLESy_typer   rQ   rQ   rR   test_column_or_1d  s*   




r  c                  C   s    G dd d} t |  rJ dS )z@Check that _is_polars_df return False for non-dataframe objects.c                   @   r  )z+test__is_polars_df.<locals>.LooksLikePolarsc                 S   s   ddg| _ ddg| _d S )Nr   r   r  r  rQ   rQ   rR   r    s   
z4test__is_polars_df.<locals>.LooksLikePolars.__init__Nr  rQ   rQ   rQ   rR   LooksLikePolars  r  r  Nr  )r  rQ   rQ   rR   test__is_polars_df  s   r  c                  C   sn   t jjdd} t| ddd}t || sJ |jjsJ d| j_t| ddd}t || r/J |jjs5J dS )zyCheck the behavior of check_array when a writeable array is requested
    without copy if possible, on numpy arrays.
    r   sizeFTrY   force_writeableN)rG   rH   uniformr   r  r   	writeable)ru   r  rQ   rQ   rR   test_check_array_writeable_np  s   r  c                  C   s~   t jjdd} t| dd}t|ddd}t ||sJ |jjs"J t| dd}t|ddd}t ||r7J |jjs=J d	S )
a  Check the behavior of check_array when a writeable array is requested
    without copy if possible, on a memory-map.

    A common situation is when a meta-estimators run in parallel using multiprocessing
    with joblib, which creates read-only memory-maps of large arrays.
    r   r  zw+r  FTr  r  N)rG   rH   r  r    r   r  r   r  )ru   mmapr  rQ   rQ   rR   test_check_array_writeable_mmap  s   r  c                  C   s   t d} tjjdd}| j|dd}t|ddd}t||s"J |jj	s(J d|j_	| j|dd}t|ddd}t||rBJ |jj	sHJ dS )	zxCheck the behavior of check_array when a writeable array is requested
    without copy if possible, on a dataframe.
    r   r   r  FrX   Tr  N)
rM   r   rG   rH   r  r  r   r  r   r  )r   ru   r  r  rQ   rQ   rR   test_check_array_writeable_df  s   
r  c               	   C   s   t g dg dg} tdd5 t t| ddsJ tt t|  W d    n1 s0w   Y  W d    d S W d    d S 1 sHw   Y  d S )NrY  r\  Tr  r;  )ro   r   r   rv  r   rM   rN   r   )X_sprQ   rQ   rR   8test_check_array_on_sparse_inputs_with_array_api_enabled	  s   
"r  c                  C   s   t jjdd} t jjddd}d}tjt|d t| dd W d    n1 s)w   Y  tjt|d t| |dd W d    n1 sGw   Y  tjt|d t	| dd W d    d S 1 sew   Y  d S )	Nr   r  r~   )rU   z5'force_all_finite' was renamed to 'ensure_all_finite'r   T)force_all_finite)
rG   rH   r  rL   rM   rt  r6  r   r   r   r  rQ   rQ   rR   $test_force_all_finite_rename_warning	  s   "r  )__doc__r  rU  r0  	itertoolsr   operatorr   tempfiler   numpyrG   rM   scipy.sparserN  ro   r   r  sklearn._configr   sklearn._min_dependenciesr   sklearn.baser   sklearn.datasetsr	   sklearn.ensembler
   sklearn.exceptionsr   r   sklearn.linear_modelr   (sklearn.metrics.tests.test_score_objectsr   sklearn.neighborsr   sklearn.random_projectionr   sklearn.svmr   sklearn.utilsr   r   r   r   r   r   sklearn.utils._mockingr   r   sklearn.utils._testingr   r   r   r   r   r   r    r!   sklearn.utils.estimator_checksr"   sklearn.utils.fixesr#   r$   r%   r&   r'   r(   sklearn.utils.validationr)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rS   r{   markparametrizerH   rF  tocsrr   r   r   r   r  rr   r   r   r   r   r   r   r   rl   r   r   filterwarningsr   r  rb   re   r  r  r'  r+  r.  r:  r=  r>  fixturerN  rP  rQ  rY  rc  rk  r}  r  r  r  r  r  rH  r  r  r  r  r  r  r  r  r  r  r  r  r   rO   r  r  rp   rb  rq  rr  rp  
dia_matrixr  r  r  r  r   r   r  Integralrf   r   r  _psd_cases_validvalueskeysr!  _psd_cases_invalidr%  r&  r*  r+  r,  r7  r;  r@  rL  rR  re  r  integerunsignedintegerrr  rw  r}  r~  rH  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  rQ   rQ   rQ   rR   <module>   s    (
 t$*
(	

 


%	+


?*#$%



.
9










q




,

	*
	
	
&
	0
