Read ints from .txt file into numpy array

馋奶兔 提交于 2020-01-30 02:42:34

问题


I'm tring to read 4 ints from simple .txt array as described in this question genfromtxt : read ints from space separated .txt file

but I want it as 2D numpy array.

def read_data():
    data = np.genfromtxt('Skin_NonSkin.txt', dtype=(int, int, int, int))

    print type(data)
    print data.shape
    print data[0]
    print type(data[0])
    print data[0].shape
    print data[0][1]

    return data

it gives me

<type 'numpy.ndarray'>
(245057L,)
(74, 85, 123, 1)
<type 'numpy.void'>
()
85

So how to properly read the data or convert it to 2D nampy array with shape (245057,4)?


回答1:


just use:

data = np.genfromtxt('Skin_NonSkin.txt', dtype=np.int32)

You are creating a 1D array of (int,int,int,int) but what you really want is a 2D array of np.int32.



来源:https://stackoverflow.com/questions/32275834/read-ints-from-txt-file-into-numpy-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!