chaps
chaps

Reputation: 158

Why do these two arrays have the same shape?

So I am trying to create an array and then access the columns by name. So I came up with something like this:

import numpy as np
data = np.ndarray(shape=(3,1000),
                  dtype=[('x',np.float64),
                  ('y',np.float64),
                  ('z',np.float64)])

I am confused as to why

data.shape 

and data['x'].shape

both come back as (3,1000), this is causing me issues when I'm trying to populate my data fields

data['x'] = xvalues

where xvalues has a shape of (1000,). Is there a better way to do this?

Upvotes: 1

Views: 147

Answers (2)

elelias
elelias

Reputation: 4761

The reason why it comes out the same is because 'data' has a bit more structure than the one revealed by shape.

Example:

data[0][0] returns: (6.9182540632428e-310, 6.9182540633353e-310, 6.9182540633851e-310)

while data['x'][0][0]: returns 6.9182540632427993e-310

so data contains 3 rows and 1000 columns, and the element of that is a 3-tuple.

data['x'] is the first element of that tuple of all combinations of 3 rows and 1000 columns, so the shape is (3,1000) as well.

Upvotes: 1

postelrich
postelrich

Reputation: 3496

Just set shape=(1000,). The triple dtype will create 3 columns.

Upvotes: 0

Related Questions