0

My goal is to have one list of standard deviations from the data. I have 13 columns (features) in trainx and three labels(1,2,3) in trainy array corresponding to each row of trainx. My purpose is to find a feature which has minimum standard deviation.

I thought that first I'll calculate standard deviation for each feature for label 1, append it in a list and then will find the minimum std deviation. I tried to write below code blocks but have been unsuccessful so far:

a=[]
for i in range(0,13):
    b=[np.std(trainx[trainy==1,i])]
    print(a.append(b))

It is returning this output:

None
None
None
None
None
None
None
None
None
None
None
None
None

If I try below code:

a=[]
for i in range(0,13):
    b=[np.std(trainx[trainy==1,i])]
    a=a.append(b)
print(a)

it returns:

AttributeError                            Traceback (most recent call last)
<ipython-input-78-6b02e93115a0> in <module>
      3 for i in range(0,13):
      4     b=[np.std(trainx[trainy==1,i])]
----> 5     a=a.append(b)
      6 print(a)

AttributeError: 'NoneType' object has no attribute 'append'

Please help me. Other ways are welcome too.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 3
    The `append()` method always returns `None`. You need `a.append(b)` inside the loop and `print(a)` outside. – quamrana Jun 04 '20 at 12:21
  • 2
    Appending to a list doesn't return the list, or anything: it returns `None`. The list *itself*, however, is altered. Print the list outside your loop. – 9769953 Jun 04 '20 at 12:21

1 Answers1

2

list.append doesn't return the list. It only appends the object to the list. To get the list after you append, just access the list.

a=[]
for i in range(0,13):
    b=[np.std(trainx[trainy==1,i])]
    a.append(b)
    print(a)
noteness
  • 2,440
  • 1
  • 15
  • 15