9

Given a `scipy.sparse.coo_matrix` matrix how to determine the index and the maxi...

 3 years ago
source link: https://www.codesd.com/item/given-a-scipy-sparse-coo-matrix-matrix-how-to-determine-the-index-and-the-maximum-value-of-each-line.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Given a `scipy.sparse.coo_matrix` matrix how to determine the index and the maximum value of each line?

advertisements

Given a sparse matrixR of type scipy.sparse.coo_matrix of shape 1.000.000 x 70.000 I figured out that

row_maximum = max(R.getrow(i).data)

will give me the maximum value of the i-th row.

What I need now is the index corresponding to the value row_maximum.

Any ideas how to achieve that?

Thanks for any advice in advance!


getrow(i) returns a 1 x n CSR matrix, which has an indices attribute that gives the row indices of the corresponding values in the data attribute. (We know the shape is 1 x n, so we don't have to deal with the indptr attribute.) So this will work:

row = R.getrow(i)
max_index = row.indices[row.data.argmax()] if row.nnz else 0

We have to deal with the case where row.nnz is 0 separately, because row.data.argmax() will raise an exception if row.data is an empty array.

Related Articles

How to get the maximum value for each group in Oracle?

I've found some solutions for this problem, however, they don't seem to work with Oracle. I got this: I want a view to present only the informations about the oldest person for each team. So, my output should be something like this: PERSON | TEAM | A

How to use the maximum number for each line in awk?

Here is a simple awk script max1.awk to get the max length of all lines. #! /usr/bin/awk BEGIN {max =0 } { if (length($0) > max) { max = length($0)} } END {print max} It can get max length with some test file. awk -f max1.awk test_file Now to move al

How to determine the maximum value among the specific fields of an array of json objects?

This question already has an answer here: Finding the max value of an attribute in an array of objects 10 answers I have an array of json objects: var data = [ {date: "12/27/2012", resolver: "Group 1", volume: 15, escalation: 90, bubbl

pandas: How to find the maximum values ​​for each category in a column

I have a huge municipal library catalog dataset with book title, the library it's in, the library's borough, and the number of times it was loaned out. I want to find the top 3 most loaned books for each neighbourhood. Ideally, I'd get something like

How to find the maximum value of each group and display their information when using & ldquo; group by & rdquo;

For example, i create a table about people contribue to 2 campaigns +-------------------------------------+ | ID Name Campaign Amount (USD) | +-------------------------------------+ | 1 A 1 10 | | 2 B 1 5 | | 3 C 2 7 | | 4 D 2 9 | +------------------

How to select the maximum value for each identifier in the sql selection statement?

Consider these two tables. First Table stageTable stageId-----stageName 1-----------Start 2-----------Planning 3-----------Working 4-----------Review 5-----------Closing Second Table stageProject stageid-------projectId 1-------------1 2-------------

How to find the maximum value of a numpy array, with location restrictions?

I have a numpy array in python 2.7, which I am using the imshow() function to visualise. The code generating the array looks like: from pylab import * r0 = 3.0 S0 = 10.0 x = zeros((101,101)) noiseimg = zeros((101,101)) for i in range(101): for j in r

How do I determine the maximum value of a pid_t?

How can I portably determine the maximum value of the pid_t type? There's no PID_MAX constant on my system. (Note, I mean the maximum value permitted by the data type, not the de facto maximum value the system will assign to processes.) Use case: I'm

R how to find the maximum value by a primary key and remove the duplicates

This question already has an answer here: How to select the row with the maximum value in each group 7 answers I have a dataset as below and want to reduce it by removing rows. In situation where I have more than 1 ATTACH value for the same exact com

How to find the minimum value of each column in a matrix 10x10 vba excel

I need some help in finding the minimum value in each column of my 10x10 matrix. My matrix is not displayed in the spreadsheet so I cant use worksheetfunction.min(cells(1,j),cells(10,j)) for example. Also, i want to replace the cell next to the cell

How to get the maximum value of two years in R

Hi I have data frame as How to create column max_value with max from last 2 years max value Name year value *max_value* A 2012 22 NA A 2012 99 NA A 2013 12 99 A 2014 01 99 A 2015 23 12 A 2016 40 23 A 2017 12 40 B 2012 12 NA B 2013 33 12 B 2013 40 12

How to determine the maximum use of the battery in the on-board system with gcc?

I'm writing the startup code for an embedded system -- the code that loads the initial stack pointer before jumping to the main() function -- and I need to tell it how many bytes of stack my application will use (or some larger, conservative estimate

How to find the maximum values ​​of similar groups in an object

I have an object like this obj = { 'ab-adf': 2, 'ab-d': 3, 'cd-23': 1, 'cd-df': 5, 'ef-a': 3, 'ef-nb': 4 }; Expected output number or strings: 3, 5, 4 from each ab, cd and ef group. I am also looking for solution to print it out like this ab-asdf=8,

How to find the maximum value in a table

var max=0.0d; for(inc=0;inc<array.length;inc++){ if(max<array[inc]) max=array[inc]; } I want to find out the maximum value of an array.The above code is generally we used to find out maximum value of an array. But this code will return 0 if the arra

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK