-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

1
  • arr = numpy.array(your_list) should work just fine, just rewrite your list from True/False to 1 and 0 first with normal list comprehension? Commented Feb 1, 2023 at 19:39

2 Answers 2

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])
Sign up to request clarification or add additional context in comments.

3 Comments

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.
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?
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])
0

Convert to integer before handing values to numpy:

arr = np.array(list(map(int, preds)))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.