I'm supposed to code a function that Receives a labeled dataset and splits the datapoints according to label:
def get_clusters(X: np.ndarray, y: np.ndarray) -> List[np.ndarray]:
"""
Receives a labeled dataset and splits the datapoints according to label
Args:
X (np.ndarray): The dataset
y (np.ndarray): The label for each point in the dataset
Returns:
List[np.ndarray]: A list of arrays where the elements of each array
are datapoints belonging to the label at that index.
Example:
>>> get_clusters(
np.array([[0.8, 0.7], [0, 0.4], [0.3, 0.1]]),
np.array([0,1,0])
)
>>> [array([[0.8, 0.7],[0.3, 0.1]]),
array([[0. , 0.4]])]
"""
idx = np.unique(y,return_index = True)[1]
C = []
for i,label in enumerate(np.unique(y)):
if i != len(idx)-1:
C.append(X[idx[i]:idx[i+1]])
else:
C.append(X[idx[i]:])
return C
This is what I tried so far, but appearently it only works with a sorted input. I'm not allowed to use more then one for loop. Has anyone an idea how I can improve the function?
I hope I included all necessary information.

