How to sort scipy.sparse.csr.csr_matrix according to result of another function?
<p>I am learning about machine learning, more precisely about logistic regression / classification. In my code I have a <code>&lt;class 'scipy.sparse.csr.csr_matrix'&gt;</code> object. I need to sort this sparse matrix, or the <code>SFrame</code> from which it was generated (using ...) according to the result of <code>LogisticRegression.predict_proba</code>, to be precise the second column the arrays, which are contained in the resulting array of <code>predict_proba</code>.</p>

<p>How I generated the sparse matrix:</p>

<pre><code>from sklearn.feature_extraction.text import CountVectorizer

products = sframe.SFrame('...')

train_data, test_data = products.random_split(.8, seed=1)

vectorizer = CountVectorizer(token_pattern=r'\b\w+\b')
test_matrix = vectorizer.transform(test_data['review_clean'])
</code></pre>

<p>How I calculate the probabilities:</p>

<pre><code>sentiment_model.predict_proba(test_matrix)
</code></pre>

<p>(where sentiment_model is a learned classifier, using logistic regression)
This gives me a <code>&lt;class 'numpy.ndarray'&gt;</code>, which looks like this:</p>

<pre><code>[[  4.65761066e-03   9.95342389e-01]
 [  9.75851270e-01   2.41487300e-02]
 [  9.99983374e-01   1.66258341e-05]]
</code></pre>

<p>Here is an example for what the SFrame data looks like, if I print it using the <code>print</code> function:</p>

<pre><code>+-------------------------------+-------------------------------+--------+
|              name             |             review            | rating |
+-------------------------------+-------------------------------+--------+
|   Our Baby Girl Memory Book   | Absolutely love it and all... |  5.0   |
| Wall Decor Removable Decal... | Would not purchase again o... |  2.0   |
| New Style Trailing Cherry ... | Was so excited to get this... |  1.0   |
+-------------------------------+-------------------------------+--------+
+-------------------------------+-----------+
|          review_clean         | sentiment |
+-------------------------------+-----------+
| Absolutely love it and all... |     1     |
| Would not purchase again o... |     -1    |
| Was so excited to get this... |     -1    |
+-------------------------------+-----------+
</code></pre>

<p>So I'd need some function, which can sort that matrix, depending on the results of the predict_proba function.</p>

<p><strong>Question</strong>: How can I sort it like that?</p>

<h3>What I already tried</h3>

<pre><code>sorted(test_matrix)
</code></pre>

<p>results in:</p>

<pre><code>ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all().
</code></pre>

<hr>

<pre><code>sorted(test_matrix_complete, key=lambda x: sentiment_model.predict_proba(x))
</code></pre>

<p>also results in:</p>

<pre><code>ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
</code></pre>

<hr>

<p>The main problem is, that I don't know how to make the connection between the SFrame's data and the sparse matrix efficiently, I guess.</p>