0

I have the following python code which compares two numbers and stores either true or false in the array. However, in stead of storing it like this:

 [True, False, True....]

It stores it like this:

[array([ True]), array([ False]), array([ True])]

Here's the code:

def runSample(file_name):
    samples=open(file_name,'r').readlines()
    result=[]
    check = False
    for line in samples:
        data=json.loads(line);
        check=data[-1]==clf.predict([data[:-1]])
        result.append(check)
    print(result)
8
  • Try result.append(check).item() Commented Jun 23, 2020 at 7:22
  • Gives error. 'NoneType' object has no attribute 'item' Commented Jun 23, 2020 at 7:27
  • Well you have Nones... So add a if check is not None Commented Jun 23, 2020 at 7:28
  • Can you share a sample of samples Commented Jun 23, 2020 at 7:28
  • Why not perform all predictions at once?? Just filter None's and predict all valid data at once. Your current solution will be slow for a lot of data Commented Jun 23, 2020 at 7:30

1 Answer 1

3

Try

if check is not None:
    result.extend(check)

This will add check items to result

Sign up to request clarification or add additional context in comments.

1 Comment

I accept this answer. Though it's a bit slower than append, but it does the job.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.