官术网_书友最值得收藏!

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:

主站蜘蛛池模板: 当阳市| 乌兰浩特市| 灌阳县| 曲靖市| 昌江| 安福县| 定西市| 古交市| 灌南县| 阆中市| 成安县| 深水埗区| 吴江市| 溧水县| 文成县| 大洼县| 成安县| 齐齐哈尔市| 德令哈市| 明溪县| 大厂| 成武县| 克拉玛依市| 重庆市| 洛宁县| 邹城市| 眉山市| 吉水县| 大田县| 大英县| 吴江市| 梁河县| 铜陵市| 望江县| 治县。| 西和县| 家居| 惠安县| 江门市| 纳雍县| 精河县|