标签: boolean-indexing

布尔索引行为的解释

对于二维数组 y:

y = np.arange(20).reshape(5,4)
---
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]
 [16 17 18 19]]
Run Code Online (Sandbox Code Playgroud)

所有索引都选择第 1、第 3 和第 5 行。这是清楚的。

print(y[
    [0, 2, 4],
    ::
])
print(y[
    [0, 2, 4],
    ::
])
print(y[
    [True, False, True, False, True],
    ::
])
---
[[ 0  1  2  3]
 [ 8  9 10 11]
 [16 17 18 19]]
Run Code Online (Sandbox Code Playgroud)

问题

请帮助了解产生结果的规则或机制。

[]用元组替换会产生一个形状为 (0, 5, 4) 的空数组。

y[
    (True, …
Run Code Online (Sandbox Code Playgroud)

python numpy boolean-indexing

9
推荐指数
1
解决办法
116
查看次数

创建新数据帧时收到“布尔系列键将重新索引以匹配数据帧索引”警告

使用以下代码创建新数据框是否有任何潜在的缺点,其中我指定了我希望在新数据框中看到的原始数据框中的非常具体的信息。

df_workloc = (df[df['WorkLoc'] == 'Home'][df['CareerSat'] == 'Very satisfied'][df['CurrencySymbol'] == 'USD'][df['CompTotal'] >= 50000])

我使用了 2019 年 Stack Overflow 调查数据。像这样:

WorkLoc指定受访者的工作地点。

CareerSat详细说明了受访者的职业满意度。

货币符号指定受访者收到付款的货币。

CompTotal指定受访者的总薪酬是多少。

如果有人有一种更干净、更有效的方法来实现具有精致/特定信息的数据框架,我很乐意看到它。我想做的一件事是在同一行中指定补偿总计CompTotal >= 50000 且 <=75000。但是,当我尝试包含第二个布尔值时出现错误。

提前致谢。

python dataframe pandas boolean-indexing

3
推荐指数
1
解决办法
3214
查看次数

布尔值作为索引的 Python 效果 (a[a==0] = 1)

我目前正在实现我在 github 上看到的一些代码。

( https://gist.github.com/karpathy/a4166c7fe253700972fcbc77e4ea32c5 )

这里的兴趣点如下:

def prepro(I):
   """ prepro 210x160x3 uint8 frame into 6400 (80x80) 1D 
   float vector """
   I = I[35:195] # crop
   I = I[::2,::2,0] # downsample by factor of 2
   I[I == 144] = 0 # erase background (background type 1)
   I[I == 109] = 0 # erase background (background type 2)
   I[I != 0] = 1 # everything else (paddles, ball) just set to 1
   return I.astype(np.float).ravel()
Run Code Online (Sandbox Code Playgroud)

作者在这里预处理图像以训练神经网络。我感到困惑的部分是:

I[I == 144] = 0 # …
Run Code Online (Sandbox Code Playgroud)

python arrays indexing numpy boolean-indexing

1
推荐指数
1
解决办法
51
查看次数

标签 统计

boolean-indexing ×3

python ×3

numpy ×2

arrays ×1

dataframe ×1

indexing ×1

pandas ×1