Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 comparing each rows of two matrix
#11
Here is how you could print every pair of identical rows
from collections import defaultdict
import itertools as itt
import numpy as np
a=np.matrix([[1,2,3],[42,68,69],[1,2,3],[85,89,95]])
b=np.matrix([[42,68,69],[1,2,3],[85,89,95], [42,68,69]])
 
def group_lines(m):
    d = defaultdict(list)
    for i, row in enumerate(m.A):
        d[tuple(row)].append(i)
    return d

def identical_rows(ma, mb):
    da, db = (group_lines(m) for m in (ma, mb))
    for row, na in da.items():
        if row in db:
            yield from itt.product(na, db[row])

if __name__ == '__main__':
    for i, j in identical_rows(a, b):
        print(i, j, a.A[i])
The result is
Output:
3 2 [85 89 95] 1 0 [42 68 69] 1 3 [42 68 69] 0 1 [1 2 3] 2 1 [1 2 3]
PhysChem likes this post
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  display graph in columns and rows william888 1 172 Jul-02-2019, 10:19 AM
Last Post: dataman
  Creating new rows and adding them to empty data frame kapilan15 0 163 May-31-2019, 10:19 AM
Last Post: kapilan15
  Inverse of singular matrix Divanova94 4 657 May-11-2019, 02:40 AM
Last Post: scidam
  matrix by vector mcgrim 8 290 May-02-2019, 10:39 AM
Last Post: ichabod801
  Extract of matrix subpart using a deep copy paul18fr 2 184 May-02-2019, 06:49 AM
Last Post: paul18fr
  Help in adding confusion matrix Aashish 5 489 Apr-15-2019, 11:45 PM
Last Post: scidam
  counting the occurence of a specified number in a numpy-matrix PhysChem 1 217 Apr-03-2019, 01:37 PM
Last Post: PhysChem
  function matrix mcgrim 14 684 Mar-19-2019, 09:28 PM
Last Post: mcgrim
  PyCM 1.8 released: Machine learning library for confusion matrix statistical analysis sepandhaghighi 0 299 Jan-05-2019, 12:36 PM
Last Post: sepandhaghighi
  Drop rows from data with zero value Devilish 3 501 Dec-27-2018, 02:06 AM
Last Post: Devilish

Forum Jump:


Users browsing this thread: 1 Guest(s)