Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python "Too many indices for array"

Tags:

python

numpy

I am reading a file in python using pandas and then saving it in a numpy array. The file has the dimension of 11303402 rows x 10 columns. I need to split the data for cross validation and for that I sliced the data into 11303402 rows x 9 columns of examples and 1 array of 11303402 rows x 1 col of labels. The following is the code:

tdata=pd.read_csv('train.csv')
tdata.columns='Arrival_Time','Creation_Time','x','y','z','User','Model','Device','sensor','gt']

User_Data = np.array(tdata)
features = User_Data[:,0:9]
labels = User_Data[:,9:10]

The error comes in the following code:

classes=np.unique(labels)
idx=labels==classes[0]
Yt=labels[idx]
Xt=features[idx,:]

On the line:

Xt=features[idx,:]

it says 'too many indices for array'

The shapes of all 3 data sets are:

print np.shape(tdata) = (11303402, 10)
print np.shape(features) = (11303402, 9)
print np.shape(labels) = (11303402, 1)

If anyone knows the problem, please help.

like image 420
Farhan Javed Avatar asked May 11 '16 12:05

Farhan Javed


People also ask

How do you fix too many indexes in an array?

The Python "IndexError: too many indices for array" occurs when we specify too many index values when accessing a one-dimensional numpy array. To solve the error, declare a two-dimensional array or correct the index accessor.

What does Too many indices for array mean?

The indexerror: too many indices for an array means that you have a declared an array in a different dimension and trying to index it in another dimension. For example, suppose you have declared a numpy array in a single dimension and try to access the elements of an array in 2 dimensional.24-Nov-2021.

How do you fix IndexError too many indices for array array is 1 dimensional but 2 were indexed?

This occurs when you are trying to access the elements of a one-dimensional numpy array as a 2D array. To avoid this error, you need to mention the correct dimensions of the array. This error is thrown by Python 'numpy array' library when you try to access a single-dimensional array into multiple dimensional arrays.

How do you flatten an array in Python?

By using ndarray. flatten() function we can flatten a matrix to one dimension in python. order:'C' means to flatten in row-major. 'F' means to flatten in column-major.


1 Answers

The problem is idx has shape (11303402,1) because the logical comparison returns an array of the same shape as labels. These two dimensions use all of the indexes in features. The quick work around is

Xt=features[idx[:,0],:]
like image 200
Keith Prussing Avatar answered Oct 22 '22 02:10

Keith Prussing