mrgloom
mrgloom

Reputation: 21682

Read ints from .txt file into numpy array

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)?

Upvotes: 1

Views: 3389

Answers (1)

derricw
derricw

Reputation: 7036

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.

Upvotes: 4

Related Questions