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

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:

主站蜘蛛池模板: 赣州市| 大庆市| 肇州县| 沧源| 于田县| 咸丰县| 方山县| 萍乡市| 武威市| 芦溪县| 五指山市| 永登县| 霍邱县| 蒲江县| 班玛县| 民和| 丹东市| 建平县| 德清县| 九江市| 孙吴县| 鄂托克前旗| 平南县| 深水埗区| 贺兰县| 武山县| 岐山县| 榆树市| 山丹县| 攀枝花市| 大渡口区| 涡阳县| 崇信县| 福清市| 乌鲁木齐市| 丘北县| 腾冲县| 大余县| 宕昌县| 宁阳县| 洛扎县|