如何对"DenseMatrix"进行混洗在F#中 [英] How to shuffle a "DenseMatrix" in F#
问题描述
在Matlab中,我们可以编写以下代码对矩阵进行混洗:
In Matlab we can write the following code to shuffle a matrix:
data = data(:, randperm(size(data,2)));
我是用Math.NET写的:
there is what I write with Math.NET:
let csvfile = @"../UFLDL-tutorial-F#/housing.csv"
let housingAsLines =
File.ReadAllLines(csvfile)
|> Array.map (fun t -> t.Split(',')
|> Array.map (fun t -> float t))
let housingAsMatrix= DenseMatrix.OfRowArrays housingAsLines
let housingAsMatrixT = housingAsMatrix.Transpose()
let v1 = DenseVector.Create(housingAsMatrixT.ColumnCount,1.0)
housingAsMatrixT.InsertRow(0,v1)
// How to shuffle a "DenseMatrix" in F#
使用F#slice语法和从零开始的索引在Matlab中模拟矩阵运算.但是,它不起作用.
To simulate matrix operation in Matlab, using the F# slice syntax and zero-based indexing. However, it doesn't work.
housingAsMatrixT.[*,0]
我在vscode中收到了错误消息.
And I got the error message in vscode.
未定义字段,构造函数或成员'GetSlice'
The field, constructor or member 'GetSlice' is not defined
推荐答案
您实际上有两个问题,1)如何对矩阵进行切片以及2)如何对矩阵的列进行混洗.
You actually have two questions, 1) how to slice Matrices ala matlab and 2) how to shuffle the columns of a matrix.
对于1)实际上,您在评论中链接的确实是问题277 提供解决方案.但是,您可能使用的是旧版本,或者可能未正确引用F#
扩展名:
For 1) actually Issue 277 you linked in the comment does indeed provide the solution. However you might be using an old version or you might not be referencing the F#
extensions correctly:
#r @"..\packages\MathNet.Numerics.3.13.1\lib\net40\MathNet.Numerics.dll"
#r @"..\packages\MathNet.Numerics.FSharp.3.13.1\lib\net40\MathNet.Numerics.FSharp.dll"
open MathNet.Numerics
open MathNet.Numerics.LinearAlgebra
open MathNet.Numerics.Distributions
open System
//let m = DenseMatrix.randomStandard<float> 5 5
let m = DenseMatrix.random<float> 5 5 (ContinuousUniform(0., 1.))
let m' = m.[*,0]
m'
//val it : Vector<float> =
//seq [0.4710989485; 0.2220238937; 0.566367266; 0.2356496324; ...]
这将提取矩阵的第一列.
This extracts the first column of the matrix.
现在是2),假设您需要对矩阵或包含矩阵的数组进行混洗,则可以使用下面的一些方法. mathnet.numerics
中可能有一个更优雅的方法.
Now for 2), assuming you need to shuffle the matrix or the arrays containing a matrix you can use some of the approaches below. There might be a more elegant method within mathnet.numerics
.
要置换上面的向量:m'.SelectPermutation()
或SelectPermutationInplace
用于数组.还有其他便捷功能,例如.Column(idx)
,.EnumerateColumnsIndexed()
或EnumerateColumns()
等
To permute the vector above: m'.SelectPermutation()
or SelectPermutationInplace
for arrays. There are other convenience function like .Column(idx)
,.EnumerateColumnsIndexed()
or EnumerateColumns()
, etc.
所以m'.SelectPermutation()
将洗牌m'的元素.或改组列(您的matlab函数会这样做):
So m'.SelectPermutation()
will shuffle the elements of m'. Or to shuffle the columns (which your matlab function does):
let idx = Combinatorics.GeneratePermutation 5
idx
//val it : int [] = [|2; 0; 1; 4; 3|]
let m2 = idx |> Seq.map (fun i -> m.Column(i)) |> DenseMatrix.ofColumnSeq
m2.Column(1) = m.Column(0)
//val it : bool = true
由于原始矩阵的第一列移至新矩阵的第二列,因此两者应相等.
Since the first column of the original matrix moved to the second column of the new matrix, the two should be equal.
这篇关于如何对"DenseMatrix"进行混洗在F#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!