【问题标题】:PYTHON: Percentage of sorting level of a numpy 2D arrayPYTHON:numpy 2D 数组的排序级别百分比
【发布时间】:2017-06-17 06:28:37
【问题描述】:

单调性百分比是数组按升序或降序排列的方式。 我需要一种 python(最好是 numpy)的方式来实现这一点,请考虑以下示例

   array([[2,3,4,6,5] # 60% sorted
         [1,2,3,4,5] # 100% sorted
         [0,2,4,8,10]# 100% sorted
         [0,2,4,8,10]]# 100% sorted
         /  | |  \  \
        /   | |   \  \
    100% 100% 80% 80% 100%

Monotonicity percentage is average(80,100,100,100,100,100,80,80,100)

我想在 AI 启发式中运行它,速度非常重要,感谢您的帮助

已编辑 这就是我现在所拥有的,它只是返回一个布尔值,它是一维的

def mncity(arr):
    dx = np.diff(arr)
    return np.all(dx <= 0) or np.all(dx >= 0)

我一般是 PYTHON 的新手

【问题讨论】:

  • 抱歉,这不是编码服务。你试过什么了?你在哪里挣扎?
  • 感谢@ImanolLuengo

标签: python arrays numpy artificial-intelligence heuristics


【解决方案1】:

您可以使用zip()list comprehension 执行类似的操作:

def get_pourcent(a, order='ascending'):
    if order == 'ascending':
        # Check if every element is inferior than his next in the list
        b = [1 if j < v else 0 for j, v in zip(a, a[1:])]
        # Get how much zeros in b
        zeros = len(b) - sum(b)
        percent = (1 - (float(zeros)/len(a)))*100

    elif order == 'descending':
        b = [1 if j > v else 0 for j, v in zip(a, a[1:])]
        zeros = sum(b)
        percent = (float(zeros)/len(a))*100

    else:
        return None

    return '"%s": %.2f%% sorted' % (order, percent)


# Test
tests = [('ascending', [2,3,4,6,5]), ('ascending', [1,2,3,4,5]),
 ('ascending', [0,2,4,8,10]), ('descending', [2,3,4,6,5]), ('descending', [0,2,4,8,10])]

for k, v in tests:
    print v, get_pourcent(v, order=k)

输出:

[2, 3, 4, 6, 5] "ascending": 80.00% sorted
[1, 2, 3, 4, 5] "ascending": 100.00% sorted
[0, 2, 4, 8, 10] "ascending": 100.00% sorted
[2, 3, 4, 6, 5] "descending": 20.00% sorted
[0, 2, 4, 8, 10] "descending": 0.00% sorted

编辑:

tests = [[ 2, 4, 0, 8], [ 4, 24, 0, 16], [ 16, 2, 16, 32], [ 16, 2, 16, 128]]

for k in tests:
    print get_pourcent(k)

将输出:

"ascending": 75.00% sorted
"ascending": 75.00% sorted
"ascending": 75.00% sorted
"ascending": 75.00% sorted

【讨论】:

  • 我喜欢这个解决方案,也有同样的想法,但这段代码可能会更清晰
  • @DanielSanchez 为什么不呢? .给我你的建议,我会编辑它:-) 我愿意接受任何建议。不要犹豫:-)
  • 当代码相同但更改 1 个单个运算符时使用 if ,我也只会返回百分比结果:)
  • 是的,但是代码阅读起来会更复杂我认为OP无法理解。但是,我将使用新代码添加新编辑。谢谢:-)
  • 哦!对不起,我省略了第二个更高的数组大括号
【解决方案2】:

您想沿轴(0 或 1)取 diff,然后查看沿该轴的正值的比例:

(np.diff(a, axis=1) > 0).mean(axis=1)

对于在 diff 和 mean 中带有 axis=0 的列也可以这样做。

我不确定为什么第一行需要 80 % 而不是 75 %,但如果你愿意,你可以这样做:

(np.diff(a, axis=1) > 0).sum(axis=1) / a.shape[1]

对于列:

(np.diff(a, axis=0) > 0).sum(axis=0) / a.shape[0]

【讨论】:

  • @Camilleri 你能完成这个答案成为一个可调用的吗?
  • 好吧,你可以定义一个返回我写的行的函数,这很简单。
【解决方案3】:

这是我构建并按预期工作的功能

def merge(mat):
  monotone = 0
  matrix = numpy.copy(mat)
  for i in range(4):
    m_vertical = 4 if numpy.size(numpy.where(numpy.diff(matrix[:, i]) < 0)[0]) == 0 else numpy.where(numpy.diff(matrix[:, i]) < 0)[0][0]+1
    m_horizontal = 4 if numpy.size(numpy.where(numpy.diff(matrix[i]) < 0)[0]) == 0 else numpy.where(numpy.diff(matrix[i]) < 0)[0][0]+1
    monotone += (m_vertical + m_horizontal)*3.125
  return monotone

【讨论】:

    猜你喜欢
    • 2021-06-11
    • 2020-09-05
    • 1970-01-01
    • 1970-01-01
    • 2021-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-13
    相关资源
    最近更新 更多