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]])
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With