c2_w4_Practice lab_Ex2 split_dataset() why cannot pass test

Here is my code.:

def split_dataset(X, node_indices, feature):
    """
    Splits the data at the given node into
    left and right branches
    
    Args:
        X (ndarray):             Data matrix of shape(n_samples, n_features)
        node_indices (list):  List containing the active indices. I.e, the samples being considered at this step.
        feature (int):           Index of feature to split on
    
    Returns:
        left_indices (list): Indices with feature value == 1
        right_indices (list): Indices with feature value == 0
    """
    
    # You need to return the following variables correctly
    left_indices = []
    right_indices = []
    
    ### START CODE HERE ###
    ### END CODE HERE ###
        
    return left_indices, right_indices

Error message: IndexError: list index out of range

Does anyone let me know how to fix it?

Thanks

1 Like

Hi @Changda_Feng, welcome to our community! A couple of comments:

  1. In your use of np.where, it won’t always give you the numbers initially in node_indices which is what you should have used for the return of left_indices and right_indices. I suggest you to make a test case yourself, and make sure the list of node indices you pick does not contain 0.

  2. Make sure the left_indices and right_indices are two lists, and their elements combined are exactly those from node_indices, not more and not less.

  3. It’s better to include a screenshot or text-copy of the complete traceback of the error, instead of just the final line of the error.

  4. We don’t share assignment code here. So I have removed it for you.

Raymond

Hi @Changda_Feng, I am closing this thread, please open a new one if you have any follow-up.

Raymond