o
    tBh_                     @   s  d dl Z d dlZd dlZd dlZd dl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 d d	lmZmZ d d
lmZ eeeegZeeg ZeddgddgddgddgddgddggZeg dZejd ZejddZ ejddd k!e"Z#ej$dddZ%eg d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.d&d' Z/d(d) Z0d*d+ Z1d,d- Z2d.d/ Z3ej45d0ed1d2 Z6ej45d0ed3d4 Z7ej45d5ed6d7 Z8d8d9 Z9ej45d0ed:d; Z:ej45d0ed<d= Z;ej45d0ed>d? Z<ej45d0ed@dA Z=ej45d0eej45dBdCdDgej45dEdCdDgdFdG Z>ej45dHdIdJdK Z?dLdM Z@dNdO ZAdPdQ ZBdRdS ZCdTdU ZDej45dVdWeg dXg dYgeg dYg dYged dggedWdWgfdWdZgeg dXg dYgeg d[g d[ged dWggedWdZgfdedd gddggeddgddgged dggeddgggd\d] ZEej45d^d_dWdgddZggd`fdagdbdc ZFddde ZGdfdg ZHdhdi ZIej45djedkdl ZJdS )m    N)load_digits	load_iris)train_test_split)cross_val_score)assert_almost_equal)assert_array_equal)assert_array_almost_equal)
GaussianNBBernoulliNB)MultinomialNBComplementNB)CategoricalNB      )r   r   r   r   r   r   )
      )sizer      )   d   )r   r   r   r   r   r   c                  C   s   t  } | ttt}t|t | t}| t}tt	
||d tjtdd t  jttddgd W d    d S 1 sBw   Y  d S )N   z;The target label.* in y do not exist in the initial classesmatchr   r   classes)r	   fitXypredictr   predict_probapredict_log_probar   nplogpytestraises
ValueErrorpartial_fit)clfy_predy_pred_probay_pred_log_proba r-   u/var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/sklearn/tests/test_naive_bayes.pytest_gnb(   s   


"r/   c                  C   sT   t  } | tt tjtdd t| j| j	 W d    d S 1 s#w   Y  d S )Nz!Attribute `sigma_` was deprecatedr   )
r	   r   r   r   r%   warnsFutureWarningr   sigma_var_r)   r-   r-   r.   test_gnb_var?   s
   "r5   c                  C   sL   t  tt} ttddgd | jd t  tt	} t| j
 d d S )Nr         @r   r   )r	   r   r   r   r   r#   arrayclass_prior_X1y1sumr4   r-   r-   r.   test_gnb_priorG   s   r<   c                  C   s   t d} t tt}t tt| }t|j|j t|j|j t	
tjd } t jtt| d}t jttddg| d d}|jtt| d d t|j|j t|j|j t	dtjd d}t j|tjd d}t t| t| }t tt|}t|j|j t|j|j d	S )
z5Test whether sample weights are properly used in GNB.r   r   sample_weightr   r   r   r>      )	minlengthN)r#   onesr	   r   r   r   r   theta_r3   rngrandshaper(   randintbincount)swr)   clf_swclf1clf2indr>   clf_duplr-   r-   r.   test_gnb_sample_weightP   s"   
rO   c                  C   X   t tddgd} d}tjt|d | tt W d   dS 1 s%w   Y  dS )z:Test whether an error is raised in case of negative priorsg             @priorszPriors must be non-negativer   N	r	   r#   r7   r%   r&   r'   r   r   r   r)   msgr-   r-   r.   test_gnb_neg_priorsp   
   "rW   c                  C   sZ   t tddgdtt} t| ddggtddggd t| jtddg dS )	z6Test whether the class prior override is properly used333333?gffffff?rR   皙g[9h?gs\?r   N)	r	   r#   r7   r   r   r   r   r!   r8   r4   r-   r-   r.   test_gnb_priorsy   s   r[   c                  C   s|   t ddgddgddgddgddgddgddgddgd	d	gd
d
gg
} t g d}t g d}t|d}|| | d S )Nr   r   r   r   r      r   )
g{Gz?Q?gQ?{Gz?)\(?ra   gQ?r`   rb           )
r   r   r   r_   r   r      r   	   r   rR   )r#   r7   r	   r   )r   rS   Yr)   r-   r-   r.   test_gnb_priors_sum_isclose   s"   
rg   c                  C   sX   t tg dd} d}tjt|d | tt W d   dS 1 s%w   Y  dS )z`Test whether an error is raised if the number of prior is different
    from the number of class)      ?rh   rh   rh   rR   -Number of priors must match number of classesr   NrT   rU   r-   r-   r.   test_gnb_wrong_nb_priors   s
   "rj   c                  C   rP   )z?Test if an error is raised if the sum of prior greater than onerQ         ?rR   z!The sum of the priors should be 1r   NrT   rU   r-   r-   r.   test_gnb_prior_greater_one   rX   rl   c                  C   sD   t tddgd} | tt | ddggtdgks J dS )z@Test if good prediction when class prior favor largely one classg{Gz?gGz?rR   rZ   r   N)r	   r#   r7   r   r   r   r    r4   r-   r-   r.   test_gnb_prior_large_bias   s   $rm   c                  C   sP   d} d}d}t dtjd f}t| |||\}}||ks J ||ks&J dS )z4Test when the partial fit is called without any datar   rc   rk   r   r   N)r#   emptyr   rF   r	   _update_mean_variance)prev_pointsmeanvarx_emptytmeantvarr-   r-   r.   "test_gnb_check_update_with_no_data   s   rv   c                  C   s   t  tt} t  tttt}t| j|j t| j	|j	 t| j
|j
 t  tdd dd d f tdd d tt}|tdd d tdd d  t| j|j t| j	|j	 t| j
|j
 d S )Nr   r   r   )r	   r   r   r   r(   r#   uniquer   rC   r3   r8   )r)   clf_pfclf_pf2r-   r-   r.   test_gnb_partial_fit   s   2 rz   c                     sP   t  } | j| j  fdddD }t|d |d  t|d |d  d S )Nc                    s(   g | ]}t  |  |  qS r-   )r	   r   r    ).0fr   r   r-   r.   
<listcomp>   s   ( z9test_gnb_naive_bayes_scale_invariance.<locals>.<listcomp>)绽|=r   g    _Br   r   r   )r   datatargetr   )irislabelsr-   r}   r.   %test_gnb_naive_bayes_scale_invariance   s
   r   DiscreteNaiveBayesc                 C   s4   |   tt}tttg dd |jd d S )N)r   r   r   r6   r   )r   X2y2r   r#   r$   r7   class_log_prior_)r   r)   r-   r-   r.   test_discretenb_prior   s   r   c                 C   s  |  }| ddgddgddggg d |  }|jddgddgddggg dddgd t|j|j | tu rMtt|jD ]}t|j| |j|  q>nt|j|j |  }|jddggdgddgd |ddggdg |ddggdg t|j|j | tu rtt|jD ]$}t|j| j	|j| j	 tt
j|j| ddt
j|j| dd qt|jd d t
ddg t|jd d t
ddg t|jd d t
ddg t|jd d t
ddg d S t|j|j d S )Nr   r   r   r   r   r   axisr   )r   r(   r   class_count_r   rangelencategory_count_feature_count_rF   r#   r;   r7   )r   rK   rL   iclf3r-   r-   r.   test_discretenb_partial_fit   s:   "*"r   
NaiveBayesc                 C   s   t jtdd |  tt W d    n1 sw   Y  |  }|jttttd t jtdd |jtttdd W d    d S 1 sJw   Y  d S )Nz8classes must be passed on the first call to partial_fit.r   r   .is not the same as on last call to partial_fit*   )	r%   r&   r'   r(   r   r   r#   rw   arange)r   r)   r-   r-   r.   $test_NB_partial_fit_no_first_classes  s   "r   c                  C   s|  g dg dg dg} ddgddgddgg}g d}t ttg| |gD ];\}}| ||}||d	d  d
ks;J ||d gjdksHJ t||d d
 jddt	
