getting indices in numpy

前端 未结 1 779
忘掉有多难
忘掉有多难 2021-01-20 17:25

Can someone find out what is wrong with the code below?

import numpy as np
data = np.recfromcsv(\"data.txt\", delimiter=\" \", names=[\'name\', \'types\', \'         


        
相关标签:
1条回答
  • 2021-01-20 18:05

    With Python3.2 and numpy 1.7, this line works

    indices = np.where((data.name == b'david') * data.types.startswith(b'height'))
    

    data displays as:

    rec.array([(b'david', b'weight_2005', 50),...], 
          dtype=[('name', 'S5'), ('types', 'S11'), ('value', '<i4')])
    

    type(data.name[0]) is <class 'bytes'>.

    b'height' works in Python2.7 as well.


    another option is to convert all the data to unicode (Python 3 strings)

    dtype=[('name','U5'), ('types', 'U11'), ('value', '<i4')]
    dataU=data.astype(dtype=dtype)
    indices = np.where((dataU.name == 'david') * dataU.types.startswith('height'))
    

    or

    data = np.recfromtxt('data.txt', delimiter=" ", 
        names=['name', 'types', 'value'], dtype=dtype)
    

    It looks like recfromcsv does not take a dtype, but recfromtxt does.

    0 讨论(0)
提交回复
热议问题