Matrices in PowerShell

I’ve been exploring how well PowerShell is suited for creating machine learning (ML) systems. My informal definition of ML is any system that uses data to make a prediction. Examples of ML systems include logistic regression binary classification and neural network classification.

PowerShellMatrixDemo

The heart of many ML software systems includes numeric matrices. So, as part of my ML-with-PowerShell investigation I needed to look at matrices. The bottom line is that PowerShell matrices are completely capable of creating ML systems. Here is a short demo:

# matrixDemo.ps1

write-host "`nBegin PowerShell matrix demo"

function MakeMatrix([int]$rows, [int]$cols,
  [double]$v)
{
  [double[][]] $m = new-object double[][] $rows
  for ($i = 0; $i -lt $m.Length; $i++) {
    $m[$i] = new-object double[] $cols
  }
  for ($i = 0; $i -lt $m.Length; $i++) {
    for ($j = 0; $j -lt $m[$i].Length; $j++) {
      $m[$i][$j] = $v
    }
  }
  return $m
}

function ShowMatrix([double[][]] $m,
 [int]$decimals, [bool]$indices)
{
  [int]$pad = $m.Length.ToString().Length
  for ($i = 0; $i -lt $m.Length; $i++) {
    if ($indices -eq $true) {
      $t = "[" + $i.ToString().PadLeft($pad)
      $t += "]  "
      write-host -nonewline $t
    }

    for ($j = 0; $j -lt $m[$i].Length; $j++) {
      $x = $m[$i][$j]
      [string]$fmt = "{0:N" + $decimals + "}"
      [string]$s = $($fmt -f $x) + "  "
      write-host -nonewline $s
    }
    write-host ""
  }
}

write-host "`nCreating 5x3 matrix with all cells init to 1.1"
$rows = 5
$cols = 3
$val = 1.1
$matrix = MakeMatrix $rows $cols $val 

write-host "`nThe matrix is:"
ShowMatrix $matrix 2 $false

write-host "`nEnd demo`n"

The demo uses a script-defined MakeMatrix function rather than creating the matrix directly. PowerShell can create a true two-dimensional matrix, but in ML it’s far more common is to use an array-of-arrays style matrix so that rows can be manipulated directly.

There are many ways to create an array. The demo uses the new-object approach, which is in my opinion the most understandable. The ShowMatrix helper function can print a matrix with or without row indices. Here’s a related function that loads data from a text file into a numeric matrix:

function LoadData([string]$file, [char]$delim)
{
  $f = get-content $file
  [int]$rows = $f.Length 
  $cols = $f[0].split($delim).Length 

  [double[][]] $result = new-object double[][] $rows
  for ($i = 0; $i -lt $result.Length; $i++) {
    $result[$i] = new-object double[] $cols
  }

  $i = 0
  foreach ($line in $f) {
    $tokens = $line.split($delim)
    for ($j = 0; $j -lt $tokens.Length; $j++) {
      $result[$i][$j] = $tokens[$j].trim()
    }
    $i++
  }
  return $result
}

For a comma-delimited text file, this function can be called along the lines of:

$file = ‘C:\Data\someData.txt’
$delim = ‘,’
$matrix = LoadData $file $delim

Sometimes it’s useful to directly create and initialize a matrix. For example:

[double[][]] $m = new-object double[][] 3
$m[0] = (2.0, 4.0, 6.0, 8.0)
$m[1] = (2.1, 4.1, 6.1, 8.1)
$m[2] = (2.2, 4.2, 6.2, 8.2)

ShowMatrix $m 2 $true

This entry was posted in Machine Learning. Bookmark the permalink.