0

I'm trying to convert all the values that are zero to one, as referenced in this question. Using the indexing method has no effect on the matrix though.

enter image description here

How would I change all the 0's in my matrix to -1?

Community
  • 1
  • 1
goldisfine
  • 4,742
  • 11
  • 59
  • 83

1 Answers1

2

Your boolean indexing is failing because new_train_data is currently a matrix of strings (dtype='|S3'), not floating point numbers!

You can cast the matrix to dtype=np.float64 to convert it to floats, then use boolean indexing to assign a value of -1 to all elements that are equal to 0.0:

new_training_data = new_training_data.astype(np.float64)
new_training_data[new_training_data == 0] = -1
ali_m
  • 71,714
  • 23
  • 223
  • 298