python - 'numpy.ndarray' object is not callable when retrieving matrix diagonal. -


i have 291*291 matrix , automatically retrieved values (0,1), (1,2), (2,3).... (n-1, n). there straightforward way using loops or function?

the matrix cosine-similarity between texts in data base:

bodies = [d['body'] d in data] tfidf = vectorizer.fit_transform(bodies)  matrix =(tfidf * tfidf.t).a 

since want create vector, how attempting it:

vector = [] in range(len(data) -1):     vector.append(matrix(i, i+1)) 

but following error:

typeerror: 'numpy.ndarray' object not callable 

any ideas of how fix it?

as matrix square, can use numpy.diagonal offset of 1 acquire desired values

mat.diagonal(offset = 1) 

the positive offset of 1 acquires diagonal 1 above matrix's main diagonal.


mini demo:

mat = numpy.ones((3,3)) mat[0,1] = 2 mat[1,2] = 3 print(mat.diagonal(offset = 1)) 

outputs:

[ 2.  3.] 

Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -