在 PowerShell 中计算标准偏差时出错

Error When Calculating Standard Deviation in PowerShell

我正在尝试使用 PowerShell 为 CSV 文件的每一列中的值计算一些统计信息。 Measure-Object cmdlet 似乎可以解决我需要的所有问题,除了标准偏差。我在网上找到了一个描述,其中标准偏差是使用 [MATH] 计算的,但是当我 运行 代码时,我在包含 Pow():

的行中得到以下错误

Method invocation failed because system.management.automation.psobject doesn't contain a method named 'op_Subtraction'.

这是我的代码,如有任何帮助,我们将不胜感激:

$i = 1

While ($i -le 211) {

#Set the variable to the filename with the iteration number
$filename = "c:\zMFM\z550OutputdSummer\fixed20dSum550Output$i.csv"


#Check to see if that a file with $filename exists. If not, skip to the next iteration of $i. If so, run the code to collect the statistics for each variable and output them each to a different file
If (Test-Path $filename) {


#Calculate the Standard Deviation
#First get the average of the values in the column
$STDEVInputFile = Import-CSV $filename

#Find the average and count for column 'td'
$STDEVAVG = $STDEVInputFile | Measure-Object td -Average | Select Count, Average
$DevMath = 0

# Sum the squares of the differences between the mean and each value in the array
Foreach ($Y in $STDEVInputFile) {
$DevMath += [math]::pow(($Y - $STDEVAVG.Average), 2)

#Divide by the number of samples minus one
$STDEV = [Math]::sqrt($DevMath / ($STDEVAVG.Count-1))

}

#Calculate the basic statistics for column 'td' with the MEASURE-OBJECT cmdlet
$STATS = Import-CSV $Filename |
Measure-Object td -ave -max -min

#Append the standard deviation variable to the statistics array and add the value

$colSTDDEV = New-Object System.Data.DataColumn StdDev,([double])
$colVZA = New-Object System.Data.DataColumn VZA,([double])
$colVAZ = New-Object System.Data.DataColumn VAZ,([double])


$VZA = $Stats.VZA
$VAZ = $Stats.VAZ


$STATS.Columns.Add($colSTDDEV)
$STATS[0].StandardDev = $STDEV


$STATS.Columns.Add($colVZA)
$STATS[0].StandardDev = $VZA


$STATS.Columns.Add($colVAZ)
$STATS[0].StandardDev = $VAZ

#Export the $STATS file containing everything you need in the correct folder


Export-CSV -notype "c:\zMFM\z550OutputdSummerdSum550Statistics.csv"

}
$i++
}
$DevMath += [math]::pow(($Y - $STDEVAVG.Average), 2)

您可能需要将其替换为:

$DevMath += [math]::pow(($Y.Average - $STDEVAVG.Average), 2)

因为 $Y 似乎是一个对象,而不是一个数值。