mobelahcen
mobelahcen

Reputation: 424

Ignore unused argument when calling a function

I'm trying to ignore both the argument and the output linked to it by using '_':

data_scaled, _, scaler = scaling(sequence_arr,*_)

The function is:

def scaling(x, y):
    x = x.reshape(x.shape[0], x.shape[1])
    y = y.reshape(y.shape[0], y.shape[1])
    scale = MinMaxScaler(feature_range=(-1, 1))
    scale = scale.fit(x)
    x_scaled = scale.transform(x)
    y_scaled = scale.transform(y)
    return x_scaled, y_scaled, scale

I get the error:

NameError: name '_' is not defined

Upvotes: 0

Views: 225

Answers (3)

rrobby86
rrobby86

Reputation: 1464

The error is given by the use of *_ in the call because the _ variable isn't defined. The _ in the assignment is OK because the variable gets created.

In general you can't "ignore" an argument which is expected by the function, unless it has a default value. You should at least pass something like None:

#                                x =           y =
data_scaled, _, scaler = scaling(sequence_arr, None)

However in your case this will raise an error as reshape is called on the None object.

Upvotes: 0

Bram Vanroy
Bram Vanroy

Reputation: 28437

You have to make y optional and go from there.

def scaling(x, y=None):
    x = x.reshape(x.shape[0], x.shape[1])
    scale = MinMaxScaler(feature_range=(-1, 1))
    scale = scale.fit(x)
    x_scaled = scale.transform(x)
    if y is not None:
        y = y.reshape(y.shape[0], y.shape[1])
        y_scaled = scale.transform(y)
    else:
        y_scaled = None
    return x_scaled, y_scaled, scale

You can then call it like

data_scaled, _, scaler = scaling(sequence_arr)

Upvotes: 3

rdas
rdas

Reputation: 21275

You're trying to unpack _ here:

data_scaled, _, scaler = scaling(sequence_arr,*_)

Unpacking only works on existing.

You can only use _ for assignment - for ignoring the value idiomatically.

For ignoring extra arguments to scaling, you need to change the function itself.

def scaling(x, y, *args, **kwargs):

The idiomatic pythion function definition, takes any extra positional arguments into args & keyword arguments into kwargs

Upvotes: 0

Related Questions