【问题标题】:Error When Calculating Standard Deviation in PowerShell在 PowerShell 中计算标准差时出错
【发布时间】:2016-03-12 04:30:24
【问题描述】:

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

方法调用失败,因为 system.management.automation.psobject 不包含名为“op_Subtraction”的方法。

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

$i = 1

While ($i -le 211) {

#Set the variable to the filename with the iteration number
$filename = "c:\zMFM\z550Output\20dSummer\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\z550Output\20dSummer\20dSum550Statistics.csv"

}
$i++
}

【问题讨论】:

    标签: csv powershell statistics standard-deviation


    【解决方案1】:
    $DevMath += [math]::pow(($Y - $STDEVAVG.Average), 2)
    

    您可能需要将其替换为:

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

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

    【讨论】:

    • 谢谢,这成功了,至少在错误信息消失的意义上。现在我遇到了程序似乎挂起的问题,所以我看不到输出是否真的正确。有什么想法吗?
    • 在发布另一个问题之前:),尝试逐行注释和取消注释您的代码以隔离罪魁祸首,或使用调试器 (technet.microsoft.com/fr-fr/library/dd819480.aspx)
    猜你喜欢
    • 2021-12-27
    • 2021-03-27
    • 1970-01-01
    • 2015-05-26
    • 1970-01-01
    • 1970-01-01
    • 2017-09-26
    • 2016-04-14
    • 2020-01-28
    相关资源
    最近更新 更多