0
$\begingroup$

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 would like to create a new column that has a list of length 1000 with binary values in which we have a 1 on the 1st row (index 0) and the $833^{rd}$ 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 would like to do this for every row for which sparse matrix has been calculated. How can I do this?

$\endgroup$

1 Answer 1

0
$\begingroup$
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.

$\endgroup$
1
  • $\begingroup$ sparse = TfidfVectorizer(max_features = 1000).fit_transform(dataframe['column1']). sparse is the sparse matrix object that is created ! $\endgroup$ Feb 6, 2019 at 10:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.