Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Index for the end of a numpy array

I have a function that loads some data into a 2D numpy array. I want to let the function call specify a number of rows and columns that can be removed from the beginning and the end. If no parameter is specified, it returns all the data.

import numpy as np
function load_data(min_row, max_row, min_col, max_col):
    a = np.loadtxt('/path/to/mydata.txt')[min_row:max_row,min_col:max_col]

Now, min_row and min_col could default to 0. How can I set the defaults for max_col and max_row to refer to the end of the array?

My only solution is:

function load_data(min_row=0, max_row=None, min_col=0, max_col=None):
    a = np.loadtxt('/path/to/mydata.txt')
    if not max_row: max_row = a.shape[0]
    if not max_col: max_col = a.shape[1]
    a = a[min_row:max_row,min_col:max_col]

Is there a better solution, something like:

function load_data(min_row=0, max_row="end", min_col=0, max_col="end"):
    a = np.loadtxt('/path/to/mydata.txt')[min_row:max_row,min_col:max_col]

For the record, example data could be:

np.array([[1, 2, 3, 4, 5],
          [1, 2, 3, 4, 5],
          [1, 2, 3, 4, 5],
          [1, 2, 3, 4, 5],
          [1, 2, 3, 4, 5]])
like image 696
gozzilli Avatar asked Mar 19 '23 01:03

gozzilli


1 Answers

You can just use None directly in the slice, for example,

x = np.arange(10)
x[2:None]    # array([5, 6, 7, 8, 9])

or you could write your function like:

function load_data(min_row=0, max_row=None, min_col=0, max_col=None):
    a = np.loadtxt('/path/to/mydata.txt')
    a = a[min_row:max_row,min_col:max_col]

Here, you could also replace you min defaults with None too. This works because None is used as the defaults in the slice object. For more explicit docs on using None in numpy slicing, see the Note box at the end of the Basic Slicing docs description.

like image 99
tom10 Avatar answered Mar 26 '23 02:03

tom10