-1

The following is my script. Each equal part has self.number samples, in0 is input sample. There is an error as follows:

pn[i] = pn[i] + d

IndexError: list index out of range

Is this the problem about the size of pn? How can I define a list with a certain size but no exact number in it?

for i in range(0,len(in0)/self.number):       
    pn = []
    m = i*self.number      
    for d in in0[m: m + self.number]:           
    pn[i] += d             
    if pn[i] >= self.alpha:
        out[i] = 1
    elif pn[i] <= self.beta:
        out[i] = 0
    else:
       if pn[i] >= self.noise:
      out[i] = 1
       else:
      out[i] = 0
       if pn[i] >= self.noise:
      out[i] = 1
       else:
      out[i] = 0
njzk2
  • 38,969
  • 7
  • 69
  • 107
Betty
  • 65
  • 1
  • 6
  • Please re-indent your code. Indentation is syntactically significant in Python, and your code as posted here doesn't even compile. – Dan Lowe Jan 28 '16 at 17:13
  • `pn = []` is of size `0`. also, your indentation is wrong, this can't run – njzk2 Jan 28 '16 at 17:14
  • Possible duplicate of [Create an empty list in python with certain size](http://stackoverflow.com/questions/10712002/create-an-empty-list-in-python-with-certain-size) – njzk2 Jan 28 '16 at 17:15

2 Answers2

1

There are a number of problems in the code as posted, however, the gist seems to be something that you'd want to do with numpy arrays instead of iterating over lists.

For example, the set of if/else cases that check if pn[i] >= some_value and then sets a corresponding entry into another list with the result (true/false) could be done as a one-liner with an array operation much faster than iterating over lists.

import numpy as np
# for example, assuming you have 9 numbers in your list
# and you want them divided into 3 sublists of 3 values each

# in0 is your original list, which for example might be:
in0 = [1.05, -0.45, -0.63, 0.07, -0.71, 0.72, -0.12, -1.56, -1.92]

# convert into array
in2 = np.array(in0)

# reshape to 3 rows, the -1 means that numpy will figure out
# what the second dimension must be.
in2 = in2.reshape((3,-1))

print(in2)

output:

[[ 1.05 -0.45 -0.63]
 [ 0.07 -0.71  0.72]
 [-0.12 -1.56 -1.92]]

With this 2-d array structure, element-wise summing is super easy. So is element-wise threshold checking. Plus 'vectorizing' these operations has big speed advantages if you are working with large data.

# add corresponding entries, we want to add the columns together,
# as each row should correspond to your sub-lists.
pn = in2.sum(axis=0)  # you can sum row-wise or column-wise, or all elements

print(pn)

output: [ 1. -2.72 -1.83]

# it is also trivial to check the threshold conditions
# here I check each entry in pn against a scalar
alpha = 0.0
out1 = ( pn >= alpha )
print(out1)

output: [ True False False]

# you can easily convert booleans to 1/0
x = out1.astype('int')  # or simply out1 * 1
print(x)

output: [1 0 0]

# if you have a list of element-wise thresholds
beta = np.array([0.0, 0.5, -2.0])
out2 = (pn >= beta)
print(out2)

output: [True False True]

I hope this helps. Using the correct data structures for your task can make the analysis much easier and faster. There is a wealth of documentation on numpy, which is the standard numeric library for python.

svohara
  • 2,159
  • 19
  • 17
  • Thank you, svohara. It's great help, but if the Input in0 has 11 numbers in it,and I only want to use the first 9 numbers to do further calculation, how can I do ? – Betty Feb 01 '16 at 11:40
  • @Betty, you can use slicing to work on whatever part of the array you want to. Here is a link to [indexing with numpy arrays](http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html) that should help. – svohara Feb 03 '16 at 22:55
0

You initialize pn to an empty list just inside the for loop, never assign anything into it, and then attempt to access an index i. There is nothing at index i because there is nothing at any index in pn yet.

for i in range(0, len(in0) / self.number):       
    pn = []
    m = i*self.number      
    for d in in0[m: m + self.number]:           
        pn[i] += d             

If you are trying to add the value d to the pn list, you should do this instead:

pn.append(d)
Dan Lowe
  • 51,713
  • 20
  • 123
  • 112
  • You are correct in that the error is trying to add to a non-initialized list. However, using the append alone won't accumulate the numbers Betty is trying to add together. My suggestion is that numpy is the way to go, but the fix here with minimal changes would be to initialize pn to the correct length of zeros, such as `pn = [0] * self.number`. – svohara Jan 28 '16 at 17:54