Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Torch/Lua,如何将数组配对到一个表中?_Lua_Statistics_Correlation_Torch - Fatal编程技术网

Torch/Lua,如何将数组配对到一个表中?

Torch/Lua,如何将数组配对到一个表中?,lua,statistics,correlation,torch,Lua,Statistics,Correlation,Torch,我需要在Torch/Lua程序中使用皮尔逊相关系数。 这就是功能: function math.pearson(a) -- compute the mean local x1, y1 = 0, 0 for _, v in pairs(a) do x1, y1 = x1 + v[1], y1 + v[2] print('x1 '..x1); print('y1 '..y1); end -- compute the coefficient x1, y1 = x1

我需要在Torch/Lua程序中使用皮尔逊相关系数。 这就是功能:

function math.pearson(a)
  -- compute the mean
  local x1, y1 = 0, 0
  for _, v in pairs(a) do
  x1, y1 = x1 + v[1], y1 + v[2]
  print('x1 '..x1);
  print('y1 '..y1);
  end
  -- compute the coefficient
  x1, y1 = x1 / #a, y1 / #a
  local x2, y2, xy = 0, 0, 0
  for _, v in pairs(a) do
    local tx, ty = v[1] - x1, v[2] - y1
    xy, x2, y2 = xy + tx * ty, x2 + tx * tx, y2 + ty * ty
  end

  return xy / math.sqrt(x2) / math.sqrt(y2)
end
此函数需要一个可在pairs()函数中使用的输入耦合表。 我试图向它提交正确的输入表,但我无法使任何东西正常工作

我试过:

z = {}
a = {27, 29, 45, 98, 1293, 0.1}
b = {0.0001, 0.001, 0.32132, 0.0001, 0.0009}
z = {a,b}
但不幸的是,它不起作用。它将计算b的第一个元素之间的对,而我希望它计算a和b之间的相关性。 我怎么办

你能提供一个好的对象作为math.pearson()函数的输入吗?

如果你正在使用,那么我认为它需要一个不同的参数结构。您应该传递一个包含两个值表的表,如中所示:

local z = {
  {27, 0.0001}, {29, 0.001}, {45, 0.32132}, {98, 0.0001}, {1293, 0.0009}
}
print(math.pearson(z))
这为我打印
-0.25304101592759