如何从NumPy数组中按行选择元素? [英] How to select elements row-wise from a NumPy array?

查看:685
本文介绍了如何从NumPy数组中按行选择元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的numpy数组

I have an array like this numpy array

dd= [[foo 0.567 0.611]
     [bar 0.469 0.479]
     [noo 0.220 0.269]
     [tar 0.480 0.508]
     [boo 0.324 0.324]]

一个如何遍历数组 选择foo并获得0.567 0.611作为单例的浮点数. 然后选择bar并获得0.469 0.479作为单例的浮点数..

How would one loop through array selecting foo and getting 0.567 0.611 as floats as a singleton. Then select bar and getting 0.469 0.479 as floats as a singleton .....

我可以通过使用

dv=  dd[:,1]

'foo'和'bar'元素不是未知变量,它们可以更改.

The 'foo', and 'bar' elements are not unknown variables, they can change.

如果元素位于[1]位置,该如何更改?

How would I change if element is in position [1]?

[[0.567 foo2 0.611]
  [0.469 bar2 0.479]
  [0.220 noo2 0.269]
  [0.480 tar2 0.508]
  [0.324 boo2 0.324]]

推荐答案

您已将 NumPy 标记放在您的Question上,所以我假设您要使用NumPy语法,而我之前的答案并没有不使用.

You have put the NumPy tag on your Question, so i'll assume you want NumPy syntax, which the answer before mine doesn't use.

实际上,如果您希望使用NumPy,那么您可能不希望数组中的字符串,否则您还必须将浮点数表示为字符串.

If in fact you wish to use NumPy, then you likely don't want the strings in your array, otherwise you will also have to represent your floats as strings.

您要查找的是 NumPy语法,用于按行访问2D数组的元素(不包括第一列) .

What you are looking for is the NumPy syntax to access elements of a 2D array by row (and exclude the first column).

该语法为:

M[row_index,1:]        # selects all but 1st col from row given by 'row_index'

问题中的第二种情况- 选择不相邻的列 :

W/r/t the second scenario in your Question--selecting non-adjacent columns:

M[row_index,[0,2]]     # selects 1st & 3rd cols from row given by 'row_index'


您问题中的小复杂之处在于您想为row_index使用字符串,因此有必要删除字符串(以便您可以创建2D NumPy浮点数数组),并将其替换为数字行索引,然后 创建一个查找表,以将字符串与数字行索引进行映射 :


The small complication in your Question is just that you want to use a string for row_index, so it's necessary to remove the strings (so you can create a 2D NumPy array of floats), replace them with numerical row indices and then create a look-up table to map the the strings with the numerical row indices:

>>> import numpy as NP
>>> # create a look-up table so you can remove the strings from your python nested list,
>>> # which will allow you to represent your data as a 2D NumPy array with dtype=float
>>> keys
      ['foo', 'bar', 'noo', 'tar', 'boo']
>>> values    # 1D index array comprised of one float value for each unique string in 'keys'
      array([0., 1., 2., 3., 4.])
>>> LuT = dict(zip(keys, values))

>>> # add an index to data by inserting 'values' array as first column of the data matrix
>>> A = NP.hstack((vals, A))
>>> A
        NP.array([  [ 0., .567, .611],
                    [ 1., .469, .479],
                    [ 2., .22, .269],
                    [ 3., .48, .508],
                    [ 4., .324, .324] ])

>>> # so now to look up an item, by 'key':
>>> # write a small function to perform the look-ups:
>>> def select_row(key):
        return A[LuT[key],1:]

>>> select_row('foo')
      array([ 0.567,  0.611])

>>> select_row('noo')
      array([ 0.22 ,  0.269])

问题中的第二种情况:如果索引列发生更改怎么办?

The second scenario in your Question: what if the index column changes?

>>> # e.g., move index to column 1 (as in your Q)
>>> A = NP.roll(A, 1, axis=1)
>>> A
      array([[ 0.611,  1.   ,  0.567],
             [ 0.479,  2.   ,  0.469],
             [ 0.269,  3.   ,  0.22 ],
             [ 0.508,  4.   ,  0.48 ],
             [ 0.324,  5.   ,  0.324]])

>>> # the original function is changed slightly, to select non-adjacent columns:
>>> def select_row2(key):
        return A[LuT[key],[0,2]]

>>> select_row2('foo')
        array([ 0.611,  0.567])

这篇关于如何从NumPy数组中按行选择元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