- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是一个Python初学者。我收到一条错误消息“ValueError:操作数无法与形状一起广播”。
这是我的数据:
import numpy as np
spent = np.array([
10, 10, 13, 12, 109, 17, 31, 1, 39, 41, 45,
41, 71, 161, 39, 115, 5, 51, 58, 334, 165, 1032,
40, 52, 21, 68, 79, 482, 10, 265, 60, 67, 12,
53, 188, 32, 397, 51, 17, 156, 100, 85, 53, 95,
68, 308, 53, 675, 78, 27, 219, 45, 45, 30, 61,
16, 72, 80, 96, 1386, 370, 16, 81, 28, 43, 90,
33, 66, 77])
visit = np.array([
19, 13, 16, 16, 18, 9, 12, 3, 15, 16, 16, 3, 4, 11, 11, 11, 11,
12, 12, 12, 13, 13, 14, 14, 15, 15, 5, 6, 6, 7, 7, 7, 7, 7,
17, 17, 8, 8, 8, 4, 4, 13, 8, 4, 4, 9, 20, 10, 11, 11, 14,
12, 12, 15, 12, 12, 13, 13, 13, 13, 14, 14, 14, 15, 16, 16, 18, 11,
6])
我的工作是选择花费>100并一起访问>10。因此,我想在访问次数超过 10 次的人中找到支付超过 100 美元的人。我已经尝试过以下代码。
a=spent[spent>100] & [visit>10]
print(a)
但是,我收到一条错误消息“ValueError:操作数无法与形状一起广播”。你能告诉我如何处理这个问题吗?我只是不知道。
最佳答案
IIUC,您不需要像您所做的那样spent
本身使用面具:
In[16]:
a=(spent>100) & (visit>10)
a
Out[16]:
array([False, False, False, False, True, False, False, False, False,
False, False, False, False, True, False, True, False, False,
False, True, True, True, False, False, False, False, False,
False, False, False, False, False, False, False, True, False,
False, False, False, False, False, False, False, False, False,
False, False, False, False, False, True, False, False, False,
False, False, False, False, False, True, True, False, False,
False, False, False, False, False, False], dtype=bool)
这为您提供了一个 bool 掩码,仅当两个数组中都满足两个条件时,该掩码才为True
,然后您可以使用它来掩码原始数组
因此使用它来对抗spent
:
In[18]:
spent[a]
Out[18]: array([ 109, 161, 115, 334, 165, 1032, 188, 219, 1386, 370])
您的错误是您屏蔽了原始数组,该数组生成的数组与您尝试针对 visit
广播的数组的形状不同:
print(spent[spent>100].shape)
print((visit>10).shape)
(16,)
(69,)
您可以将条件复合到同一个掩码中:
In[20]:
spent[(spent > 100) & (visit > 10)]
Out[20]: array([ 109, 161, 115, 334, 165, 1032, 188, 219, 1386, 370])
产生相同的结果
关于python - Numpy错误: operands could not be broadcast together with shapes 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47265369/
我在我的 AngularJS 应用程序中看到了一些非常奇怪的行为。 制作单个 $broadcast 时,(唯一的)接收器会被触发两次。 对整个应用程序的影响就好像接收器只被触发了一次。 (即只有一件元
以下是我的类(class)中的功能: def labeling(self, value, labelMap, dtype='string'): if dtype.value == 's
只是想澄清一些事情,一些容易实现的目标,一个通过在另一个问题中观察用户试图对广播变量调用 RDD 操作而产生的问题?这是错误的,对吧? 问题是:Spark 广播变量不是 RDD,对吗?这是 Scala
我正在尝试通过聊天室让聊天应用程序运行。 在服务器端我有类似的东西: io.sockets.on('connection', function (socket) { socket.emit('co
我目前正在基于此示例开发一个简单的聊天多房间: https://github.com/Atmosphere/atmosphere-samples/blob/master/samples/chat-mu
我正在构建一个搜索指令,我希望我的应用程序的多个其他指令能够监听对文本搜索的更改。 我试图了解广播和发射之间的区别,以及什么最适合我的目的。 据我了解,$broadcast 和 $emit 之间的区别
Class ProdsTransformer: def __init__(self): self.products_lookup_hmap = {} self.bro
我有多个名为 as 和 bs 的大型数据帧(大约 30GB),一个名为 spp 的相对较小的数据帧(大约 500MB ~ 1GB)。 我试图将 spp 缓存到内存中,以避免多次从数据库或文件中读取数据
我正在以下配置上运行 Spark 应用程序: 1 个 Master 节点,2 个 Worker 节点。 每个工作线程有 88 个核心,因此总数为 88 个。核心数量 176 每个工作线程有 502 G
我想检测 LAN 中 IP:10.x.x.x 掩码:255.0.0.0 的所有设备。我首先写了一个同步 ping 程序来逐个 ping IP,但是太慢了。因此我通过使用异步ping对其进行了改进,然后
我想检测 LAN 中 IP:10.x.x.x 掩码:255.0.0.0 的所有设备。我首先写了一个同步 ping 程序来逐个 ping IP,但是太慢了。因此我通过使用异步ping对其进行了改进,然后
张量(Tensor)、标量(scalar)、向量(vector)、矩阵(matrix) Python Numpy 切片和索引(高级索引、布尔索引、花式索引) Python NumPy
我试图找到这个词的起源,以及它是否有其他名称。我在网上能找到的所有用法都指向 numpy 手册。不知道以前是否使用过? 最佳答案 它最初源自 Yorick ,一种较旧的以数组为中心的编程语言,许多原始
有没有办法让 $broadcast 在初始化阶段将变量传播到 $on? {{testContent}} var app = angular
我可以要一个 $broadcast $on有多个参数, 就像是: $scope.$broadcast('event',$scope.item, $scope.item); 在任何情况下都有可能有这样的
尝试使用 Angular 的 $broadcast,当我测试它并使用字符串“hi”并在同级 Controller 中监视它时,它工作正常,如下所示: //first controller app.co
我遇到一个问题,事件已正确触发,但数据未从 Broadcast::channel 方法内的授权回调函数返回。 该事件如下所示: public function __construct($userId,
我们计划将 Telegram 与我们的项目集成。要求是当某些事件发生时,向一组用户发送通知。我已经用 Telegram 创建了一个测试机器人。我的问题是,是否可以使用机器人来做到这一点,即向用户组发送
我有一个简单的服务/工厂: angular.module('myapp').factory('User', ['$rootScope', function($rootScope) { retu
我们计划将 Telegram 与我们的项目集成。要求是当某些事件发生时,向一组用户发送通知。我已经用 Telegram 创建了一个测试机器人。我的问题是,是否可以使用机器人来做到这一点,即向用户组发送
我是一名优秀的程序员,十分优秀!