在 Powershell [MATH] 函数中迭代单元格
Iterating Through Cells in a Powershell [MATH] Function
我正在尝试使用 [MATH] 在 PowerShell 中计算一些基本统计数据,特别是名为 'td' 的字段中一组值的标准差。
我的代码当前输出的数字太高而不能成为标准偏差。例如,如果列中的值没有范围(所有值均为 15),标准差应为零,则返回值等于 15。
我认为这可能与这段代码中 $Y
的错误使用有关:
Foreach ($Y in $STDEVInputFile) {
$DevMath += [math]::pow(($STDEVInputFile[$Y].td - $STDEVAVG.Average), 2)
不过,我对 PowerShell 还不够熟悉,无法确定。外面有人能告诉我哪里出错了吗?这是完整的代码:
##################################################
#Calculate statistics for TD column in each file
$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
$Y =
# Sum the squares of the differences between each value in the field and the mean
Foreach ($Y in $STDEVInputFile) {
$DevMath += [math]::pow(($STDEVInputFile[$Y].td - $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 |
#Export the statistics as a CSV
Export-CSV -notype "c:\zMFM\z550OutputdSummer\tempstats$i.csv"
#Store the values that will go into the final table as variables
$GetColumns = Import-CSV $filename
$VZA = $GetColumns[0].VZA
$VAZ = $GetColumns[0].VAZ
#Import the temporary stats file, append columns and populate them with the declared variables
Import-Csv "c:\zMFM\z550OutputdSummer\tempstats$i.csv" |
Select-Object @{Name="VZA";Expression={$VZA}},
@{Name="VAZ";Expression={$VAZ}},
@{Name="STDDEV";Expression={$STDEV}},
Count, Average, Maximum, Minimum, Property |
#Export the $STATS file containing everything you need in the correct folder
Export-CSV -notype "c:\zMFM\z550OutputdSummer\Statistics_TD_20dSum550_$i.csv"
}
$i++
}
我认为:
$DevMath += [math]::pow(($STDEVInputFile[$Y].td - $STDEVAVG.Average), 2)
应该是
$DevMath += [math]::pow(($Y.td - $STDEVAVG.Average), 2)
原始的 $STDEVInputFile[$Y].td
部分将变为 return null/0,因此对于输入中所有 15 的示例,而不是每一步执行 15-15=0 的平方,你在做 0-15 = -15 的平方。
您可能还想去掉 foreach 循环之前的 $Y= 空行,因为它(可能)将 $Y 设置为整个循环的输出,如果没有别的,则在使用 $ 时使用它循环中的 Y 令人困惑。
我正在尝试使用 [MATH] 在 PowerShell 中计算一些基本统计数据,特别是名为 'td' 的字段中一组值的标准差。
我的代码当前输出的数字太高而不能成为标准偏差。例如,如果列中的值没有范围(所有值均为 15),标准差应为零,则返回值等于 15。
我认为这可能与这段代码中 $Y
的错误使用有关:
Foreach ($Y in $STDEVInputFile) {
$DevMath += [math]::pow(($STDEVInputFile[$Y].td - $STDEVAVG.Average), 2)
不过,我对 PowerShell 还不够熟悉,无法确定。外面有人能告诉我哪里出错了吗?这是完整的代码:
##################################################
#Calculate statistics for TD column in each file
$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
$Y =
# Sum the squares of the differences between each value in the field and the mean
Foreach ($Y in $STDEVInputFile) {
$DevMath += [math]::pow(($STDEVInputFile[$Y].td - $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 |
#Export the statistics as a CSV
Export-CSV -notype "c:\zMFM\z550OutputdSummer\tempstats$i.csv"
#Store the values that will go into the final table as variables
$GetColumns = Import-CSV $filename
$VZA = $GetColumns[0].VZA
$VAZ = $GetColumns[0].VAZ
#Import the temporary stats file, append columns and populate them with the declared variables
Import-Csv "c:\zMFM\z550OutputdSummer\tempstats$i.csv" |
Select-Object @{Name="VZA";Expression={$VZA}},
@{Name="VAZ";Expression={$VAZ}},
@{Name="STDDEV";Expression={$STDEV}},
Count, Average, Maximum, Minimum, Property |
#Export the $STATS file containing everything you need in the correct folder
Export-CSV -notype "c:\zMFM\z550OutputdSummer\Statistics_TD_20dSum550_$i.csv"
}
$i++
}
我认为:
$DevMath += [math]::pow(($STDEVInputFile[$Y].td - $STDEVAVG.Average), 2)
应该是
$DevMath += [math]::pow(($Y.td - $STDEVAVG.Average), 2)
原始的 $STDEVInputFile[$Y].td
部分将变为 return null/0,因此对于输入中所有 15 的示例,而不是每一步执行 15-15=0 的平方,你在做 0-15 = -15 的平方。
您可能还想去掉 foreach 循环之前的 $Y= 空行,因为它(可能)将 $Y 设置为整个循环的输出,如果没有别的,则在使用 $ 时使用它循环中的 Y 令人困惑。