Decision Tree Clasifier with torch.Tensor

I want to use a Decision Tree with data from audio. I get this torch.Tensor for X_train

[tensor([[ 0,  0,  0,  0,  0,  0,  0,  0, 11,  0,  0, 28,  0,  0, 11,  0,  0, 20,
           0,  4,  0,  0,  0,  0,  0,  0, 15,  0,  0,  0,  0,  4,  4,  0, 21,  0,
           9,  0,  9,  0,  0,  0,  7,  0,  0, 25, 15, 15, 21,  0, 20,  7, 18,  0,
           0, 18, 31,  4,  4, 13,  0, 11,  0, 26,  0,  4,  4, 26, 14,  0, 11,  4,
           0, 19,  0, 21,  0, 20,  0,  0, 10,  0,  7,  0,  0, 31,  0,  4,  4,  0,
           8,  0, 18, 27, 27, 11,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 25,  0,
           0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  4]]),
    tensor([[ 0,  0,  0,  0,  0,  0,  0, 10, 21,  0, 20, 20,  5, 26, 26,  0,  4,  4,
           0,  0,  7,  0,  0, 25,  0, 17,  4,  4, 19, 19, 11,  4,  4, 26, 21, 21,
           4,  0,  9,  0,  0,  7,  0, 24,  0,  0, 24,  0, 31,  4,  4,  7, 20,  0,
           4,  4,  0,  0,  0,  0,  7,  0, 27,  0,  0,  0, 18,  0,  0, 31,  0,  0,
           4,  4,  0, 24, 24,  0,  0,  7,  0,  0,  0,  0, 13,  0,  0,  0,  4, 18,
          18, 15, 15,  0,  0, 17,  0, 11,  4,  4,  0, 26, 14, 14,  0,  0,  7,  0,
           0,  0,  0,  0,  0, 26,  0,  0,  0,  0,  0,  0,  0,  0,  4]]),
    tensor([[ 0,  0,  0,  0,  0,  0,  0,  0, 25, 25, 14, 21,  0, 27, 18, 10,  4,  4,
          16,  0,  0, 15,  0,  0,  0, 24,  0,  0,  7,  0,  0,  0, 12,  0,  0,  0,
           4,  8,  0,  0, 11,  0,  4,  0, 17,  0,  0, 11,  0,  0, 22,  0,  0, 26,
           0,  4,  0, 15, 20,  0,  4,  4,  4,  0, 25,  0,  0,  0, 19,  0,  0,  0,
           0,  7, 18,  0,  0, 18,  0,  4,  0, 32, 21, 21,  0, 21,  0,  0,  0,  0,
           0,  0,  0, 25, 11,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  4]]), .......

The size of those tensor are variable

X_train[0].size() -> torch.Size([1, 121])
X_train[1].size() -> torch.Size([1, 123])
X_train[2].size() -> torch.Size([1, 106])

When I try to apply a Decisiontreeclasifier I have a lot of problems.

I assume you are trying to use sklearn.tree.DecisionTreeClassifier with PyTorch tensors while numpy arrays are expected?
Could you describe your use case and why you want to use tensors instead of numpy arrays?

Yes, sorry. sklearn.tree.DecisionTreeClassifier.
tensors appear to extract the outputs of capable intermediaries of a neural network. When I try to convert them to a numpy array, I’m having trouble with the clasify algorithm.

What kind of errors are you running into when transforming the tensors to numpy arrays?

I do that:

for i in (range(len(X_train))): 
    X_train[i] = X_train[i].detach().numpy()

[array([[ 0,  0,  0,  0,  0,  0,  0,  0, 11,  0,  0, 28,  0,  0, 11,  0,
          0, 20,  0,  4,  0,  0,  0,  0,  0,  0, 15,  0,  0,  0,  0,  4,
          4,  0, 21,  0,  9,  0,  9,  0,  0,  0,  7,  0,  0, 25, 15, 15,
         21,  0, 20,  7, 18,  0,  0, 18, 31,  4,  4, 13,  0, 11,  0, 26,
          0,  4,  4, 26, 14,  0, 11,  4,  0, 19,  0, 21,  0, 20,  0,  0,
         10,  0,  7,  0,  0, 31,  0,  4,  4,  0,  8,  0, 18, 27, 27, 11,
          0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 25,  0,  0,  0,  0,  0,
          0,  0,  0,  0,  0,  0,  0,  0,  4]]),
 array([[ 0,  0,  0,  0,  0,  0,  0, 10, 21,  0, 20, 20,  5, 26, 26,  0,
          4,  4,  0,  0,  7,  0,  0, 25,  0, 17,  4,  4, 19, 19, 11,  4,
          4, 26, 21, 21,  4,  0,  9,  0,  0,  7,  0, 24,  0,  0, 24,  0,
         31,  4,  4,  7, 20,  0,  4,  4,  0,  0,  0,  0,  7,  0, 27,  0,
          0,  0, 18,  0,  0, 31,  0,  0,  4,  4,  0, 24, 24,  0,  0,  7,
          0,  0,  0,  0, 13,  0,  0,  0,  4, 18, 18, 15, 15,  0,  0, 17,
          0, 11,  4,  4,  0, 26, 14, 14,  0,  0,  7,  0,  0,  0,  0,  0,
          0, 26,  0,  0,  0,  0,  0,  0,  0,  0,  4]]),
 array([[ 0,  0,  0,  0,  0,  0,  0,  0, 25, 25, 14, 21,  0, 27, 18, 10,
          4,  4, 16,  0,  0, 15,  0,  0,  0, 24,  0,  0,  7,  0,  0,  0,
         12,  0,  0,  0,  4,  8,  0,  0, 11,  0,  4,  0, 17,  0,  0, 11,
          0,  0, 22,  0,  0, 26,  0,  4,  0, 15, 20,  0,  4,  4,  4,  0,
         25,  0,  0,  0, 19,  0,  0,  0,  0,  7, 18,  0,  0, 18,  0,  4,
          0, 32, 21, 21,  0, 21,  0,  0,  0,  0,  0,  0,  0, 25, 11,  0,
          0,  0,  0,  0,  0,  0,  0,  0,  0,  4]]), ........
from sklearn.tree import DecisionTreeClassifier
clf=DecisionTreeClassifier()
clf.fit(X_train,y_train)

I have this error:

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 2 dimensions. The detected shape was (13, 1) + inhomogeneous part.

Try to pass a plain numpy array to the method instead of a nested object which apparently contains arrays in different shapes.

but the arrays have different sizes I don’t quite understand how to plain these arrays. I have tried with reshape(1,-1) but it doesn’t work.

As described in the sklearn.tree.DecisionTreeClassifier docs the input to fit should be an array in the shape [n_samples, n_features]. If each sample in the batch contains a different number of features I would recommend checking why that’s the case since missing features might not be expected.
These questions are also unrelated to PyTorch, so I would recommend to continue the discussion in a scikit-specific discussion board.