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