操作数不能与形状

操作数不能与形状

本文介绍了ValueError:操作数不能与形状(2501,201)(2501,)一起广播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手,所以请保持友好.

I am new to python so please be nice.

我正在尝试将两个Numpy数组与np.logical_or函数进行比较.当我运行以下代码时,
上出现错误Percentile = np.logical_or(data2 > Per1, data2 < Per2)行说明

I am trying to compare two Numpy arrays with the np.logical_or function. When I run the below code an error appears on the
Percentile = np.logical_or(data2 > Per1, data2 < Per2)line stating

data = 1st Array

data2 = 2nd Array

Per1 = np.percentile(data, 10, axis=1)

Per2 = np.percentile(data, 90, axis=1)

Percentile = np.logical_or(data2 > Per1, data2 < Per2)

print(Percentile)

我检查了两个数组的形状,它们看起来都具有相同的形状(2501,201) (2501,201).因此,我在努力理解为什么会发生此错误的过程中,将不胜感激.

I have checked the shape of both arrays and they both appear to be of the same shape (2501,201) (2501,201). Therefore I am struggling to understand why this error occurs, any help would be greatly appreciated.

推荐答案

您需要添加尺寸(通过在Per1Per2中使用[:, None]使其可广播到数据中.

You need to add a dimension (by using [:, None] to Per1 and Per2 to make them broadcastable to data.

Percentile = np.logical_or(data2 > Per1[:, None], data2 < Per2[:, None])

这篇关于ValueError:操作数不能与形状(2501,201)(2501,)一起广播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-10 23:31