Multi-Feature One-Hot-Encoder with varying amount of feature instances Let's assume we have data instances like this:
[
    [15, 20, (&quot;banana&quot;,&quot;apple&quot;,&quot;cucumber&quot;), ...],
    [91, 12, (&quot;orange&quot;,&quot;banana&quot;), ...],
    ...
]

I am wondering how I can encode the third element of these datapoints. For multiple features values we could use sklearn's OneHotEncoder, but as far as I could find out, it cannot handle inputs of different length.
Here is what I've tried out:
X = [[15, 20, (&quot;banana&quot;,&quot;apple&quot;,&quot;cucumber&quot;)], [91, 12, (&quot;orange&quot;,&quot;banana&quot;)]]

ct = ColumnTransformer(
    [
        (&quot;genre_encoder&quot;, OneHotEncoder(), [2])
    ],
    remainder='passthrough'
)
print(ct.fit_transform(X))

This will only output
[[1.0 0.0 15 20]
 [0.0 1.0 91 12]]

as expected, because the tuples are handled as the possible values this feature can be represented with.
We can't embed our features directly (like [15, 12, &quot;banana&quot;, &quot;apple&quot;, &quot;cucumber&quot;]), because

we don't know how many instances of this feature we will have (two? three?)
each position would be interpreted as an own feature and thus if we had banana in the first nominal slot in one datapoint and in the second one in our second nominal slot, they would not count to the same &quot;pool of values&quot; a feature can embody

Example:
X = [[&quot;banana&quot;,&quot;apple&quot;,&quot;cucumber&quot;], [&quot;orange&quot;,&quot;banana&quot;, &quot;cucumber&quot;]]
enc = OneHotEncoder()
print(enc.fit_transform(X).toarray())

[[1. 0. 1. 0. 1.]
 [0. 1. 0. 1. 1.]]

This representation contains 5 slots instead of 4, because the first slot is interpreted as using banana or orange, the second one as apple or banana and the last one only has the option cucumber.
(This would also not solve the problem of having different amounts of feature values per datapoint. And replacing empty ones with None does not solve the problem either, because then None faces this positional ambiguity.)
Any idea how to encode those &quot;Multi-Muliti-&quot;features, that can take multiple values and consist of a varying amount of elements? Thank you in advance!
I think you can transform this into a text preprocessing problem and then use CountVectorizer. You basically build &quot;documents&quot; by putting together all the words in your raw data and then use CountVectorizer on those documents.
from sklearn.feature_extraction.text import CountVectorizer

X = [[&quot;banana&quot;,&quot;apple&quot;,&quot;cucumber&quot;], [&quot;orange&quot;,&quot;banana&quot;, &quot;cucumber&quot;]]
# Create documents
X_ = [' '.join(x) for x in X]
enc = CountVectorizer()
print(enc.fit_transform(X_).toarray())

Returns
[[1 1 1 0]
 [0 1 1 1]]

which has 4 different values as you expected.