Decision Tree Clasifier with torch.Tensor

username: pymon
cooked: <p>I want to use a Decision Tree with data from audio. I get this torch.Tensor for X_train</p>
<pre><code class="lang-auto">[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]]), .......
</code></pre>
<p>The size of those tensor are variable</p>
<pre><code class="lang-auto">X_train[0].size() -&gt; torch.Size([1, 121])
X_train[1].size() -&gt; torch.Size([1, 123])
X_train[2].size() -&gt; torch.Size([1, 106])
</code></pre>
<p>When I try to apply a Decisiontreeclasifier I have a lot of problems.</p>

username: ptrblck
cooked: <p>I assume you are trying to use <code>sklearn.tree.DecisionTreeClassifier</code> with PyTorch tensors while numpy arrays are expected?<br>
Could you describe your use case and why you want to use tensors instead of numpy arrays?</p>

username: pymon
cooked: <p>Yes, sorry. <code>sklearn.tree.DecisionTreeClassifier</code>.<br>
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.</p>

username: ptrblck
cooked: <p>What kind of errors are you running into when transforming the tensors to numpy arrays?</p>

username: pymon
cooked: <p>I do that:</p>
<pre><code class="lang-auto">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]]), ........
</code></pre>
<pre><code class="lang-auto">from sklearn.tree import DecisionTreeClassifier
clf=DecisionTreeClassifier()
clf.fit(X_train,y_train)
</code></pre>
<p>I have this error:</p>
<pre><code class="lang-auto">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.
</code></pre>

username: ptrblck
cooked: <p>Try to pass a plain numpy array to the method instead of a nested object which apparently contains arrays in different shapes.</p>

username: pymon
cooked: <p>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.</p>

username: ptrblck
cooked: <p>As described in the <a href="https://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeClassifier.html"><code>sklearn.tree.DecisionTreeClassifier</code> docs</a> the input to <code>fit</code> should be an array in the shape <code>[n_samples, n_features]</code>. 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.<br>
These questions are also unrelated to PyTorch, so I would recommend to continue the discussion in a scikit-specific discussion board.</p>