Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
greyhatguy007
GitHub Repository: greyhatguy007/Machine-Learning-Specialization-Coursera
Path: blob/main/C2 - Advanced Learning Algorithms/week4/C2W4A1/__pycache__/public_tests.cpython-37.pyc
3589 views
B

k�b��@s,ddlZdd�Zdd�Zdd�Zdd	�ZdS)
�NcCs�t�dgd�}||�}|dks(td��t�dgd�}||�}|dksPtd��t�dgddgd�}||�}|dks�td��t�dddddddddg	�}tj||�dd	d
�s�td��tj||d�||�d	d
�s�td��td
�dS)N��
rz$Entropy must be 0 with array of onesz%Entropy must be 0 with array of zeros�z5Entropy must be 1 with same ammount of ones and zerosg��"2�b�?g�����ư>)�atolz&Wrong value. Something between 0 and 1zWrong valuez All tests passed.)�np�array�AssertionError�isclose�print)�target�y�result�r�!/home/jovyan/work/public_tests.py�compute_entropy_tests&rc
Cs>t�ddgddgddgddgddgg�}t�dddddgg�}tj||jfdd�}||ttd��d�\}}t�ddg�t�dddg�d�}t|�tks�td	t|�����t|�tks�td
t|�����t|d�tks�tdt|d�����t|d�tk�stdt|d�����t	|�dk�s4td
t	|�����t	|�dk�sTtdt	|�����t�
||d��s~td|d�d|����t�
||d��s�td|d�d|����t�ddgddgddgddgddgg�}t�dddddgg�}tj|j|fdd�}||ttd��d�\}}t�ddg�t�dddg�d�}t�
||d��rVt�
||d��s^td��tj�dd�dkd}t�dddddddddddgg�}tj||jfdd�}||dddddddgd�\}}t�dddg�t�ddddg�d�}t�
||d��rt�
||d��s2td|�d||�d|�d�����t
d�dS) Nrr)�axis����)�left�rightz)Wrong type for left. Expected: list got: z*Wrong type for right. Expected: list got: z=Wrong type for elements in the left list. Expected: int got: zAWrong type for elements in the right list. Expected: number got: z#left must have 2 elements but got: z$right must have 3 elements but got: rz!Wrong value for right. Expected: z 
got: rz Wrong value for left. Expected: z&Wrong value when target is at index 0.�g�?���	rz2Wrong value when target is at index 0. 
Expected: z 
got: \z, 'right': �\z All tests passed.)rr�concatenate�T�list�range�typer�int�len�allclose�random�randr
)r�XZX_trr�expectedrrr�split_dataset_testsJ
&(  **
,"Fr)cCs�t�ddgddgddgddgddgg�}t�dddddgg�j}ttd��}||||d�}||||d�}|dkrx|dks�td|�d|����t�dddddgg�j}ttd��}||||d�}tj|ddd�s�td	d�d
|����||||d�}tj|ddd��std	d�d
|����ttd��}||||d�}tj|d
dd��sVtd	d
�d
|����||||d�}tj|ddd��s�td	d�d
|����td�dS)Nrrrz=Information gain must be 0 when target variable is pure. Got z and g�G6W�s�?g�����ư>)rz!Wrong information gain. Expected z got: g������?rg�}����?gz All tests passed.)rrrrr rr	r
)rr'r�node_indexesZresult1�result2r
rrr�compute_information_gain_testMs,
$$&&&r,c
Cs�t�ddgddgddgddgddgg�}t�dddddgg�j}ttd��}||||�}|dksntd|����|dd�df}||||�}|dks�td|����|dd�df}||||�}|dks�td|����d|dd�df}||||�}|dk�s
td|����t�dddddgg�j}||||�}|dk�sFtd|����t�dddddgg�j}d	d
dg}||||�}|dk�s�td|����d
}t�dg|g�}t�dg|g�}tj�dd
�dkd}t�dgt|d	�dgt|d	�g�}	|j}ttdd��}t�||||	g�j�	|d�}||||�}|d	k�sJtd|����|j}||||�}|dk�sttd|����t
d�dS)Nrrr�����zQWhen the target variable is pure, there is no best split to do. Expected -1, got zkIf the target is fully correlated with other feature, that feature must be the best split. Expected 0, got zkIf the target is fully correlated with other feature, that feature must be the best split. Expected 1, got zWrong result. Expected 1, got rrrzWrong result. Expected 0, got �dg�?��PzWrong result. Expected 2, got z All tests passed.)rrrrr rr%r&r"�reshaper
)
rr'rr*r
Z	n_samples�X0�X1�X2�X3rrr�get_best_split_testnsN

,r6)�numpyrrr)r,r6rrrr�<module>s4!