ddgd q#g d}t ttg| |gD ]O\}}| ||}||dd jdksJ ||d d
 jdksJ tt	||d gd tt	||d	 gd tt	t	|jd qld S )N)r   r   r   r   r   r   )r   r   r   r   r   r   r_   )r   r   r   r   r   )r   r   r   rk   r   r   r   r   )r   r   )r   r   )zipr
   r   r   r    r!   rF   r   r;   r#   r7   r   expr   )X_bernoulliX_multinomialr   r   r   r)   r-   r-   r.   test_discretenb_predict_proba$  s.   &r   c                 C   sR   |  }|j dd |dgdgdggg d t|j}t|tddg d S )NF)	fit_priorr   r   r   r   r         ?)
set_paramsr   r#   r   r   r   r7   )r   r)   priorr-   r-   r.   test_discretenb_uniform_priorE  s
   r   c                 C   s   | ddgd}| dgdgdggg d t|j}t|tddg d}tjt|d | dgdgdggg d	 W d    n1 sGw   Y  d
}tjt|d |j	dgdggddgg dd W d    d S 1 sqw   Y  d S )Nr   class_priorr   r   r   ri   r   r   r   r   r   r   )
r   r#   r   r   r   r7   r%   r&   r'   r(   )r   r)   r   rV   r-   r-   r.   test_discretenb_provide_priorQ  s   ""r   c           	      C   s   t  }t|j|jddd\}}}}d g dfD ]+}| |d}||j|j | |d}|j||g dd ||| t|j|j qd S )N皙?i  )	test_sizerandom_state)rY   rY   r   r   r   r   )r   r   r   r   r   r(   r   r   )	r   r   
iris_data1
iris_data2iris_target1iris_target2r   clf_fullclf_partialr-   r-   r.   .test_discretenb_provide_prior_with_partial_fitd  s   

r   c                 C   s  g dg dg dg dg}g d}t jg dt jd}||  }|  j|||d}t||g d |  }|j|d d	 |d d	 g d
|d d	 d |j|d	d |d	d |d	d d |j|dd  |dd  |dd  d t||g d d S )Nr   r   r   r   r   )r   r   r   r   r   r   r   r   )dtyper=   )r   r   r   r   r   r   r?   r   )r#   r7   float64r;   r   r   r    r(   )r   r   r   r>   r)   r-   r-   r.   (test_discretenb_sample_weight_multiclassy  s   .((r   use_partial_fitFTtrain_on_single_class_yc                 C   s   g dg dg dg}g d}|r|d d }|d d }t tt|}t|}|  }|r7|j|||d n||| ||d d |d ksLJ g d	}|D ]+}	t||	d }
|
d u r_qRt|
t	j
ro|
jd |ksnJ qR|
D ]}|jd |ks|J qqqRd S )
Nr   r   r   )r   r   r   r   r   r   r   )classes_r   r   r   feature_log_prob_)sortedlistsetr   r(   r   r    getattr
isinstancer#   ndarrayrF   )r   r   r   r   r   r   num_classesr)   attribute_namesattribute_name	attributeelementr-   r-   r.   )test_discretenb_degenerate_one_class_case  s.   r   kind)densesparsec                 C   s  | dkrt }n
| dkrtjt }t }d}tjt|d || t	 W d    n1 s/w   Y  ||t	
|}t|t	 ||}||}tt||d t }|j|d d t	d d tt	d ||dd t	dd  ||dd  t	dd   |
|}t|t	 ||}	||}
tt|	|
d t|	| t|
| t }|j|t	tt	d |
|}t|t	 ||}||}tt||d t|| t|| d S )	Nr   r   z!Negative values in data passed tor   r   r   r   r   )r   scipyr   
csr_matrixr   r%   r&   r'   r   r   r    r   r!   r"   r   r#   r$   r(   rw   )r   r   r)   rV   r*   r+   r,   rL   y_pred2y_pred_proba2y_pred_log_proba2r   y_pred3y_pred_proba3y_pred_log_proba3r-   r-   r.   	test_mnnb  sD   


&










r   c                  C   sN  t ddgddgg} t ddg}t }t  tdt |j| |g dd W d    n1 s4w   Y  |ddggdksEJ |ddggdksQJ |ddggdks]J t  tdt |ddggdg W d    n1 s|w   Y  |ddggdksJ |ddggdksJ |ddggdksJ d S )Nr   r   errorr   r   r   )	r#   r7   r   warningscatch_warningssimplefilterRuntimeWarningr(   r    )r   r   r)   r-   r-   r.   !test_mnb_prior_unobserved_targets  s"   

