python:将列表分成等份并在每个部分中添加样本

时间:2016-01-28 17:02:02

标签: python

以下是我的脚本。每个相等的部分都有self.number个样本,in0是输入样本。如下错误:

pn[i] = pn[i] + d

IndexError: list index out of range

这是pn大小的问题吗?如何定义具有特定大小但没有确切数字的列表?

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

2 个答案:

答案 0 :(得分:1)

代码中有许多问题已发布,但是,要点似乎是您想要使用numpy数组而不是迭代列表。

例如,检查pn[i] >= some_value然后将相应条目设置到具有结果(true / false)的另一个列表的if / else案例集可以作为带有数组操作的单行程完成比迭代列表快得多。

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)

输出:

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

使用这种2-d阵列结构,元素方式求和非常简单。元素阈值检查也是如此。加上&#39;矢量化&#39;如果您处理大数据,这些操作具有很大的速度优势。

# 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)

输出:[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)

输出:[真假错]

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

输出:[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)

输出:[真假真]

我希望这会有所帮助。为您的任务使用正确的数据结构可以使分析更容易,更快捷。有一个wealth of documentation on numpy,它是python的标准数字库。

答案 1 :(得分:0)

您将pn初始化为for循环内的空列表,从不向其中分配任何内容,然后尝试访问索引i。索引i没有任何内容,因为pn中的任何索引都没有。

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             

如果您尝试将值d添加到pn列表,则应该执行以下操作:

pn.append(d)