繁体   English   中英

Python Numpy数组拆分索引超出范围

[英]Python numpy array split index out of range

我正在尝试执行以下代码:

def calculate_squared_dist_sliced_data(self, data, output, proc_numb):
        for k in range(1, self.calc_border):
            print("Calculating",k, "of", self.calc_border, "\n", (self.calc_border - k), "to go!")
            kmeans = KMeansClusterer.KMeansClusterer(k, data)
            print("inertia in round", k, ": ", kmeans.calc_custom_params(data, k).inertia_)
            output.put( proc_numb,  (kmeans.calc_custom_params(self.data, k).inertia_))

    def calculate_squared_dist_mp(self):
        length = np.shape(self.data)[0]
        df_array = []
        df_array[0] = self.data[int(length/4), :]
        df_array[1] = self.data[int((length/4)+1):int(length/2), :]
        df_array[2] = self.data[int((length/2)+1):int(3*length/4), :]
        df_array[3] = self.data[int((3*length/4)+1):int(length/4), :]
        output = mp.Queue()
        processes = [mp.Process(target=self.calculate_squared_dist_sliced_data, args=(df_array[x], output, x)) for x in range(4)]
        for p in processes:
            p.start()
        for p in processes:
            p.join()

        results = [output.get() for p in processes]

当执行df_array[0] = self.data[int(length/4), :] ,出现以下错误:

IndexError: list assignment index out of range

变量lentgh的值为20195(正确)。 我想通过多重处理来完成方法calculate_squared_dist_sliced_data ,因此我需要分割传递给此类的数组data
这是此numpy数组外观的示例:

 [[ 0.          0.          0.02072968 ..., -0.07872599 -0.10147049 -0.44589   ]
 [ 0.         -0.11091352  0.11208243 ...,  0.08164318 -0.02754813
  -0.44921876]
 [ 0.         -0.10642599  0.0028097  ...,  0.1185457  -0.22482443
  -0.25121125]
 ..., 
 [ 0.          0.          0.         ..., -0.03617197  0.00921685  0.        ]
 [ 0.          0.          0.         ..., -0.08241634 -0.05494423
  -0.10988845]
 [ 0.          0.          0.         ..., -0.03010139 -0.0925091
  -0.02145017]]

现在,我想将此孔阵列分成四个相等的部分,以将每个部分分配给一个过程。 但是,当选择行时,会出现上述异常。 有人能帮我吗?
也许对于我想做的事更理论化:

A   B   C   D
1   2   3   4
5   6   7   8
9   5   4   3
1   8   4   3

结果,例如,我想要两个数组,每个数组包含两行:

A  B  C  D
1  2  3  4
5  6  7  8

A   B   C   D
9   5   4   3
1   8   4   3

有人能帮我吗?

列表的长度为0,因此不允许分配的左侧。

要么将其修复为:

df_array = [None, None, None, None]

或使用

df_array.append(self.data[int(length/4), :])
...

代替。

我只是注意到我试图使用像数组这样的列表...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM