def compute_information_gain(X, y, node_indices, feature):
"""
Compute the information of splitting the node on a given feature
Args:
X (ndarray): Data matrix of shape(n_samples, n_features)
y (array like): list or ndarray with n_samples containing the target variable
node_indices (ndarray): List containing the active indices. I.e, the samples being considered in this step.
Returns:
cost (float): Cost computed
"""
# Split dataset
left_indices, right_indices = split_dataset(X, node_indices, feature)
# Some useful variables
X_node, y_node = X[node_indices], y[node_indices]
X_left, y_left = X[left_indices], y[left_indices]
X_right, y_right = X[right_indices], y[right_indices]
# You need to return the following variables correctly
information_gain = 0
### START CODE HERE ###
# moderator edit: code removed
### END CODE HERE ###
return information_gain
Information Gain from splitting the root on brown cap: [[-6.1720857 -6.1720857 -6.1720857]
[-6.1720857 -inf -6.1720857]
[-6.1720857 -inf -inf]
[-6.1720857 -inf -inf]
[-6.1720857 -6.1720857 -6.1720857]
[ -inf -6.1720857 -6.1720857]
[ -inf -inf -inf]
[-6.1720857 -inf -6.1720857]
[ -inf -6.1720857 -inf]
[-6.1720857 -inf -inf]]
Information Gain from splitting the root on tapering stalk shape: [[-2.79609 -2.79609 -2.79609]
[-2.79609 -inf -2.79609]
[-2.79609 -inf -inf]
[-2.79609 -inf -inf]
[-2.79609 -2.79609 -2.79609]
[ -inf -2.79609 -2.79609]
[ -inf -inf -inf]
[-2.79609 -inf -2.79609]
[ -inf -2.79609 -inf]
[-2.79609 -inf -inf]]
Information Gain from splitting the root on solitary: [[-2.97060452 -2.97060452 -2.97060452]
[-2.97060452 -inf -2.97060452]
[-2.97060452 -inf -inf]
[-2.97060452 -inf -inf]
[-2.97060452 -2.97060452 -2.97060452]
[ -inf -2.97060452 -2.97060452]
[ -inf -inf -inf]
[-2.97060452 -inf -2.97060452]
[ -inf -2.97060452 -inf]
[-2.97060452 -inf -inf]]
Traceback (most recent call last)
in
9
10 # UNIT TESTS
—> 11 compute_information_gain_test(compute_information_gain)
~/work/public_tests.py in compute_information_gain_test(target)
97 result2 = target(X, y, node_indexes, 0)
98
—> 99 assert result1 == 0 and result2 == 0, f"Information gain must be 0 when target variable is pure. Got {result1} and {result2}"
100
101 y = np.array([[0, 1, 0, 1, 0]]).T
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Even though its seems like all the code is correct, it’s still displaying value error. Please help me in approaching this problem