Book Image

F# for Machine Learning Essentials

By : Sudipta Mukherjee
Book Image

F# for Machine Learning Essentials

By: Sudipta Mukherjee

Overview of this book

The F# functional programming language enables developers to write simple code to solve complex problems. With F#, developers create consistent and predictable programs that are easier to test and reuse, simpler to parallelize, and are less prone to bugs. If you want to learn how to use F# to build machine learning systems, then this is the book you want. Starting with an introduction to the several categories on machine learning, you will quickly learn to implement time-tested, supervised learning algorithms. You will gradually move on to solving problems on predicting housing pricing using Regression Analysis. You will then learn to use Accord.NET to implement SVM techniques and clustering. You will also learn to build a recommender system for your e-commerce site from scratch. Finally, you will dive into advanced topics such as implementing neural network algorithms while performing sentiment analysis on your data.
Table of Contents (16 chapters)
F# for Machine Learning Essentials
About the Author
About the Reviewers


Machine learning (ML) is more prevalent now than ever before. Every day a lot of data is being generated. Machine learning algorithms perform heavy duty number crunching to improve our lives every day. The following image captures the major tasks that machine learning algorithms perform. These are the classes or types of problems that ML algorithms solve.

Our lives are more and more driven by the output of these ML algorithms than we care to admit. Let me walk you through the image once:

  • Computers everywhere: Now your smartphone can beat a vintage supercomputer, and computer are everywhere: in your phone, camera, car, microwave, and so on.

  • Clustering: Clustering is the task of identifying groups of items from a given list that seem to be similar to the others in the group. Clustering has many diverse uses. However, it is heavily used in market segment analysis to identify different categories of customers.

  • Classification: This is the ML algorithm that works hard to keep your spam e-mails away from your priority inbox. The same algorithm can be used to identify objects from images or videos and surprisingly, the same algorithm can be used to predict whether a patient has cancer or not. Generally, a lot of data is provided to the algorithm, from which it learns. That's why this set of algorithms is sometime referred to as supervised learning algorithms, and this constitutes the vast majority of machine learning algorithms.

  • Predictions: There are several ML algorithms that perform predictions for several situations that are important in life. For example, there are predictors that predict fuel price in the near future. This family of algorithms is known as regressions.

  • Anomaly detection: Anomaly, as the name suggests, relates to items that have attributes that are not similar to normal ones. Anomaly detection algorithms use statistical methods to find out the anomalous items from a given list automatically. This is an example of unsupervised learning. Anomaly detection has several diverse uses, such as finding faulty items in factories to finding intruders on a video stream coming from a surveillance camera, and so on.

  • Recommendations: Every time you visit Amazon and rate a product, the site recommends some items to you. Under the hood is a clever machine learning algorithm in action called collaborative filtering, which takes cues from other users purchasing similar items as you are. Recommender systems are a very active research topic now and several other algorithms are being considered.

  • Sentiment analysis: Whenever a product hits the market, the company that brought it into the market wants to know how the market is reacting towards it. Is it positive or negative? Sentiment analysis techniques help to identify these reactions. Also, in review websites, people post several comments, and the website might be interested in publishing a generalized positive or negative rating for the item under review. Here, sentiment analysis techniques can be quite helpful.

  • Information retrieval: Whenever you hit the search button on your favorite search engine, a plethora of information retrieval algorithms are used under the hood. These algorithms are also used in the content-based filtering that is used in recommender systems.

Now that you have a top-level idea of what ML algorithms can do for you, let's see why F# is the perfect fit for the implementations. Here are my reasons for using F# to implement machine learning algorithms:

What this book covers

Chapter 1, Introduction to Machine Learning, introduces machine learning concepts.

Chapter 2, Linear Regression, introduces and implements several linear regression models using F#.

Chapter 3, Classification Techniques, introduces classification as a formal problem and then solves some use cases using F#.

Chapter 4, Information Retrieval, provides implementations of several information retrieval distance metrics that can be useful in several situations.

Chapter 5, Collaborative Filtering, explains the workhorse algorithm for recommender systems, provides an implementation using F#, and then shows how to evaluate such a system.

Chapter 6, Sentiment Analysis, explains sentiment analysis and after positioning it as a formal problem statement, solves it using several state-of-the-art algorithms.

Chapter 7, Anomaly Detection, explains and poses the anomaly detection problem statement and then gives several algorithms and their implementation in F#.

What you need for this book

You will need Visual Studio 2010 or above and a good internet connection because some of the plotting APIs used here rely on connectivity.

Who this book is for

If you are a C# or F# developer who now wants to explore the area of machine learning, then this book is for you. No prior knowledge of machine learning is assumed.


In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "For example, able has a positive polarity of 0.125 and unable has a negative polarity of 0.75."

A block of code is set as follows:

let calculateSO (docs:string list list)(words:string list)=
    let mutable res  = 0.0
    for i in 0 .. docs.Length - 1 do
        for j in 0 .. docs.[i].Length - 1    do
            for pw in words do
                 res <- res + pmi docs docs.[i].[j] pw

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

Calling this function is simple as shown below.
//The above rating matrix is represented as (float list)list in F#
let ratings = [[4.;0.;5.;5.];[4.;2.;1.;0.];[3.;0.;2.;4.];[4.;4.;0.;0.];[2.;1.;3.;5.]]
//Finding the predicted rating for user 1 for item 2
let p12 = Predictu  ratings  0 1

Any command-line input or output is written as follows:

if d1 = 0.0 || d2 = 0.0 then 0.0 else num  / ((sqrt d1) * (sqrt d2 ))

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "Navigate to user id and then on item id."


Warnings or important notes appear in a box like this.


Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from You can also visit for more updates on the F#.

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from


Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to and enter the name of the book in the search field. The required information will appear under the Errata section.


Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.


If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.