How to Slice Numpy Arrays ?

A forum for general discussion of the Python programming language.

How to Slice Numpy Arrays ?

Postby tnknepp » Mon Mar 18, 2013 5:01 pm

I have a numpy array that is 840x2048, but I only want columns 1 and 51:1076. Every time I try to slice out these columns I fail. I feel like my brain is failing right now, and I should be able to do this.

I've tried:

Code: Select all
data2 = data[:,[1,51:1076]]    # Fail
data2 = data[:][:,(1,51:1076)] # Fail
data2 = data[np.ix_( [:],[1] )] # Worth a try
Last edited by Yoriz on Tue Mar 19, 2013 1:26 pm, edited 1 time in total.
Reason: Changed title to be more specific, original was 'Slicing Arrays'
Python: 2.7 via Anaconda
Numpy: 1.7
Pandas: 0.11
OS: Windows 7
IDE: Spyder/IPython
User avatar
tnknepp
 
Posts: 119
Joined: Mon Mar 11, 2013 7:41 pm

Re: Slicing Arrays

Postby setrofim » Mon Mar 18, 2013 6:16 pm

IIRC, you can't index into non-consecutive, non-evenly-spaced ranges. So you'll probably have to do something like
Code: Select all
import numpy as np

data = np.random.rand(840, 2048)
data2 = np.hstack((data[:, 1:2], data[:, 51:1076]))
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: Slicing Arrays

Postby tnknepp » Tue Mar 19, 2013 12:37 pm

Thanks. I was trying to avoid stacking them, but I guess sometimes you have to use what you have. Thanks again,
Python: 2.7 via Anaconda
Numpy: 1.7
Pandas: 0.11
OS: Windows 7
IDE: Spyder/IPython
User avatar
tnknepp
 
Posts: 119
Joined: Mon Mar 11, 2013 7:41 pm


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 4 guests