TransWikia.com

How can I create a new column of binary values from my TfidfVectorizer sparse matrix?

Data Science Asked by Rishabh Sharma on December 7, 2020

I currently have a sparse matrix object of TfidfVectorizer which is of 1000 length. Right now it is displayed like this :

(0, 833)    0.0125811983337
(0, 273)    0.017346359033
(0, 602)    0.0150870927018
(0, 336)    0.123313011424
(0, 921)    0.117637963781
(0, 387)    0.0255455514666
(0, 151)    0.0402355794242
(0, 959)    0.0752284252869
(0, 862)    0.0183447833135
(0, 119)    0.0142898118798
(0, 289)    0.156947194082
(0, 820)    0.484668345462
(0, 95)     0.265061750957
(0, 351)    0.0958489700942
(0, 192)    0.148380396091
(0, 104)    0.104538714112
(0, 558)    0.137032224303
(0, 692)    0.0121762757783

and so on !

I want to create a new column of having a list of length 1000 with binary values in which we have a 1 on the 1st row (index 0) and the 833rd item (column 833 which represents 0,833 0.0125811) and so on..

much like

column 1 2 3 4 5 ... 833 ... 1000

row 0 [0 0 0 0 0 ...  1 ....   0]

and I want to do this for every row for which sparse matrix has been calculated. How can I do this ?

One Answer

var = (0,833)
new_var = np.zeros(1000)
for i in var:
    new_var[i] = 1

I would need your sparse matrix datatype to give you a more precise answer.

Answered by Danny on December 7, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP