Path: blob/main/C2 - Advanced Learning Algorithms/week4/C2W4A1/public_tests.py
3585 views
import numpy as np12def compute_entropy_test(target):3y = np.array([1] * 10)4result = target(y)56assert result == 0, "Entropy must be 0 with array of ones"78y = np.array([0] * 10)9result = target(y)1011assert result == 0, "Entropy must be 0 with array of zeros"1213y = np.array([0] * 12 + [1] * 12)14result = target(y)1516assert result == 1, "Entropy must be 1 with same ammount of ones and zeros"1718y = np.array([1, 0, 1, 0, 1, 1, 1, 0, 1])19assert np.isclose(target(y), 0.918295, atol=1e-6), "Wrong value. Something between 0 and 1"20assert np.isclose(target(-y + 1), target(y), atol=1e-6), "Wrong value"2122print("\033[92m All tests passed.")2324def split_dataset_test(target):25X = np.array([[1, 0],26[1, 0],27[1, 1],28[0, 0],29[0, 1]])30X_t = np.array([[0, 1, 0, 1, 0]])31X = np.concatenate((X, X_t.T), axis=1)3233left, right = target(X, list(range(5)), 2)34expected = {'left': np.array([1, 3]),35'right': np.array([0, 2, 4])}3637assert type(left) == list, f"Wrong type for left. Expected: list got: {type(left)}"38assert type(right) == list, f"Wrong type for right. Expected: list got: {type(right)}"3940assert type(left[0]) == int, f"Wrong type for elements in the left list. Expected: int got: {type(left[0])}"41assert type(right[0]) == int, f"Wrong type for elements in the right list. Expected: number got: {type(right[0])}"4243assert len(left) == 2, f"left must have 2 elements but got: {len(left)}"44assert len(right) == 3, f"right must have 3 elements but got: {len(right)}"4546assert np.allclose(right, expected['right']), f"Wrong value for right. Expected: { expected['right']} \ngot: {right}"47assert np.allclose(left, expected['left']), f"Wrong value for left. Expected: { expected['left']} \ngot: {left}"4849X = np.array([[0, 1],50[1, 1],51[1, 1],52[0, 0],53[1, 0]])54X_t = np.array([[0, 1, 0, 1, 0]])55X = np.concatenate((X_t.T, X), axis=1)5657left, right = target(X, list(range(5)), 0)58expected = {'left': np.array([1, 3]),59'right': np.array([0, 2, 4])}6061assert np.allclose(right, expected['right']) and np.allclose(left, expected['left']), f"Wrong value when target is at index 0."6263X = (np.random.rand(11, 3) > 0.5) * 1 # Just random binary numbers64X_t = np.array([[0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0]])65X = np.concatenate((X, X_t.T), axis=1)6667left, right = target(X, [1, 2, 3, 6, 7, 9, 10], 3)68expected = {'left': np.array([1, 3, 6]),69'right': np.array([2, 7, 9, 10])}7071assert np.allclose(right, expected['right']) and np.allclose(left, expected['left']), f"Wrong value when target is at index 0. \nExpected: {expected} \ngot: \{left:{left}, 'right': {right}\}"727374print("\033[92m All tests passed.")7576def compute_information_gain_test(target):77X = np.array([[1, 0],78[1, 0],79[1, 0],80[0, 0],81[0, 1]])8283y = np.array([[0, 0, 0, 0, 0]]).T84node_indexes = list(range(5))8586result1 = target(X, y, node_indexes, 0)87result2 = target(X, y, node_indexes, 0)8889assert result1 == 0 and result2 == 0, f"Information gain must be 0 when target variable is pure. Got {result1} and {result2}"9091y = np.array([[0, 1, 0, 1, 0]]).T92node_indexes = list(range(5))9394result = target(X, y, node_indexes, 0)95assert np.isclose(result, 0.019973, atol=1e-6), f"Wrong information gain. Expected {0.019973} got: {result}"9697result = target(X, y, node_indexes, 1)98assert np.isclose(result, 0.170951, atol=1e-6), f"Wrong information gain. Expected {0.170951} got: {result}"99100node_indexes = list(range(4))101result = target(X, y, node_indexes, 0)102assert np.isclose(result, 0.311278, atol=1e-6), f"Wrong information gain. Expected {0.311278} got: {result}"103104result = target(X, y, node_indexes, 1)105assert np.isclose(result, 0, atol=1e-6), f"Wrong information gain. Expected {0.0} got: {result}"106107print("\033[92m All tests passed.")108109def get_best_split_test(target):110X = np.array([[1, 0],111[1, 0],112[1, 0],113[0, 0],114[0, 1]])115116y = np.array([[0, 0, 0, 0, 0]]).T117node_indexes = list(range(5))118119result = target(X, y, node_indexes)120121assert result == -1, f"When the target variable is pure, there is no best split to do. Expected -1, got {result}"122123y = X[:,0]124result = target(X, y, node_indexes)125assert result == 0, f"If the target is fully correlated with other feature, that feature must be the best split. Expected 0, got {result}"126y = X[:,1]127result = target(X, y, node_indexes)128assert result == 1, f"If the target is fully correlated with other feature, that feature must be the best split. Expected 1, got {result}"129130y = 1 - X[:,0]131result = target(X, y, node_indexes)132assert result == 0, f"If the target is fully correlated with other feature, that feature must be the best split. Expected 0, got {result}"133134y = np.array([[0, 1, 0, 1, 0]]).T135result = target(X, y, node_indexes)136assert result == 1, f"Wrong result. Expected 1, got {result}"137138y = np.array([[0, 1, 0, 1, 0]]).T139node_indexes = [2, 3, 4]140result = target(X, y, node_indexes)141assert result == 0, f"Wrong result. Expected 0, got {result}"142143n_samples = 100144X0 = np.array([[1] * n_samples])145X1 = np.array([[0] * n_samples])146X2 = (np.random.rand(1, 100) > 0.5) * 1147X3 = np.array([[1] * int(n_samples / 2) + [0] * int(n_samples / 2)])148149y = X2.T150node_indexes = list(range(20, 80))151X = np.array([X0, X1, X2, X3]).T.reshape(n_samples, 4)152result = target(X, y, node_indexes)153154assert result == 2, f"Wrong result. Expected 2, got {result}"155156y = X0.T157result = target(X, y, node_indexes)158assert result == -1, f"When the target variable is pure, there is no best split to do. Expected -1, got {result}"159print("\033[92m All tests passed.")160161