根据布尔逻辑创建数组

编程入门 行业动态 更新时间:2024-10-24 12:30:43
本文介绍了根据布尔逻辑创建数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

问题1

我有一个numpy数组

I have a numpy array

data[:,0:5] Out[98]: array([[ 1.00200300e+09, 1.00000000e+00, 2.00000000e+00, 3.00000000e+00, 4.00000000e+00], [ 1.00200400e+09, 1.00000000e+00, 2.00000000e+00, 4.00000000e+00, 5.00000000e+00], [ 1.00200300e+09, 3.00000000e+00, 4.00000000e+00, 1.00000000e+00, 2.00000000e+00], [ 1.00200400e+09, 4.00000000e+00, 5.00000000e+00, 1.00000000e+00, 2.00000000e+00], [ 5.70580591e+10, 5.70000000e+01, 5.80000000e+01, 5.90000000e+01, 6.00000000e+01], [ 5.70580601e+10, 5.70000000e+01, 5.80000000e+01, 6.00000000e+01, 6.10000000e+01], [ 5.70580591e+10, 5.90000000e+01, 6.00000000e+01, 5.70000000e+01, 5.80000000e+01], [ 5.70580601e+10, 6.00000000e+01, 6.10000000e+01, 5.80000000e+01, 5.70000000e+01]])

每个数据[:,1:5]表示2D网格上的一个位置,总共有64个位置.我正在尝试定义一个给出每个位置的x位置的函数.

Each of data[:,1:5] refers to a position on a 2D grid, of which there are 64 positions in total. I'm trying to define a function that gives the x positions of each position.

我想要的是一种情况,

0 < i <= 8 returns 0.00 8 < i <= 16 returns 0.01 16 < i <= 24 returns 0.02 24 < i <= 32 returns 0.03 32 < i <= 40 returns 0.04 40 < i <= 48 returns 0.05 48 < i <= 56 returns 0.06 56 < i <= 64 returns 0.07

,我希望将这些返回的值放入一个数组中,以便以后可以在np.hstack中使用数据.

and I want those returned values to be put in an array that I can later np.hstack with data.

我正在

data[:,1] Out[103]: array([ 1., 1., 3., 4., 57., 57., 59., 60.])

因此对于此数组,它应生成数组:

so for this array it should produce the array:

[[0.0, 0.0, 0.0, 0.0, 0.07, 0.07, 0.07, 0.07]]

我尝试过:

pos = np.empty([len(data),]) def xpos(col): for i in col: if i <= 8: np.append(pos, [0.0]) if 8 < i <= 16: np.append(pos, [1.0e-02]) if 16 < i <= 24: np.append(pos, [2.0e-02]) if 24 < i <= 32: np.append(pos, [3.0e-02]) if 32 < i <= 40: np.append(pos, [4.0e-02]) if 40 < i <= 48: np.append(pos, [5.0e-02]) if 48 < i <= 56: np.append(pos, [6.0e-02]) else: np.append(pos, [7.0e-02]) return pos xcol1 = xpos(data[:,1])

哪个给:

xcol1 Out[104]: array([ 0., 0., 0., 0., 0., 0., 0., 0.])

它不会产生我在新数组中需要的值.有人知道我在做什么错吗?

It doesn't produce the values that I'm after in the new array. Does anyone know what I'm doing wrong?

问题2

第二个问题是y位置问题,它不能很好地适合于连续分档.

The secondary problem is the y-position problem, which doesn't fit nicely into sequential bins.

i == 1 or 9 or 17 or 25 or 33 or 41 or 49 or 57 returns 0.00 i == 2 or 10 or 18 or 26 or 34 or 42 or 50 or 58 returns 0.01 i == 3 or 11 or 19 or 27 or 35 or 43 or 51 or 59 returns 0.02 i == 4 or 12 or 20 or 28 or 36 or 44 or 52 or 60 returns 0.03 i == 5 or 13 or 21 or 29 or 37 or 45 or 53 or 61 returns 0.04 i == 6 or 14 or 22 or 30 or 38 or 46 or 54 or 62 returns 0.05 i == 7 or 15 or 23 or 31 or 39 or 47 or 55 or 63 returns 0.06 i == 8 or 16 or 24 or 32 or 40 or 48 or 56 or 64 returns 0.07

如此

data[:,1] Out[103]: array([ 1., 1., 3., 4., 57., 57., 59., 60.])

在这种情况下应该返回

[[0.0, 0.0, 0.02, 0.03, 0.0, 0.0, 0.02, 0.03]]

我希望通过以下方式做到这一点:

I was hoping to do this with:

pos = np.empty([len(data),]) def ypos(col): for i in col: if i == 1 or i == 9 or i == 17 or i == 25 or i == 33 or i == 41 or i == 49 or i == 57: np.append(pos, [0.0]) if i == 2 or i == 10 or i == 18 or i == 26 or i == 34 or i == 42 or i == 50 or i == 58: np.append(pos, [1.0e-02]) if i == 3 or i == 11 or i == 19 or i == 27 or i == 35 or i == 43 or i == 51 or i == 59: np.append(pos, [2.0e-02]) if i == 4 or i == 12 or i == 20 or i == 28 or i == 36 or i == 44 or i == 52 or i == 60: np.append(pos, [3.0e-02]) if i == 5 or i == 13 or i == 21 or i == 29 or i == 37 or i == 45 or i == 53 or i == 61: np.append(pos, [4.0e-02]) if i == 6 or i == 14 or i == 22 or i == 30 or i == 38 or i == 46 or i == 54 or i == 62: np.append(pos, [5.0e-02]) if i == 7 or i == 15 or i == 23 or i == 31 or i == 39 or i == 47 or i == 55 or i == 63: np.append(pos, [6.0e-02]) else: np.append(pos, [7.0e-02]) return pos ya = ypos(data[:,1])

但是会再次返回零数组

ya Out[119]: array([ 0., 0., 0., 0., 0., 0., 0., 0.])

推荐答案

方法1::您可以使用 np.digitize -

Approach #1: You can use np.digitize -

bins = np.arange(8,64,8) out = np.digitize(a, bins, right=True)*0.01

样品运行-

案例1:

In [150]: a = np.array([ 1., 1., 3., 4., 57., 57., 59., 60.]) In [151]: bins = np.arange(8,64,8) In [152]: np.digitize(a, bins, right=True)*0.01 Out[152]: array([ 0. , 0. , 0. , 0. , 0.07, 0.07, 0.07, 0.07])

案例2:

In [156]: a Out[156]: array([ -5., 1., 3., 4., 56., 57., 59., 65.]) In [157]: np.digitize(a, bins, right=True)*0.01 Out[157]: array([ 0. , 0. , 0. , 0. , 0.06, 0.07, 0.07, 0.07])

方法2:或者,使用 np.searchsorted -

Approach #2: Alternatively, using np.searchsorted -

np.searchsorted(bins, a)*0.01

更多推荐

根据布尔逻辑创建数组

本文发布于:2023-10-27 18:57:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1534256.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:布尔   数组   逻辑

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!