Home Analytics An Introduction to Julia for Data Analysis

An Introduction to Julia for Data Analysis

by datatabloid_difmmk

This article data science blogthon.

What language do you use when it comes to data analysis? python, is not? But there is another rapidly growing language for data analysis.Some of you may guess the language – I’m speaking Julia. I always like to learn something new. So, of course, I will do some data analysis. After learning this language, I’m pretty impressed. I hope that after reading this article you will also like this language.

Several articles about the Julia programming language are available that cover the basics. However, this article only covers points that are commonly used when analyzing Julia.

So let’s get started.

About “Julia”

Hear from an instructor as you learn Python. In Python everything is an objectBut in Julia everything is a generic function – Classes don’t exist in Julia! Instead of classes, they made features more powerful and provided new functionality. If you’re familiar with data analysis, you’ll notice that I rarely use classes. But there are a huge number of uses for functions. Julia is specifically written for scientific computing, data science, and machine learning, so no classes are included.Julia is

  • Easy-to-use syntax like python

  • Statistics functions such as R.When

  • fast performance like C++

So you can expect similar syntax for these three languages ​​in Julia.

Now let’s discuss the built-in data structures and functions used for data analysis.

Julia data structures

arrangement

After hearing the name “array”, you may be disappointed to think that you are thinking of C or C++ arrays and again dealing with C arrays where you have to implement every method. don’t think about it They named it an Array, which is just a python list with a few tweaks. You declare arrays in Julia the same way you declare lists in Python.

In the code above you can also see it used here to print something to the console. of println() function.

Now comes the interesting part. If you view the list type in Python, you will see the following output:

output

But for Julia it looks like this:

println(typeof(my_list))

output:

output

This data structure is called an Array, but if you want to check the array type, you can see that Julia recognized it as a vector of strings. A vector is a dynamic array in C++. Remember? Julia already has C++ functionality.

How to insert and remove elements from an array in Julia? Just like we did with the stack data structure in C++ – using push() When pop(). C++ calls these two as objects, but Julia uses them as functions with a slight modification. See code below.

Now ask why there is a bang operator after the function name. Julia has two kinds of functions. mutation When non-mutant. In a mutating function, the function takes arguments and modifies them. However, with non-mutating functions, the function takes arguments and returns a new result computed by those arguments. A mutating function is indicated by adding a bang operator after the function name, and a non-mutating function has no bang operator after the function name.

here push! () When pop! () It’s a mutating function because you’re mutating an existing array instead of returning a new array. Running the above code gives the following output.

Julia

Arrays can also be accessed by specifying the array index in square brackets at the end of the array name. Just remember one thing. In Python, array indices start at 0. However, in Julia, array indices start at 1. For example, if you want to know the first element of the my_list array, print the value: my list[1]. it will give the output “Julia”.

Similar to Python, you can also insert a value at a specific list index by assigning the new value to the specific list index. Here is an example.

my_list[1] = "Java"

dictionary

In Python, dictionaries are created using curly braces or the dict() function. But Julia uses Dict(). Below is an example dictionary.

my_dict = Dict(1 => "one", 2 => "two", 3 => "three", 4 => "four")
println(my_dict)

From the code above, ‘=>’ A signature between key-value pairs. In Python, use “:” between key-value pairs. When I print the dictionary, I get the following output:

Julia

Hmm, in that case the dictionary is unordered.If you want an ordered dictionary you should use OrderedDict from ordered collection package.look this Link.

Values ​​in a dictionary can be retrieved by indexing by the corresponding key.For example, if you want to print my_dict[1]in which case you will get the output “1”.

println(my_dict[1])  # prints "one"

To add a new entry you need to index the dictionary by the desired key and assign the value = Just like Python.

my_dict[5] = "five"
println(my_dict)
# prints Dict(5 => "five", 4 => "four", 2 => "two", 3 => "three", 1 => "one")

can be used to remove keys from the dictionary. erase! () function.

delete!(my_dict, 5)
# it removes the key 5 and its corresponding value

If you want to get the deleted value, pop! () function.

popped_value = pop!(my_dict, 5)

two lists zip () Did you create a function and use it to create a dictionary in Python? You can do it here too. See example below.

strings

Strings in Python are represented by single or double quotes. However, Julia requires strings to be double-quoted and characters to be single-quoted. This functionality is taken from C++. you can easily notice that. Here is an example string:

my_str = "Julia is an awesome language. I really like it."

You can also write multi-line strings. See example below.

my_string = "
Bravo 6, 
Going Dark.
"

Julia allows you to write multi-line strings using a single double quote, which Python does not. You can also use three double quotes if you want to keep the same rules for writing multi-line strings in Python.

my_str = """
All stations,
This is Bravo 6. 
Get down.
"""

If you’re familiar with the Call of Duty game franchise, you’ll easily recognize whose words this is. Using he can also concatenate two strings. ‘*’ operator.

str1 = "hello"
str2 = "goodbye"
println(str1 * str2)
# prints hellogoodbye

If you want a space between two strings when concatenating them.use join () function.

join(["hello", "goodbye"], "")

In Julia, I’ve always wanted one more thing in Python. string interpolation. See example below.

Name = "Subhradeep Rang"
println("My name is $Name")
# prints “My name is Subhradeep Rang”

From the code above, we can see that we can pass variables within strings by adding a ‘$’ sign in front of the variable name. This feature is also available in Kotlin.

Julia broadcaster

In Python you can do almost anything with lists. But when it comes to array manipulation, you can’t do this with Python lists. Here is a NumPy array. But that’s not the case with Julia. They introduced broadcasters. See example below.

list1 = [1, 2, 3, 4]
println(list1 .+ 2)
# it prints [3, 4, 5, 6]

The above example adds 2 to all array elements. To do this in Python, you’ll need to use numpy arrays. However, we are using broadcasters here. For this you need to add a ‘.’. before the operator. This also applies to the functions described below.

function

Now comes the most interesting part. Julia defines functions differently. No parentheses or indentation is used here. See example below.

function square(x::Int64)
     return x*x
end

See, it’s that simple. Now finish the function like this: end keyword.Here you can also specify the argument type as double colon (::)You can also broadcast functions here.If you want to find the square of all the elements of List 1 The previously used function should be written as:

println(square.(list1))
# it will print [1, 4, 9, 16]

Interesting. I like Julia’s great features. You can also write compact functions in Julia. If you want to write the square function as a compact function, write it like this:

square(x::Int64) = x*x

If you don’t need to write any more lines in your function, you can use this. Now you can ask me – you’re creating a function that never mutates. What about the mutating function? Create a function of that type, of course. See the example below for the mutation function.

I think you have already guessed the main functionality of this function.You can define a mutating function like before, but this time Bang (!) Between the function name and the left parenthesis. Thanks to broadcasters, we don’t have to write a for loop. Here we add 1 to each element of the array. Since this is a mutating function, it doesn’t need to return a value. Just pass the array and BOOM! Array changed.

At this point, a question may arise among newbies: which language should be learned first, Python or Julia? Beginners are encouraged to learn Python first. Python is not only easy to understand, it is also popular in the fields of data science and machine learning. If you already know Python well enough, you can try Julia. Adds a new feather to your skill set. After doing a bit of research on the language, if I’m not mistaken, Julia won’t be hiding for long.

It’s impossible to include all of Julia’s features in one article.But if you want to learn all things follow this LinkIn this article, we covered:

  • What is a broadcaster and how do I use it?
  • Different types of functions: mutating and non-mutating functions, and their implementations in Julia.
  • How to apply a single function to a list of values ​​by broadcasting.

Other packages such as Python are also available here, but are not included in this article. These packages are described later. Until then, stay tuned. Also, don’t forget to check out other articles on AnalyticsVidhya.

Media shown in this article are not owned by Analytics Vidhya and are used at the author’s discretion.

You may also like

Leave a Comment

Subscribe my Newsletter for new blog posts, tips & new photos. Let's stay updated!

About Us

We're a provider of Data IT News and we focus to provide best Data IT News and Tutorials for all its users, we are free and provide tutorials for free. We promise to tell you what's new in the parts of modern life Data professional and we will share lessons to improve knowledge in data science and data analysis field.

Facebook Twitter Youtube Linkedin Instagram

5 Strategies To Reduce IT Support Tickets – Ultimate Guide

Recent Articles

Redefining the Role of IT in a Modern BI World What (Really) Are Issues Faced by Data Scientist in 2022 How I start Data Science Projects | What to do when you're stuck

Featured

5 Strategies To Reduce IT Support Tickets – Ultimate Guide Redefining the Role of IT in a Modern BI World What (Really) Are Issues Faced by Data Scientist in 2022

Copyright ©️ All rights reserved. | Data Tabloid