在python中插入2D数据网格

时间:2016-02-04 11:51:05

标签: python arrays numpy interpolation

我有一个具有放射性β衰变率的2D网格。每个阀门对应于特定温度和密度对上的速率(均以对数标度表示)。我想做的是,当我有一个温度和密度数据对(在得到它们的对数之后),找到表中的匹配值。我尝试使用scipy interpolate interpn function,但我有点困惑,我很感激你的帮助。

到目前为止我所拥有的:

if (a.count > 1) { //atleast 2 models
  var data1 = {};
  data1.Id = a.Id1;
  data1.Id2 = a.Id1_2;
  newModel(data1);
  var data2 = {};
  data2.Id = a.Id2;
  data2.Id2 = a.Id2_2;
  newModel(data2);

  //delete data1;
  //delete data2;
}
if (a.count > 5) { //atleast 6 models
  var data3 = {};
  data3.Id = a.Id3;
  data3.Id2 = a.Id3_2;
  newModel(data3);

  var data4 = {};
  data4.Id = a.Id4;
  data4.Id2 = a.Id4_2;
  newModel(data4);

  var data5 = {};
  data5.Id = a.Id5;
  data5.Id2 = a.Id5_2;
  newModel(data5);

  var data6 = {};
  data6.Id = a.Id6;
  data6.Id2 = a.Id6_2;
  newModel(data6);

}
if (a.count > 9) { //10 models
  var data7 = {};
  data7.Id = a.Id7;
  data7.Id2 = a.Id7_2;
  newModel(data7);

  var data8 = {};
  data8.Id = a.Id8;
  data8.Id2 = a.Id8_2;
  newModel(data8);

  var data9 = {};
  data9.Id = a.Id9;
  data9.Id2 = a.Id9_2;
  newModel(data9);

  var data10 = {};
  data10.Id = a.Id10;
  data10.Id2 = a.Id10_2;
  newModel(data10);

}

interpn函数具有以下参数:pointsx = np.array([7+0.2*i for i in range(0,16)]) #temperature range pointsy = np.array([i for i in range(0,11) ]) #rho_el range data = numpy.loadtxt(filename) #getting data from file logT = np.log10(T) #wanted temperature logarithmic logrho = np.log10(rho) #wanted rho logarithmic 。我认为点数将是我points, values, xi, method='linear', bounds_error=True, fill_value=nanpointsx,数据非常明显,pointsy将是xi我想要的。但我不确定,它们的尺寸应该是多少? (T,rho)的大小与points的大小相同吗?所以我必须创建一对dataT对应的数组,它们是rho部分,然后points(T, rho) }?

1 个答案:

答案 0 :(得分:0)

如果您对某项功能的工作原理不确定,打开REPL并自行测试总是一个好主意。在这种情况下,根据您对文档的理解,该功能完全按预期工作。

>>> points = [[1, 2, 3, 4], [1, 2, 3, 4]]  # Input values for each grid dimension
>>> values = [[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6], [4, 5, 6, 7]]  # The grid itself
>>> xi = (1, 1.5)
>>> scipy.interpolate.interpn(points, values, xi)
array([ 1.5])
>>> xi = [[1, 1.5], [2, 1.5], [2, 2.5], [3, 2.5], [3, 3.5], [4, 3.5]]
>>> scipy.interpolate.interpn(points, values, xi)
array([ 1.5,  2.5,  3.5,  4.5,  5.5,  6.5])

你唯一遗漏的是points应该是一个元组。但正如您从上面所看到的那样,即使points确定了一个元组,它也能正常工作。