0

The relevant part of my code is here *Edit: did not include the outer loop the first time.

for i in range(numAttributes): 
    for w in range(numFeatures):
        if X[i][w]!=0:
            print ("Setting nFeat[",y[i],"][",w,"]")
            self._Nfeat[y[i]][w]=1    
            print self._Nfeat[3][numFeatures-1]
            if self._Nfeat[3][numFeatures-1]!=0:
                print("y[i] is: ", y[i]," and w is: ", w)
                sys.exit()

essentially self._Nfeat[3][numFeatures-1] is being set to 1 when it shouldnt be. The print statements and final if statement are all put in for debugging.

The last 4 things output are:

0
setting nFeat[1][265676]
1
y[i] is 1 and w is 26576

I have also placed other if self._Nfeat[3][numFeatures-1]!=0 checks in other places and determined it is definitely happening right before the current placement.

This has left me very confused as self._Nfeat[3][numFeatures-1] seems to be being changed when it shouldnt.

the list self._Nfeat is initialized in init with:

self._Nfeat=[]

At the start of the method it is then reinitialized with

self._Nfeat=[[0]*numFeatures]*numClasses

There is no where else in the code which deals with this list at all.

I am new to python and would appreciate any help greatly

Str8WaveDave
  • 199
  • 1
  • 13
  • You need to be more specific as your question is not clear. Apparently `y[i]` is `1`, yet you check for `nFeat[3]` instead of `nFeat[1]`. What is the reason for that? – Selcuk Sep 28 '16 at 07:26
  • I know that nFeat[3][265676] should not be set to 1 ever. However it is happening at this point in the code. The confusion comes from it been set to 1 at this part when nFeat[1] is being referred to. – Str8WaveDave Sep 28 '16 at 07:33
  • You are most probably cloning the same list to generate your nested lists. In this case all the lists will point to the same memory location. Please post how do you instantiate `self._Nfeat` in the first place. – Selcuk Sep 28 '16 at 07:35
  • the class init has: self._Nfeat = [] the method this is called in has: self._Nfeat=[[0]*numFeatures]*numClasses – Str8WaveDave Sep 28 '16 at 07:40
  • I will also clarify, this method is only called once, and there is no where else in the code which deals with _Nfeat – Str8WaveDave Sep 28 '16 at 07:47
  • Your inner list is been duplicated across all sublists. You should do: `self._Nfeat=[[0]*numFeatures for _ in range(numClasses)]` – Moses Koledoye Sep 28 '16 at 08:02

1 Answers1

0

The problem is with your instantiation:

self._Nfeat=[[0]*numFeatures]*numClasses 

This will copy (not clone!) the same list numClasses times, which will result in the bug you are experiencing. Change it to something like:

self._Nfeat = [[0] * numFeatures for _ in range(numClasses)] 
Selcuk
  • 57,004
  • 12
  • 102
  • 110