- F# for Machine Learning Essentials
- Sudipta Mukherjee
- 559字
- 2021-07-16 13:07:01
The basics of matrices and vectors (a short and sweet refresher)
Using Math.Net numerics, you can create matrices and vectors easily. The following section shows how. However, before you can create the vector or the matrix using Math.NET API, you have to reference the library properly. The examples in this chapter run using the F# script.
You have to write the following code at the beginning of the file and then run these in the F# interactive:

Creating a vector
You can create a vector as follows:

The vector values must always be float
as per Math.NET. Once you run this in the F# interactive, it will create the following output:

Creating a matrix
A matrix can be created in several ways using the Math.NET package. In the examples in this chapter, you will see the following ways most often:
- Creating a matrix by hand: A matrix can be created manually using the Math.Net F# package, as follows:
Once you run this, you will get the following in the F# interactive:
- Creating a matrix from a list of rows: A matrix can also be created from a given list of rows. Normally, matrices that are generally expected to be filled with mostly non-zero values are known as
DenseMatrix
.Here is how to create a dense matrix from an array or rows.
The following is the mileage data of several cars. This data is available at https://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data.
The following are the column descriptions:
You can get these details at https://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.names.
As you can see, apart from the name column, everything is real valued and thus can be loaded in a
DenseMatrix
. So if we get rid of the last column and remove the rows with missing values, then we can load the entire matrix using the command that follows.
I have modified the file by deleting all the rows with missing values (missing values are denoted by ?) and then delimiting the file with commas instead of multiple white spaces. You can download the file with the book's source code.
//Loading values of the csv file and generating a dense matrix //Please modify the file path to point it in your local disc let rows = File.ReadAllLines("C:\\mpg.csv") |> Array.map ( fun t -> t.Split(',') |> Array.map(fun t -> float t)) let mpgData = DenseMatrix.ofRowArrays rows
Finding the transpose of a matrix
Finding the transpose of a matrix is very important because we have to rely on this function a lot while implementing several linear regression algorithms. A transpose is nothing but rotating of the matrix. So in a transposed matrix, the rows of the original matrix become the columns, and the columns of the original matrix become the rows.

Transposing will produce this:

Using Math.NET, you can declare the first matrix and then calculate the transpose of the matrix as follows:

Finding the inverse of a matrix
Only square matrices can be inversed. And if the determinant of the matrix is zero then the elements of the inversed matrix can be infinity or negative infinity or NaN (Not a Number).
Here is an example of finding the inverse using Math.NET:

When run in the F# interactive, this snippet will produce the following result:

Trace of a matrix
The trace of a matrix is the sum of the diagonal elements. The trace can be calculated with the function Trace()
as follows:

- Learning LibGDX Game Development(Second Edition)
- Node.js+Webpack開發實戰
- Android Studio Essentials
- 深入淺出Android Jetpack
- Java 11 Cookbook
- Learning Python by Building Games
- R大數據分析實用指南
- Python算法從菜鳥到達人
- Angular開發入門與實戰
- 軟件項目管理實用教程
- Geospatial Development By Example with Python
- CodeIgniter Web Application Blueprints
- Secret Recipes of the Python Ninja
- Visual Basic 程序設計實踐教程
- 游戲設計的底層邏輯