- 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:

- DB2 V9權(quán)威指南
- Facebook Application Development with Graph API Cookbook
- UML和模式應(yīng)用(原書第3版)
- Visual C++數(shù)字圖像模式識別技術(shù)詳解
- Servlet/JSP深入詳解
- C語言程序設(shè)計案例式教程
- CKA/CKAD應(yīng)試教程:從Docker到Kubernetes完全攻略
- MySQL數(shù)據(jù)庫管理與開發(fā)(慕課版)
- Python機器學習算法: 原理、實現(xiàn)與案例
- Python圖形化編程(微課版)
- 詳解MATLAB圖形繪制技術(shù)
- 現(xiàn)代C:概念剖析和編程實踐
- Apache Solr for Indexing Data
- Visual C#(學習筆記)
- Android熱門應(yīng)用開發(fā)詳解