r   c                  C   s   t g dg dg dg dg} t g d}tdd}|| | t dd	g}tt |j| t g d
g dg}tt |j| t g dg}t ddgg}|t | }t|	|| d S )N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   rk   alphag      ?rh   )r   g?皙?r   r   r   )UUUUUU?UUUUUU?r   r   r   r   g@fg;u?gy?)
r#   r7   r
   r   r   r   r   r   r;   r!   )r   rf   r)   r   feature_probX_testunnorm_predict_probar!   r-   r-   r.   test_bnb  s$   
r   c                  C   s   t g dg dg dg dg dg} t g d}tdd}|| | t |jd }t t |jd | jd	 d	fj	}t
|j||  d S )
N)r   r   r   r   r   r   r   )r   r   r   )r   r   r   r   r   rk   r   rQ   r   )r#   r7   r
   r   r$   r   tiler   rF   Tr   r   )r   rf   r)   numdenomr-   r-   r.   test_bnb_feature_log_probH  s   (
$r   c                  C   s~  t g dg dg dg dg} t g d}t g dg dg}t |j}t |j}tdD ]}t ||  ||< || ||   ||< q3td	d
}t	d}t
jt|d ||  | W d    n1 sow   Y  || | t g dg dg}t|j| t ddg}	t|j|	 t g d}
t|j|
 t|j| td	dd}|| | t|j| d S )Nr   r   r   r   r   )qq?qq?r   r   r   r   )UUUUUU?r   UUUUUU?r   r   r   r   rk   r   z8Negative values in data passed to ComplementNB (input X)r   )r   r   r   r   r   r   r   r   )r   r_   r   r   r   r   T)r   norm)r#   r7   zerosrF   r   r$   r;   r   reescaper%   r&   r'   r   r   r   r   feature_all_r   r   )r   rf   thetaweightsnormed_weightsr   r)   rV   feature_countclass_countfeature_allr-   r-   r.   test_cnb_  s<   

r   c                  C   s  t  } | ttt}t|t tddgddgg}tddg}t ddd} | || t| jtddg td	d
gg}tdg}t	
d}tjt|d | | W d    n1 sdw   Y  tjt|d | || W d    n1 sw   Y  tddgg}tddgg}| }	t| |||	  t| j|jd ksJ td	d	gd	dgd	d	gddgg}tg d}t ddd} | || t| td	d	ggtdg t| jtddg dD ]N}
td	d	gd	dgd	d	gddgg}tg d}tg d|
 }t ddd} | j|||d t| td	d	ggtdg t| jtddg qd S )Nr   r_   r   r   F)r   r   r   r   r   r   z9Negative values in data passed to CategoricalNB (input X)r   r   qq?r   )rk   rY   r   g-C6?)r   r   r   皙?r=   )r   r   r   r   r    r   r#   r7   n_categories_r   r   r%   r&   r'   r;   r   r!   r   r   rF   )r)   r*   X3y3r   r   	error_msgX3_testbayes_numeratorbayes_denominatorfactorr>   r-   r-   r.   test_categoricalnb  sN   

"$"$r	  zDmin_categories, exp_X1_count, exp_X2_count, new_X, exp_n_categories_r   )r   r   r   r   r_   )r   r   r   r   c                 C   s   t ddgddgddgddgg}t g d}t dg}tdd| d}||| |j\}	}
t|	| t|
| ||}t|| t|j| d S )Nr   r   r   Fr   r   min_categories)r#   r7   r   r   r   r   r    r  )r  exp_X1_countexp_X2_countnew_Xexp_n_categories_X_n_categoriesy_n_categoriesexpected_predictionr)   X1_countX2_countpredictionsr-   r-   r.   &test_categoricalnb_with_min_categories  s   ""




r  zmin_categories, error_msg)bad_arg%'min_categories' should have integralz"'min_categories' should have shape)rk   r  c                 C   s~   t ddgddgddgddgg}t g d}tdd| d}tjt|d ||| W d    d S 1 s8w   Y  d S )Nr   r   r   Fr
  r   )r#   r7   r   r%   r&   r'   r   )r  r  r   r   r)   r-   r-   r.   (test_categoricalnb_min_categories_errors  s   "
