A Comprehensive Guide to Learn Swift from Scratch for Data Science

Overview Swift is quickly becoming one of the most powerful and effective languages for data science Swift is quite similar to Python so you’ll find the transition quite smooth We’ll cover the basics of Swift and also learn how to build your first data science model using this language   Introduction Python is widely considered the best and most effective language for data science.

Most of the polls and surveys that I’ve come across in recent years peg Python as the market leader in this space.

But here’s the thing – data science is a vast and ever-evolving field.

The languages we use to build our data science models have to evolve with it.

Remember when R was the go-to language?.That was swiftly overtaken by Python.

Julia also came up last year for data science and now there’s another language that is blossoming.

Yes, I’m talking about Swift for data science.

“I always hope that when I start looking at a new language, there will be some mind-opening new ideas to find, and Swift definitely doesn’t disappoint.

Swift tries to be expressive, flexible, concise, safe, easy to use, and fast.

Most languages compromise significantly in at least one of these areas.

” – Jeremy Howard When Jeremy Howard endorses a language and starts using it for his daily data science work, you need to drop everything and listen.

In this article, we will learn about Swift as a programming language and how it fits into the data science space.

If you’re a Python user, you’ll notice the subtle differences and the incredible similarities between the two.

There’s a lot of code here as well so let’s get started!.  Table of Contents Why Swift?.Swift Basics for Data Analysis Using Popular Python Libraries in Swift Building a Basic Model in Swift using TensorFlow Future of Swift for Data Science   Why Swift?.“PyTorch was created to overcome the gaps in Tensorflow.

FastAI was built to fill gaps in tooling for PyTorch.

But now we’re hitting the limits of Python, and Swift has the potential to bridge this gap”                                                                                                                 – Jeremy Howard There has been a lot of excitement and attention recently towards Swift as a language for data science.

Everyone is talking about it.

Here are a few reasons why you should learn Swift: Swift is fast, like legit fast.

It’s as close to C as possible At the same time, it has a very simple and readable syntax which is very similar to Python: Swift is a more efficient, stable and secure programming language as compared to Python It is also a good language to build for mobile.

In fact, it’s the official language for developing iOS applications for the iPhone It has strong integrated support for automatic differentiation which makes it one of the few elite languages for numerical computing It has the support of likes of Google, Apple, and FastAI behind it!.Here is Jeremy Howard articulating how good Swift is:   Swift Basics for Data Analysis Before we start with the nitty-gritty details of performing data science using Swift, let’s get a brief introduction to the basics of the Swift programming language.

  The Swift Ecosystem The current state of Swift for Data Science is primarily made up of two ecosystems: The Open-source ecosystem The Apple ecosystem The open-source ecosystem is one where we can download and run Swift on any operating system or machine.

We can build machine learning applications using really cool Swift libraries, like Swift for TensorFlow, SwiftAI and SwiftPlot.

Swift also lets us seamlessly import mature data science libraries from Python like NumPy, pandas, matplotlib and scikit-learn.

So if you had any hesitation about switching over to Swift from Python, you’re well covered!.The Apple ecosystem, on the other hand, is impressive in its own right.

There are useful libraries like CoreML that let us train large models in Python and directly import them in Swift for inferencing.

Additionally, it also comes with a plethora of pre-trained state of the art models that we can directly use to build iOS/macOS applications.

There are other interesting libraries like Swift-CoreML-Transformers that let us run state-of-the-art text generation models like the GPT-2, BERT, etc.

on the iPhone.

And there are multiple other libraries that give a good level of functionality when you need to build machine learning-based applications for Apple devices.

There are multiple differences between the two ecosystems.

But the most important one is that in order to use the Apple ecosystem, you need to have an Apple machine to work on and you can only build for Apple devices like the iOS, macOS etc.

Now that you have an overview of Swift as a language for data science, let’s get into the code!.  Setting up the Environment for Swift Swift is available to use on Google Colab with both GPU and TPU versions.

We will be using that so that you can quickly get up to speed with it without spending much time on the installation process.

You can follow the below steps to open a Colab notebook that is Swift-enabled: Open a blank Swift notebook Click on “File” and then select “Save a copy in Drive” – this will save a fresh Swift notebook in your own Google Drive!.We are all set to start working with Swift!.You can write your first line of code: print(“hello world from Swift”) Sweet!.If you want to work with Swift locally on your own system then here a few links that you can follow: If you want to install Swift on your local system then you can follow the installation instructions For installing with Jupyter Notebook on Ubuntu, refer to Jeremy Howard’s instructions to install Swift On Ubuntu, you can also install Swift for Docker Now, let’s quickly cover some basic Swift functions before jumping into the data science aspect of using it.

  The print function I’m sure you’ve already used this before.

It works the very same way as it does in Python.

Simply call print() with whatever you want to print inside the parenthesis: print(“Swift is easy to learn!”)   Variables in Swift Swift provides two useful options to create variables: let and var.

 let is used to create a “constant” which is a variable whose value cannot change anywhere further in the program.

var is very similar to the variables that we see in Python – you can change the value stored in it anytime further in the program.

Let’s look at an example to see the difference.

Create two variables a and b: let a = “Analytics” var b = “Vidhya” Now, try changing the value of both a and b: b = “AV” a = “AV” You will notice that b was able to update its value without any issue while a gives an error: This ability to create constant variables is very useful and helps us prevent unseen bugs in our code.

You will see further in the article that we will use let to create variables that store important information which we do not want to over-write even by mistake in our code.

Here’s a pro-tip: use var for temporary variables or variables you want to use for some intermediate calculations.

Similarly, use let for things like storing the training data, results, etc.

– basically the values that you do not want to change or mess up.

Also, there is this cool feature of Swift where you can even use emojis as variable names!.This is because Swift supports Unicode very well so we can create variables with Greek letters: var π = 3.

1415925   Swift’s Datatypes Swift supports all the common data types, like Integer, String, Float and Double.

We can assign any variable with a value, and its type will automatically be detected by Swift: let marks = 63 let percentage= 70.

0 var name = “Sushil” You can also explicitly mention data type while creating a variable.

This helps prevent errors in the program because Swift will throw an error if the types do not match: let weight: Double = 72.

8 Let’s have a quick quiz.

Create a constant with an explicit type of `Float` and a value of 4 and post the solution in the comments below!. More details

Leave a Reply