A quick look at Julia, R and Python - knn Example | Towards Data Science

Once again, I found myself in the middle of considering learning a new language or simply fostering my knowledge of one I already know.
Every year seems to be the year of a “new” language and it appears that the talk about the “data science languages” has come back stronger than ever. I will, therefore, provide a quick look at all three languages, Julia, R and Python, while guiding you through a basic KNN algorithm.
We will address languages in the following order (1) Julia, (2) R and (3) Python. Code can be found embedded below.
First things first, the background check. To begin with Julia, this general-purpose, dynamic language was introduced in 2012 and caters everyone willing to learn a new language that is perfectly suitable for performance-intensive tasks. Julia, like both other languages, follows multiple programming paradigms so no matter what programming knowledge you have, you are likely being able to catch up quite quickly.
I personally appreciate the sound implementation of optional typing which may be very handy depending on the purpose of your software. Syntax sometimes seems a bit like a mixture somewhere in between R and Python, but in general, you will not have a hard time to find ways helping yourself if you got stuck.
Although Julia is meant to be general-purpose and high performance, it seems that primarily the data science community is really driving Julia’s success. There is an already long list of notable users: Nasa, IBM, BlackRock, Pfizer or the NY Fed — just to name a few[1].
R was first released in 1993 and it is more than ever one of the main contenders in statistical computing. Other than Julia, R was invented to serve as a programming language for scientific purposes. The language succeeded (imho) over various large competitors like IBM’s SPSS (which has a spreadsheet-like user interface and is, therefore, easier to use for many) and SAS which is a commercial analytics software provider and language (using the same name). R is often appreciated for a straightforward way of achieving tasks as well as brilliant and at the same time easy ways to illustrate data.
About the same time R has emerged, Guido van Rossum invented Python, a general purpose, interpreted and object-oriented programming language. Python has quite stagnated over time, got fierce competition from e.g. Ruby (especially with regard to web development — Ruby on Rails), but eventually became the first choice for many. Pythons success in data science skyrocketed in the 2000s after essential libraries had emerged. Depending on where you compare data, Python is today among the top 2 or 3 languages used (depending on the sources). Python is convenient as it can be used for simple tasks like automatization, but also for large programs.
For data science purposes Python requires to gear up through Pandas (2008) and Numpy/Scipy (2005, 2001), as the standalone language will not provide you with the necessary tooling.
One of the issues you might often read about when browsing the web is, that some languages simply come with [confusingly] large varieties of packages, which can be a great thing, but may also add complexity and lets the language drift away from a ‘common standard’ of used packages. If this sounds weird to you, just try to find a good Grubbs test, which is useful for outlier removal, in Python.
As Julia is the youngest among our candidates (even if we consider Python’s rise from the late 2000s onward), packages are less overwhelming in its magnitude (however steadily growing), R is well established and comes with many packages that are used as a common standard. Python does it all, there are some “major” libraries, but the options to choose from seem endless.
Enough for now, let’s jump into our data — the IRIS flowers data set:
In order to make things comparable later on, here is a test data point and its reference class. These two shall provide us with the opportunity to compare an “similar to an existing” data point to our predicted data point.
## Our test data point derived from the reference class## We can assume that the result must be class = 1[7,3,5,1] ~> 1.0
The reference class, which is a data point in IRIS dataset:
## Reference classsepal_length    7.0 -> 7 (0)sepal_width     3.2 -> 3 (-.2)petal_length    4.7 -> 5 (+.3)petal_width     1.4 -> 1 (-.4)class           1.0 ≈> 1.0 Name: 50, dtype: float64
Julia does the start. We will use CSV, DataFrames and Statistics in order to calculate our knn results.
Clearly, renaming columns seems a lot different than we know from R and Python, the dictionary, in contrast, is great to use. One little hint, use double quotes not single ones because Julia won’t handle single quotes (sad!).
Further, note that the rename! function is followed by an exclamation mark — for a good reason: This function modifies its arguments! So keep this in mind when writing your own functions.
Personally, I prefer using dictionaries here, however as we used a dict for the mapping already, I demonstrated the collection and calculation of nearest neighbors through the data type DataFrame. While the import was quite straightforward, the loop construction reminded me more of ancient Pascal (RIP), further consider not to use extensive line breaks inside the loops when using IJulia kernel, I experienced errors as the end part was sometimes not found. Anyways, I recommend using Atom for development.
The results — just to compare with the others:
Julia:[53, 77, 51, 59, 87]Prediction: 1.0
Next it’s time for R to shine. At this point is worth to note, as far as I could see from my professional background, R is still the way to go — and this will likely remain as is. Why? R is well established, extensive in its magnitude (Machine Learning, linear algebra, ggplot, etc.), can be used for simple scripts and is already in place in many companies like Facebook, Google, Twitter, Microsoft, Uber just to name the best known — impressive, right?[2]
Fun fact, IBM joined the R Consortium Group (remember I mentioned IBM as a competitor with SPSS earlier?)!
To my experience, R is taught at many universities, too (I had the pleasure twice). What can I say, R is great!
R Syntax is easy to read and also quite fun to learn. I appreciate its similarities to other languages and accurate error messages when it comes to debugging. As R is supported from its early years on, for development you will probably like to use Atom or (beloved) RStudio. What is note mentioning, I often experience that R is far less forgiving when it comes to inaccurate code, forgetting a comma might cause an hour of head scratching. The learning curve is quite steep.
The results — just to compare with the others. Note, like Julia but unlike Python, the index starts at 1.
R:   X.index.....i X.dist.....distance53            53           0.529150377            77           0.529150351            51           0.538516559            59           0.648074187            87           0.6633250Prediction: 1
At last, let’s have a look at Python 🐍.
Why I love Python is its list comprehension, powerful iteration methods and neat data structures. Although it is great fun to use a variety of tools, it might make it quite a bit harder to start and understand. IDEs for Python are numerous, I would go for Pycharm (which is free for standard purposes != Notebooks and Web development) or Atom (for the third time).
Our knn algorithm is quite compact (maybe it is due to the fact, that I have learned Python the longest .. ), still, sorting using lambda and list comprehension may not be everyone’s best friend. I have not used a dataFrames in this case, as I did not want to import 🐼 here.
The results — just to compare with the others:
Python:[[52, 0.5291502622129179], [76, 0.5291502622129183], [50, 0.5385164807134504], [58, 0.6480740698407865], [86, 0.6633249580710798], [77, 0.7615773105863908]]Prediction: 1
Julia — Honestly I did really enjoy using Julia, however it sometimes feels .. complicated, more than necessary. In the below example I arbitrarily used a dictionary (which is not available in R and more convenient in Python) as well as a DataFrame to manipulate data. In contrast, working with most data structures is quite fun and seems indeed very fast. Julia might be a modern “one language fits all” language. This could indeed be the beginning of a love story!
R is just rock solid. It is providing a huge number of packages, is very well established and makes fun to code with. Due to its maturity, there is a lot of documentation and many examples out there. Going with R simply can’t be a mistake if you’re into data.
Python is great, it is not the best performing still, it is fast, easy to read and learn and comes with a variety of libraries. The main advantage, in my opinion, is, that Python can be used for so many different purposes. Python often is so intuitive, you will wonder how easy things can be.
Comparing Python and (the other general-purpose language) Julia, it is not unlikely to see a shift towards Julia one day, especially for more than just scientific purposes — but time will tell.
As always, see you next time! Stay safe, stay at home.
[0] Image by Ferdinand Stöhr (Thanks !) — https://unsplash.com/@fellowferdi?utm_source=medium&utm_medium=referral
[1] Julia Case Studies — https://juliacomputing.com/case-studies/
[2] R Users — https://www.datasciencecentral.com/profiles/blogs/list-of-companies-using-r
#Julia #R #Rlang #Python #Numpy #Pandas #DataScience #WebDevelopment #IDE #Google #IBM #Twitter