Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to make a p * q matrix from a numpy.nd array(Where p*q = n)?

I have these two array

data1 = [["ab","bc","ca"], ["bc","cd","da"], ["be","cd","db"]]
topics1 = [["ab","db"],["be","cd"]]

I have to find the intersection of each topic for each document. Here is my attempt.

mat11 = []
for i in range(len(data1)):
  for j in range(len(topics1)):
    mat1 = len(list(set(data1[i]) & set(topics1[j])))
    mat11.append(mat1)

mat 11 is a list of (len(data1) * len(topic1)) elements.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

mat11

enter image description here

I want it to be as a matrix of shape [len(data1) * len(topic1)]. So I have done the following.

import numpy as np
img_mat = np.array( mat11 )
shape = ( len(data1), len(topics1) )
img_mat.reshape( shape )

which is giving me this output

enter image description here

But it’s not the shape which I wanted,

enter image description here

How to make this a 3*2 matrix. Moreover my main aim is to get a dataframe which looks like

enter image description here

>Solution :

import numpy as np
img_mat = np.array( mat11 )
shape = ( len(data1), len(topics1) )
l = np.matrix(img_mat.reshape(shape))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading