python - Python numpy数组拆分索引超出范围
问题描述
我正在尝试执行以下代码:
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), :])
...
反而。
推荐阅读
- coldfusion - fileExists() vs CFHTTP 检查远程文件
- oracle - 使用受限选择 oracle 创建光标后插入数据
- python - 如何在 Python 中打开/转换 CSV,使字符串不是 unicode?
- sql - Cognos SQL WITH 子句生成语法错误
- android - 唯一识别安卓屏幕
- caching - CakePHP 3.6 缓存文件引擎的默认目录权限
- android - React Native:通知文本未显示在android中
- ruby-on-rails - 从延迟加载的模型中获取 activerecord 关系
- odoo - odoo 社区是否包含免费的条码扫描器?odo支付的必要功能是什么
- sql-server - 如果与输入数据匹配,则获取所有行,该数据在单个列中存在逗号分隔值