As below, I understand lambda y:... .
But the first Lambda(...) is a function?.
ds = datasets.FashionMNIST(
...
target_transform=Lambda(lambda y: torch.zeros(10, dtype=torch.float).scatter_(0, torch.tensor(y), value=1))
)
It's just a function in torchvision for wrapping an arbitrary function as a transform. It's nothing to do with Python syntax, and is spelled Lambda with a capital L instead of lambda to not conflict with the Python keyword.
Related
I have the following dataset for which I want to calculate several aggregation metrics>
For some I'm using the standard functions, but for other I relay on the tsfresh library, from where I'm importing the functions:
sample.groupby('id').agg(['std', benford_correlation,absolute_maximum])
It works well for functions that have only one parameter, as is the case of:
from tsfresh.feature_extraction.feature_calculators import benford_correlation #(x)
from tsfresh.feature_extraction.feature_calculators import absolute_maximum #(x)
But for others like:
from tsfresh.feature_extraction.feature_calculators import autocorrelation#(x, lag)
I get and error since it has two parameters, x and lag by I'm only passing the x implicitly in the groupby.
How can I specify the other parameters required?
see the pandas.DataFrameGroupBy.aggregate docs. Additional keyword arguments are passed to the function. So you can do this:
sample.groupby('id').agg(
['std', benford_correlation,absolute_maximum],
additional_arg=value,
)
but if you need to pass different arguments to each function, you could use a lambda function:
sample.groupby('id').agg(
[
'std',
lambda s: benford_correlation(s, lag=1),
absolute_maximum,
],
)
I am trying to evaluate the following statement using mypy:
a = [1,2,3]
b = map(float, a)
this returns the error
Argument 1 to "map" has incompatible type "Type[float]"; expected "Callable[[str], str]"
but at runtime this has no problem executing. What is the reason for this problem?
I wasn't able to replicate this either (Python Ver. 3.9.0, mypy Ver. 0.910) so first thing to try would be upgrading to latest versions.
Not quite the same issue, but these issues on the mypy github may be an interesting reference:
https://github.com/python/mypy/issues/6697 (Open)
https://github.com/python/mypy/issues/1855 (Closed)
Based on these, it looks like the following might pass without a mypy error:
b = map(lambda x: float(x), a)
a = [1,2,3]
b = map(lambda x: float(x), a)
The issue was related to using the same name for the result variable and the variable that map is applied to, I was doing something along the lines of
a = b.split()
a = list(map(lambda x: float(x),a))
which resulted in the error. Changing the variable name in the last line here resolved this problem.
If I import sympy with:
from sympy import *
Then how do set lambda to be a symbol and not a function?
E.g.
lambda = symbols('lambda')
In my case, likely because sympy has all its functions imported (I am working in a sympy only environment so it is convenient), I receive the error:
lambda = symbols('lambda')
^
SyntaxError: invalid syntax
Is there any way to avoid this if I am importing all the functions from sympy?
Thank you
This is because lambda is a keyword for creating lambda functions. You can't use it as your variable name. You'll have to find a new name. Even if you find a way to assign lambda to something, it won't work because it's not parsed as a potential variable name.
For example:
lambda_ = symbols('lambda')
will not have the same error.
Is it possible to import module within a python lambda function? For example, i have a lambda function which requires to import math
import math
is_na_yes_no = lambda x: 'Yes' if math.isnan(x) else 'No'
How can I include the import math statement within the lambda function?
To clarify, I have a scenario that need to put some lambda functions in a config file and evaluate the function in some other python files, exmample:
{
"is_na_yes_no" = "lambda x: 'Yes' if math.isnan(x) else 'No'"
}
In this case, the python file that evaluating those lambda functions need to all modules required.
Thanks #L3viathan for the answer.
Here is same thing without having to import math in the module.
is_na_yes_no = lambda x: 'Yes' if __import__('math').isnan(x) else 'No'
It highlights the flexibility of python -- __import__ feature can be used in lambda functions instead of having them written out before hand.
I would like to define a lambda function in a different module than it will be executed in. In the module that the lambda will be called, there are methods available that aren't when the lambda is defined. As it is, Python throws an error when the lambda tries to employ those functions.
For example, I have two modules.
lambdaSource.py:
def getLambda():
return lambda x: squareMe(x)
runMe.py
import lambdaSource
def squareMe(x):
return x**2
if __name__ == '__main__':
theLambdaFunc = lambdaSource.getLambda()
result = theLambdaFunc(5)
If you run runMe.py, you get a Name Error: NameError: global name 'squareMe' is not defined
The only way I can get around this is to modify the lambda's global variables dictionary at runtime.
theLambdaFunc.func_globals['squareMe'] = squareMe
This example is contrived, but this is the behavior I desire. Can anyone explain why the first example doesn't work? Why 'squareMe' isn't available to the scope of the lambda function? Especially when, if I just defined the lambda below the function squareMe, everything works out okay?
You're defining getLambda and squareMe in separate modules. The lambdaSource module only sees what's defined in its scope -- that is, everything you define directly in it and everything you import in it.
To use squareMe from getLambda, you need lambdaSource.py to have a from runMe import squareMe statement (and not the other way around as you seem to be doing).