-1

I have this type of list named "preds":

[False  True False  True  True  True  True  True  True  True  True  True
  True False False  True  True  True  True False  True  True False  True
 False False  True False  True  True  True  True  True False False False
 False  True False False  True  True  True  True False False False False
  True False  True False  True  True  True  True  True False  True False
  True]

It's the prediction i obtained with the logistic regression model. I need to convert it into an array containing 1 if the element in the list is "True" and 0 if the element is "False". I have already tried using np.array(preds) or np.asarray(preds) but it doesn't work.

Please can somebody help me finding a solution? I am sorry for the stupid question but I am very new to programming. Thanks in advance.

I already tried using the command of the numpy library like np.array(preds) or np.asarray(preds). I need to obtain a new vector with the same number of elements, in which 1 corresponds to True and 0 corresponds to False

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
frankito
  • 1
  • 1

2 Answers2

1

You can easily convert it into a regular Python list of 1s and 0s with [1 if p else 0 for p in preds], and then pass that to np.array or whatever, e.g.:

import numpy as np
preds = [False, True, False, True,  True,  True,  True,  True,  True,  True,
         True,  True, True,  False, False, True,  True,  True,  True,  False,
         True,  True, False, True,  False, False, True,  False, True,  True,
         True,  True, True,  False, False, False, False, True,  False, False,
         True,  True, True,  True,  False, False, False, False, True,  False,
         True, False, True,  True,  True,  True,  True,  False,  True, False,
         True]
np_preds = np.array([1 if p else 0 for p in preds])
Mark Reed
  • 91,912
  • 16
  • 138
  • 175
  • Thanks for the answer. If I use the command you suggested, I have this result: np_preds = [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]. I think the problem is that my "preds" is made of elements not separeted by commas. – frankito Feb 02 '23 at 09:55
  • The commas are just part of Python's syntax for a literal list; they aren't part of the list's value. It doesn't make sense to have "a list without commas"; it's either a list or it isn't. What's the type of `preds`? Is it just a string? – Mark Reed Feb 02 '23 at 14:47
  • It looks like the `.support_` result from a Recursive Feature Elimination run, in which case it's already an `numpy.ndarray`. What do you want to turn it into? This works for me: `np.array([1 if p else 0 for p in rfe.support_] [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0])` – Mark Reed Feb 02 '23 at 15:03
0

Convert to integer before handing values to numpy:

arr = np.array(list(map(int, preds)))
J_H
  • 17,926
  • 4
  • 24
  • 44