CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
test_isotonic.cpython-310.pyc133 linesDownload Raw Back to __pycache__
1o

2�Yi�Y�@sFddlZddlZddlZddlZddlZddlmZddlZddl	m3Z4ddlmZm
Z
mZmZddlmZddlmZmZmZddlmZdd	�Zd5d�Zdd
�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Z dd�Z!dd�Z"dd�Z#d d!�Z$d"d#�Z%d$d%�Z&d&d'�Z'd(d)�Z(d*d+�Z)d,d-�Z*d.d/�Z+d0d1�Z,d2d3�Z-d4d5�Z.d6d7�Z/d8d9�Z0d:d;�Z1d<d=�Z2d>d?�Z3ej4�5d@ej6ej7ej8ej9g�dAdB��Z:ej4�5dCej6ej7ej8ej9g�dDdE��Z;dFdG�Z<ej4�5d@ej9ej8g�dHdI��Z=dJdK�Z>dLdM�Z?ej4�5dNdOdPg�dQdR��Z@dSdT�ZAdUdV�ZBdWdX�ZCej4�5dYdZd[g�d\d]��ZDd^d_�ZEdS)`�N)�expit)�make_regression)�IsotonicRegression�_make_unique�check_increasing�isotonic_regression)�shuffle)�assert_allclose�assert_array_almost_equal�assert_array_equal)�check_arrayc	Csht�}gd�}gd�}gd�}t|||dd�\}}}|j|||d�}|j|||d��|�}t||�dS)N���������r�)�3rrr�r)�random_state��
sample_weight)rr�
fit_transform�fit�	transformr)	�ir�x�yrZx_sZy_sZsample_weight_sZ
y_transformedZy_transformed_s�r"��E:\DocsHouse\542 percep lab latest\PerceptionLab\PerceptionLab_Portable\python_embed\Lib\site-packages\sklearn/tests/test_isotonic.py�test_permutation_invariancesr$cC�Zgd�}gd�}t���t�dt�t||�}Wd�n1s"wY|s+J�dS)N�rrr)rg�������?g�������?�error��warnings�catch_warnings�simplefilter�UserWarningr�r r!�
is_increasingr"r"r#�-test_check_increasing_small_number_of_samples(s6�r/cCr%)N�rrrrrr)r��?g)\���(@�{�G��!@r2�2r'r(r-r"r"r#�test_check_increasing_up3�7�r4cCsZgd�}gd�}t���t�dt�t||�}Wd�n1s"wY|s+J�dS)Nr0r'r(r-r"r"r#� test_check_increasing_up_extreme?r5r6cC�Zgd�}gd�}t���t�dt�t||�}Wd�n1s"wY|r+J�dS)Nr0)rg��g)\���(��{�G��!�r8�����r'r(r-r"r"r#�test_check_increasing_downKr5r:cCr7)Nr0)r�������������������������r'r(r-r"r"r#�"test_check_increasing_down_extremeWr5r@cCsXgd�}gd�}d}tjt|d��
t||�}Wd�n1s!wY|r*J�dS)Nr0)rr;rr=rr?�interval��match)�pytestZwarnsr,r)r r!�msgr.r"r"r#�test_check_ci_warncs�rFcCs<t�gd��}t�gd��}t|t|��t�gd��}t�gd��}t|t|��t�t|��}tddd�}|�||�t|�||��|�|�	||��t|�|�|�8|��tj�t|��}tddd�}t|�	||||�|�	||�|�t|�||�|�|�|�t�}t|�	t�
t|��|�t�|��dS)N�rrr�	�r�9)rrrrIrIrIrJ)rJrr)rrr���?��y_min�y_max)�np�arrayrr�arange�lenrrrr�predict�randomZpermutation�ones�mean)r!�y_r r�permr"r"r#�test_isotonic_regressionos" &&rZcC�`gd�}gd�}gd�}t�}|�||�t|�||��|�|�||��t||�||��dS)N)rrrrrr�rrrrrr)r1r1rrrr�rrrrr�r r!Zy_truerr"r"r#�!test_isotonic_regression_ties_min�� r_cCr[)N)rrrrrrr\)rrrr�@rar]r^r"r"r#�!test_isotonic_regression_ties_max�r`rbcCsTgd�}gd�}gd�}t�}|�||�t|�|�|d�t|�||�|d�dS)aw10    Test isotonic regression fit, transform  and fit_transform11    against the "secondary" ties method and "pituitary" data from R12     "isotone" package, as detailed in: J. d. Leeuw, K. Hornik, P. Mair,13     Isotone Optimization in R: Pool-Adjacent-Violators Algorithm14    (PAVA) and Active Set Methods15 16    Set values based on pituitary example and17     the following R command detailed in the paper above:18    > library("isotone")19    > data("pituitary")20    > res1 <- gpava(pituitary$age, pituitary$size, ties="secondary")21    > res1$x22 23    `isotone` version: 1.0-2, 2014-09-0724    R version: R version 3.1.1 (2014-07-10)25    )rIrIrIrJrJrJ�rcrc�rd)���7@�rre�g�5@��rfrh)�b��h�86@rkrkrkrkrkrkrkrk�@8@rlrN)rrr26rrr^r"r"r#�(test_isotonic_regression_ties_secondary_�srmcCsbt�gd��}t�gd��}t�gd��}t�}|�||�t|�|�|�t|�||�|�dS)ax27    Non-regression test to handle issue 9432:28    https://github.com/scikit-learn/scikit-learn/issues/943229 30    Compare against output in R:31    > library("isotone")32    > x <- c(0, 1, 1, 2, 3, 4)33    > y <- c(0, 0, 1, 0, 0, 1)34    > res1 <- gpava(x, y, ties="secondary")35    > res1$x36 37    `isotone` version: 1.1-0, 2015-07-2438    R version: R version 3.3.2 (2016-10-31)39    )rrrrrr)rrrrrr)rK��?rnrnrnrLN)rPrQrrr40rrr^r"r"r#�>test_isotonic_regression_with_ties_in_differently_sized_groups�srocCs�t�gd��}t�gd��}t|dd�}t||�tdd��t�t|��|�}t||�tt�	|dd�j41�|dd�|dd�dk�dS)N�rJrHrJrr�ffffff@r)rJ�#@rrr�333333@rsrF��42increasingr;rr)rPrQrr	rrrRrSrrV�shape)r!�y_resultZy_isorXr"r"r#�!test_isotonic_regression_reversed�s43446rxcCs�t�gd��}t�t|��}tdd�}tjdd��}t�d�|�||�}t	dd�|D��s0J�Wd�n1s:wY|d	|d45k}|rKJ�dS)Nrp�autortT��record�alwayscS�g|]	}dt|j�v�qS�zinvalid value encountered in ��str�message��.0�warnr"r"r#�46<listcomp>��z<test_isotonic_regression_auto_decreasing.<locals>.<listcomp>rr;�47rPrQrRrSrr)r*r+r�all�r!r r�wrXr.r"r"r#�(test_isotonic_regression_auto_decreasing��4849�r�cCs�t�gd��}t�t|��}tdd�}tjdd��}t�d�|�||�}t	dd�|D��s0J�Wd�n1s:wY|d	|d50k}|sKJ�dS)N)rrqrrrJrHrJryrtTrzr|cSr}r~rr�r"r"r#r�
r�z<test_isotonic_regression_auto_increasing.<locals>.<listcomp>rr;r�r�r"r"r#�(test_isotonic_regression_auto_increasingr�r�cCs4t�}tj�d�}d}tjt|d��|�gd�gd�ddg�Wd�n1s*wYtjt|d��|�gd�dd	g�Wd�n1sKwYd51}tjt|d��|�|�dd�gd��Wd�n1spwYd
}tjt|d��|�	|�dd��Wd�dS1s�wYdS)N�*z:Found input variables with inconsistent numbers of samplesrBr&)rrr皙�����?g333333�?rrzX should be a 1d arrayrrJz0Isotonic regression input X should be a 1d array)52rrPrU�RandomStaterD�raises�53ValueErrorrZrandnr)r�rngrEr"r"r#�test_assert_raises_exceptionss"���"�r�cCs~t�}tj�d�}d}t�|�}|jdd|fd�dt�dt�|��}t�|�}|j|||d�}|�||�}t	||�dS)	Nr��dr9r3��sizegI@rr)54rrPrUr�rR�randint�logrVrr)rr��nr r!�weightsZy_set_valueZy_default_valuer"r"r#�3test_isotonic_sample_weight_parameter_default_value(s55*56r�cCsLtddd�}d}t�|�}t�|�}gd�}t�|�||��}t||�dS)NrrrMr)rrrrrr)rrPrR�roundrr)rr�r r!Zy_testrwr"r"r#� test_isotonic_min_max_boundaries8s5758r�cCsDt�}gd�}gd�}gd�}gd�}|j|||d�}t||�dS)Nr
r)r�fffff�+@r�r�r�r�rr)rrr)rr r!rZ59expected_yZ60received_yr"r"r#�test_isotonic_sample_weightCsr�cCs�t�gd��}t�t|��}tddd�}|�||�d}tjt|d��|�	t61|�dt|�dg�Wd�dS1s>wYdS)NrGry�raise�ru�
out_of_boundsz)in x_new is below the interpolation rangerBrJ)rPrQrRrSrrrDr�r�rT�min�max)r!r rrEr"r"r#�"test_isotonic_regression_oob_raiseNs "�r�cCs�t�gd��}t�t|��}tddd�}|�||�|�t|�dt|�dg�}|�|�}t|�t|�ks8J�t|�t|�ksBJ�dS)NrGry�clipr�rJ)	rPrQrRrSrrrTr�r�)r!r r�y1�y2r"r"r#�!test_isotonic_regression_oob_clip]s62r�cCslt�gd��}t�t|��}tddd�}|�||�|�t|�dt|�dg�}t	t�63|��dks4J�dS)NrGry�nanr�rJr)rPrQrRrSrrrTr�r��sum�isnan)r!r rr�r"r"r#� test_isotonic_regression_oob_nanmsr�cCsjt�gd��}t�t|��}tddd�}|�||�t�|tj�}t�	|�}tj64�|�|�|�|��dS)NrGryr�r�)
rPrQrRrSrr�pickle�dumps�HIGHEST_PROTOCOL�loadsZtestingrrT)r!r rZir_serZir2r"r"r#�test_isotonic_regression_pickle{s65r�cCsJgd�}gd�}tddd�}|�||�t�t�|�|���}|s#J�dS)N)rrrTr�r�)rrrPr��isfiniterT)r r!rZall_predictions_finiter"r"r#�!test_isotonic_duplicate_min_entry�sr�cCs�t�gd��}t|ddd�}t�|dk�sJ�t�|dk�s J�t|dddd�}t�|dk�s1J�t�|dk�s:J�t|ddd�}t�|dk�sJJ�dS)	N)gh��|?5�?gJ+��?g�����M�g���S��?g9��v���g�I+�ƿg/�$����g�l�����?g�t�V�?g��"��~�?g�z�G��?gy�&1��?g#��~j���g�"��~j�?g;�O��n�?�T㥛� ؿgy�&1��r�g�Zd;��g66ףp=67�?rKr�rMrF)rNrOru)rNru)rPrQrr�)r r!r"r"r#�test_isotonic_ymin_ymax�s�r�cCsptj�d�}t�}d}t�dd|�}||j|d�}|j|d�}d|dd�<|j|||d	�|j|||d	�dS)68Nr�r3r=rr�rrrIr)rPrUr�rZlinspace�uniformr)r�Z69regression�	n_samplesr r!r�r"r"r#�test_isotonic_zero_weight_loop�sr�cCs�tj�d�}d}d|�|�d}t�|�|�t|���d��d�}|�|�}d||�|�dk<tdd	d70d�}tdd	d71d�}|j|||dd
�\}}|�	||�|j72|||d�d|�|�d}	|�|	�}73|�|	�}t|74|�dS)N�{i�g4@rJ�int64�float64rr�rr�)rNrOr�F)rZtrim_duplicatesr)
rPrUr�Zrand�lessr�astyperZ_build_yZ_build_frrTr)r�r�ZX_trainZy_trainr�Z75slow_modelZ76fast_modelZX_train_fitZy_train_fitZX_testZy_pred_slowZy_pred_fastr"r"r#�test_fast_predict�s$ �7778�7980r�cCst�}t�|�dS)N)r�copy)rr"r"r#�test_isotonic_copy_before_fit�sr��dtypec	Cs�gd�}tjgd�tjd�}t�}d|�tj�|fD]@}tj||d�}t|tjtjgdd�j}t||d�}|j|ks<J�t�	t81|���|�}|j|||d�|�|�}|j|ksZJ�qdS)N�rrrrr)��������?r�r�r�r��r�F)r�Z	ensure_2dr)
rPrQr�rr��float32rr�rrRrSrrT)	r�r!r��regrZy_npZexpected_dtype�res�Xr"r"r#�test_isotonic_dtype�s"��82�r��y_dtypecCsRt�}tjgd�|d�}tjt|�tjd�}|�||�|�|�j|jks'J�dS)Nr�r�)	rrPrQrRrSr�rrTr�)r�r�r!r�r"r"r#�test_isotonic_mismatched_dtypes83r�cCs^gd�}tjtjfD]"}tj||d�}|��}t�|�}t|||�\}}}t|gd��q84dS)N)rrrrrr�)rrr)rPr�r�rQr��	ones_likerr)Zx_listr�r r!r�r"r"r#�test_make_unique_dtypes85�r�cCsltjgd�|d�}|��}t�|�}t|||�\}}}|tjkr(t�gd��}nt�ddg�}t||�dS)N)rg��ؗ�Ҝ<r�-�?r�)rrr�rr)rPrQr�r�rr�r)r�r r!r�Zx_outr"r"r#�test_make_unique_tolerance)s8687r�cCs�tjgd�tjd�}tjgd�tjd�}t��||�}|�gd��}t|t�gd���t|jt�gd���t|jt�gd���dS)N)rrrLrr�)rrrr)rg�?rr1r)rg�?r1g@r)rKrLg@)rKr1g@)	rPrQr�rrrTr�
X_thresholds_�
y_thresholds_�r�r!�ireg�y_predr"r"r#�#test_isotonic_make_unique_tolerance7sr�cCsVt�gd��}t�gd��}t��||�}|�t�gd���}t�t�|��s)J�dS)N)rKgj gJ/�rL)��z�G��?r��)\���(�?r�)rg�gA��g�����|�=)rPrQrrrTr�r�r�r"r"r#�&test_isotonic_non_regression_inf_slopeDs88r�ruTFcCs�tj�d�}d}|j|d�}|j|d�}t|d��||�}|j|j}}|j|jks,J�|jd|jdks8J�t�	||��89�sBJ�|��|��ksLJ�|��|��ksVJ�t90t�
|�dk�saJ�|rpt91t�
|�dk�snJ�dSt92t�
|�dk�s{J�dS)Nr��r�rtr)rPrUr��normalrrr�r�rv�isinr�r�r��diff)rur�r�r�r!r�ZX_thresholdsZy_thresholdsr"r"r#�test_isotonic_thresholdsNsr�cCs�t�d�}|�dd�}t�d�}t��||�}t��||�}|j|jks&J�|j|jks.J�|j|jks6J�|j|jks>J�t	|j93|j94�t	|j|j�|�|�}|�|�}t
||�dS)NrJr;r)rPrR�reshaperrZX_max_ZX_min_rOrNrr�r�rTr	)r��X_2dr!�iso_regZ95iso_reg_2dZy_pred1Zy_pred2r"r"r#�test_input_shape_validationis96979899r�cCs�t�d�}tj||f}t�d�}d}tjt|d��t��||�Wd�n1s,wYt��||�}tjt|d��
|�|�Wd�n1sOwYtjt|d��|�	|�Wd�dS1slwYdS)NrJz/should be a 1d array or 2d array with 1 featurerB)100rPrRZc_rDr�r�rrrTr)r�r�r!rEr�r"r"r#�)test_isotonic_2darray_more_than_1_features101102��"�r�cCsbtdddd�\}}t�|�}d|d<|��}t||d�t||�t�j|||d�t||�dS)z�Check that calling fitting function of isotonic regression will not103    overwrite `sample_weight`.104    Non-regression test for:105    https://github.com/scikit-learn/scikit-learn/issues/20508106    rJrr�r�Z107n_featuresrrrN)rrPr�r�rr	rr)r�r!Zsample_weight_originalZsample_weight_fitr"r"r#�6test_isotonic_regression_sample_weight_not_overwritten�s108109r�rvZ1d�2dcCslt�d�}|dkr|�dd�}t�d�}t��||�}|��}t|tj�s'J�|jt	ks.J�t110dg|�dS)z7Check `get_feature_names_out` for `IsotonicRegression`.rJr�r;rZisotonicregression0N)rPrRr�rrZget_feature_names_out�111isinstance�ndarrayr��objectr)rvr�r!Ziso�namesr"r"r#�test_get_feature_names_out�s112113r�cCs�t�d�}tdddd�\}}t�}tjdd��|�||�|�|�}|�|�}Wd�n1s2wYt	||j114�s?J�t	|tj�sGJ�dS)aCheck that `predict` does return the expected output type.115 116    We need to check that `transform` will output a DataFrame and a NumPy array117    when we set `transform_output` to `pandas`.118 119    Non-regression test for:120    https://github.com/scikit-learn/scikit-learn/issues/25499121    ZpandasrJrr�r�)Ztransform_outputN)
rDZimportorskiprr�sklearnZconfig_contextrrrTr�Z	DataFramerPr�)�pdr�r!Z	regressorZX_transr�r"r"r#�'test_isotonic_regression_output_predict�s122	123�r�)Fr�r�r)�numpyrPrDZ
scipy.specialrr�Zsklearn.datasetsrZsklearn.isotonicrrrrZ
sklearn.utilsrZsklearn.utils._testingr	r124rZsklearn.utils.validationrr$r/r4r6r:r@rFrZr_rbrmrorxr�r�r�r�r�r�r�r�r�r�r�r�r�r�r��markZparametrizeZint32r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r"r"r"r#�<module>sr

)
125,%126127128129 130

131 132133
Aluode/PerceptionLabPortable · CoolFace