"r  c            	      C   sp  t ddgddgg} t ddg}tdd}d}tjt|d |j| |ddgd W d    n1 s5w   Y  tjt|d || | W d    n1 sRw   Y  t ddgddgg}t|	| | t
dd}tjt|d |j| |ddgd W d    n1 sw   Y  tjt|d || | W d    n1 sw   Y  t dd	gddgg}t|	| | tdd}tjt|d || | W d    n1 sw   Y  t d
dgdd
gg}t|	| | tj| } tdd}tjt|d || | W d    n	1 sw   Y  t ddgddgg}t|	| | t
dd}tjt|d || | W d    n	1 sOw   Y  t dd	gddgg}t|	| | t ddgddgg} t ddg}td}tdd}t
dd}tdd}tjt|d || | W d    n	1 sw   Y  tjt|d || | W d    n	1 sw   Y  tjt|d || | W d    n	1 sw   Y  tdd}t
dd}tjt|d |j| |ddgd W d    n	1 sw   Y  tjt|d |j| |ddgd W d    d S 1 s1w   Y  d S )Nr   r   rc   r   zFalpha too small will result in numeric errors, setting alpha = 1.0e-10r   r   r   r   rk   :Smoothing parameter alpha = -1.0e-01. alpha should be > 0.rZ   )r#   r7   r
   r%   r0   UserWarningr(   r   r   r!   r   r   r   r   r   r   r   r&   r'   )	r   r   nbrV   probexpected_msgb_nbm_nbc_nbr-   r-   r.   
test_alpha  s   









$r"  c            	      C   s  t ddgddgg} t ddg}t ddg}t|d}|j| |ddgd t ddgddgg}t|jt | t d	d
gddgg}t|| | t ddg}t|d}d}tj	t
|d || | W d    n1 suw   Y  d}t |d dg}t|d}|j| |ddgd t| |dgdd t g d}t|d}td}tj	t
|d || | W d    d S 1 sw   Y  d S )Nr   r   r   r   r   r   r   g333333?grq?r   gS?gևX?rk   rZ   r  r   r      )decimal)rk   rQ   g      @zAalpha should be a scalar or a numpy array with shape [n_features])r#   r7   r   r(   r   r   r$   r!   r%   r&   r'   r   _check_alphar   r   )	r   r   r   r  r   r  r   r  	ALPHA_MINr-   r-   r.   test_alpha_vector^  s8   



"r'  c                  C   sB  t dd\} }t|dk|dk}| | || }}ttdd| |dd}| dks-J ttdd||dd}| d	ks@J ttdd| d
k|dd}| dksUJ ttdd|d
k|dd}| dksjJ tt | |dd}| dks{J ttdd| |dd}| dksJ tt ||dd}| dksJ d S )NT)
return_X_yr   r   r   r   )cvgQ?gGz?r_   g(\?gq=
ףp?gp=
ף?r   )var_smoothingg{Gz?)r   r#   
logical_orr   r   rq   r
   r	   )r   r   
binary_3v8X_3v8y_3v8scoresr-   r-   r.   test_check_accuracy_on_digits  s"   r0  	Estimatorc                 C   sl   t ddgddgg}t ddg}|  ||}tjtdd |j W d    d S 1 s/w   Y  d S )Nr   r   r   r_   r   z`n_features_` was deprecatedr   )r#   r7   r   r%   r0   r1   n_features_)r1  r   r   estr-   r-   r.   test_n_features_deprecation  s   "r4  )Kr   numpyr#   scipy.sparser   r%   r   sklearn.datasetsr   r   sklearn.model_selectionr   r   sklearn.utils._testingr   r   r   sklearn.naive_bayesr	   r
   r   r   r   DISCRETE_NAIVE_BAYES_CLASSESALL_NAIVE_BAYES_CLASSESr7   r   r   randomRandomStaterD   normalr9   astypeintr:   rG   r   r   r/   r5   r<   rO   rW   r[   rg   rj   rl   rm   rv   rz   r   markparametrizer   r   r   r   r   r   r   r   r   r   r   r   r   r   r	  r  r  r"  r'  r0  r4  r-   r-   r-   r.   <module>   s    
.	 	
		

1
!



2
6/G5	



A*"