Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is ".==" in julia and its equivalent in python?

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
like image 588
CalmPenguin Avatar asked Dec 23 '20 03:12

CalmPenguin


People also ask

How can I make Julia code in Python?

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.

How do I convert Julia data types to their Python equivalent?

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:

What is the Julia programming language?

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.

Is machine learning in Python faster than Julia?

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.


2 Answers

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
like image 152
tdelaney Avatar answered Oct 27 '22 00:10

tdelaney


What It does:

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.

Python equivalent:

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()
like image 44
smagnan Avatar answered Oct 26 '22 22:10

smagnan