Skip to content

Commit

Permalink
fix Matrix.map references
Browse files Browse the repository at this point in the history
  • Loading branch information
bvenn committed Jun 8, 2021
1 parent 7d89443 commit 3d4f3b8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/FSharp.Stats/ML/Unsupervised/GapStatistics.fs
Expand Up @@ -299,7 +299,7 @@ https://www.datanovia.com/en/lessons/determining-the-optimal-number-of-clusters-
// Calculate log(sum_i(within-cluster_i sum of squares around cluster_i mean)) of kmeans clustering result.
let logDispersionKMeansInitCvMax =
let aggregator = IterativeClustering.avgCentroid
let factory = IterativeClustering.intitCVMAX
let factory = IterativeClustering.initCVMAX
let clusterDispersion (cluster:float[][]) =
let distances =
cluster
Expand Down
19 changes: 8 additions & 11 deletions src/FSharp.Stats/ML/Unsupervised/IterativeClustering.fs
Expand Up @@ -51,17 +51,14 @@ module IterativeClustering =



// cvmax - Algorith by Moth’d Belal. Al-Daoud (Ref.: A New Algorithm for Cluster Initialization)
let intitCVMAX (sample: float[] array) k =
let dmatrix = matrix sample
// cvmax - Algorithm by Moth’d Belal. Al-Daoud (Ref.: A New Algorithm for Cluster Initialization)
let initCVMAX (sampleRows: float[] []) k =
let dmatrix = matrix sampleRows
let cvmax =
dmatrix
|> Matrix.Generic.enumerateColumnWise Seq.var
|> Seq.zip (Matrix.Generic.enumerateColumnWise id dmatrix)
|> Seq.maxBy snd
|> fst
|> Seq.mapi (fun rowI value -> (rowI,value))
|> Seq.toArray
sampleRows
|> JaggedArray.transpose
|> Array.maxBy Seq.var
|> Array.indexed
|> Array.sortBy snd

if cvmax.Length < k then failwithf "Number of data points must be at least %i" k
Expand All @@ -78,7 +75,7 @@ module IterativeClustering =
| x -> chunkSize * (i - 1) + ((cvmax.Length - chunkSize * (i - 1)) / 2)
//printfn "Array.lenght = %i and index = %i" cvmax.Length (index-1)
yield cvmax.[index-1] |> fst]
|> Seq.map (fun rowI -> dmatrix.Row(rowI).ToArray())
|> Seq.map (fun rowI -> sampleRows.[rowI])
|> Seq.toArray

// // cvmax - Algorith by Moth’d Belal. Al-Daoud (Ref.: A New Algorithm for Cluster Initialization)
Expand Down
10 changes: 5 additions & 5 deletions src/FSharp.Stats/ML/Unsupervised/PrincipalComponentAnalysis.fs
Expand Up @@ -66,7 +66,7 @@ module PCA =
data |> Matrix.meanColumnWise
// Atttention: not entierly shure if space of data before
let colStDev =
data |> Matrix.Generic.enumerateColumnWise Seq.stDevPopulation |> Seq.toArray
data |> Matrix.Generic.mapCols Seq.stDevPopulation |> Seq.toArray

let adjust (direction:AdjustmentDirection) (aData:Matrix<float>) =
match direction with
Expand All @@ -88,11 +88,11 @@ module PCA =
/// Returns an AdjustmentFactory which centers and standardize the data
let toAdjustCorrelation (data:Matrix<float>) : AdjustmentFactory =
let colMeans =
data |> Matrix.meanColumnWise
let sqrtI = sqrt (float data.NumRows)
// Atttention: not entierly shure if space of data before
data |> Matrix.meanColumnWise
let sqrtI = sqrt (float data.NumRows)
// Attention: not entirely sure if space of data before
let colStDev =
data |> Matrix.Generic.enumerateColumnWise Seq.stDevPopulation |> Seq.toArray
data |> Matrix.Generic.mapCols Seq.stDevPopulation |> Seq.toArray
let adjust (direction:AdjustmentDirection) (aData:Matrix<float>) =
match direction with
| Obverse ->
Expand Down
20 changes: 9 additions & 11 deletions src/FSharp.Stats/Signal/Normalization.fs
Expand Up @@ -23,16 +23,15 @@ module Normalization =
///
/// The additional function is applied on all values of the matrix when calculating the normalization factors. By this, a zero in the original dataset will still remain zero.
let medianOfRatiosBy (f: float -> float) (data:Matrix<float>) =
let sampleWiseCorrectionFactors =
let sampleWiseCorrectionFactors =
data
|> Matrix.mapiRows (fun _ v ->
let v = Seq.map f v
|> Matrix.mapiRows (fun _ v ->
let v = RowVector.map f v
let geometricMean = Seq.meanGeometric v
Seq.map (fun s -> s / geometricMean) v
RowVector.map (fun s -> s / geometricMean) v
)
|> matrix
|> Matrix.ofRows
|> Matrix.mapiCols (fun _ v -> Vector.median v)
|> vector
data
|> Matrix.mapi (fun r c v ->
v / sampleWiseCorrectionFactors.[c]
Expand All @@ -53,13 +52,12 @@ module Normalization =
let sampleWiseCorrectionFactors =
data
|> Matrix.mapiCols (fun _ v ->
let v = Seq.map f v
let v = Vector.map f v
let geometricMean = Seq.meanGeometric v
Seq.map (fun s -> s / geometricMean) v
Vector.map (fun s -> s / geometricMean) v
)
|> matrix
|> Matrix.mapiCols (fun _ v -> Seq.median v)
|> vector
|> Matrix.ofCols
|> Matrix.mapiRows (fun _ v -> Seq.median v)
data
|> Matrix.mapi (fun r c v ->
v / sampleWiseCorrectionFactors.[r]
Expand Down

0 comments on commit 3d4f3b8

Please sign in to comment.