Issue
I have features (size = 5011) and labels (size= 15662). One feature is having multiple labels. I was trying to transform the labels using MultiLabelBinarizer but getting error.
Any solution or better approach to overcome this issue?
My Try:
# Create a dictionary to map unique label names to label indices
class_to_label = {label_name: label_index for label_index, label_name in enumerate(unique_label_names)}
# Process each XML annotation file again and extract the labels
labels = []
for filename in annotation_filenames:
annotation_path = os.path.join(annotation_folder, filename)
tree = ET.parse(annotation_path)
root = tree.getroot()
for obj in root.findall('object'):
class_name = obj.find('name').text
label = class_to_label[class_name]
labels.append(label)
# Convert the labels to a NumPy array
labels = np.array(labels)
# Save the labels to a NumPy array
np.save('labels.npy', labels)
import numpy as np
import os
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MultiLabelBinarizer
from skmultilearn.problem_transform import BinaryRelevance
from sklearn.svm import SVC
# Load the features and labels
features = np.load('features.npy')
labels = np.load('labels.npy')
# Transform the labels using MultiLabelBinarizer
mlb = MultiLabelBinarizer()
labels_bin = mlb.fit_transform(labels)
Solution
First your code as it is does not run, consider moving the imports at the beginning :)
I'm not fully sure of what you're trying to do, but the fit(y)
and fit_transform(y)
of MultiLabelBinarizer
except an array of arrays (see the examples here). In your code, labels
looks like a 1-dimensional array of integers.
As it expects an array of arrays it will, for each element of y, consider it as an array and try to iterate over its elements. As you have an array of integer, it complains about not being able to iterate over an integer.
To make your code run, you should replace MultiLabelBinarizer
by labelBinarizer
see here. This way, the expected input format is an array, not an array of arrays.
Answered By - Romain46
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.