I'm new to julia and I'm working on to rewrite julia code to python code.
And I saw the some codes using .==
expression. I couldn't understand what this means. So I searched it on web but couldn't find an answer.
Can someone tell me what is .==
in julia and its equivalent in python?
fyi, it was written like below.
x = sum(y .== 0) # y is array
Julia codes can easily be made by converting C or Python codes. It is very difficult to make Python codes by converting C codes or the other way around. Julia arrays are 1-indexed, i.e. arrays start with 1-n not 0-n. It might cause a problem with programmers having habit of using other languages.
In order to figure out the types to pass to an IAI function from the Python interface, you can refer to the equivalent function in the Julia API and translate the types to their Python equivalent. Most literal data types convert in a straighforward manner, for example:
Julia is a programming language used for scientific computation and mathematical programming. Julia is a combination of C and Python, which doesn’t mean that it literally copies any of the features from either of the languages. This combination holds the feature of high execution speed of C and flexible code writing of Python.
Machine Learning in Python vs. Julia - Is Julia Faster? Julia programming language was designed at MIT from the beginning for high performance in scientific computing, but domain experts still largely prefer slower languages for daily work, such as Python.
That's a Vectorized dot operation and is used to apply the operator to an array. You can do this for one dimensional lists in python via list comprehensions, but here it seems like you are just counting all zeroes, so
>>> y = [0,1,1,1,0]
>>> sum(not bool(v) for v in y)
2
Other packages like numpy
or pandas
will vectorize operators, so something like this will do
>>> import numpy as np
>>> y = np.array([0,1,1,1,0])
>>> (y == 0).sum()
2
>>>
>>> import pandas as pd
>>> df=pd.DataFrame([[0,1,2,3], [1,2,3,0], [2,3,4,0]])
>>> (df==0).sum()
0 1
1 0
2 0
3 2
dtype: int64
>>> (df==0).sum().sum()
3
The dot here is for vectorized operations: dot call
It basically applies your selected operation to each element of your vector (see dot operators).
So in your case, y .== 0
will check equality to 0 for each element of your vector y, meaning x will be the number of values from y being equal to 0.
As for how to do the equivalent in python, you can do it "by hand" through list comprehension, or with a library such as numpy. Examples:
x = sum([i == 0 for i in y])
or
import numpy as np
x = sum(np.array(y) == 0)
# or
x = (np.array(y) == 0).sum()
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