与数据帧和数组一起使用时如何使用scipy griddata

时间:2017-01-07 03:27:13

标签: python pandas scipy interpolation

我有以下数据框:

    A   B   C
0   2   0.7904  0.278784507354
1   2   0.7904  0.278784507354
2   2   0.7904  0.348480634192
3   2   0.7904  0.348480634192
4   2   0.7904  0.418176761031
5   2   0.7904  0.418176761031
6   2   0.7904  0.487872887869
7   2   0.7904  0.487872887869
8   2   0.7904  0.529690563972
9   2   0.7904  0.529690563972
10  2   0.7904  0.54362978934
11  2   0.7904  0.54362978934
12  2   0.7904  0.557569014708
13  2   0.7904  0.557569014708
14  2   0.7904  0.571508240076
15  2   0.7904  0.571508240076
16  2   0.7904  0.585447465443
17  2   0.7904  0.585447465443
18  2   0.7904  0.592417078127
19  2   0.7904  0.592417078127
20  2   0.7904  0.599386690811
21  2   0.7904  0.599386690811
22  2   0.7904  0.606356303495
23  2   0.7904  0.606356303495
24  2   0.7904  0.613325916179
25  2   0.7904  0.613325916179
26  2   0.7904  0.620295528862
27  2   0.7904  0.620295528862
28  2   0.7904  0.627265141546
29  2   0.7904  0.627265141546
30  2   0.7904  0.63423475423
31  2   0.7904  0.63423475423
32  2   0.7904  0.641204366914
149 2   0.3847  1.04544190258
150 2   0.3847  1.05241151526
151 2   0.4248  1.05241151526
152 2   0.3847  1.05938112794
153 2   0.4248  1.05938112794
154 2   0.3847  1.06635074063
155 2   0.4248  1.06635074063
156 2   0.3847  1.07332035331
157 2   0.4248  1.07332035331
158 2   0.3847  1.08725957868
159 2   0.4248  1.08725957868
235 9   0.6816  0.919988874268
236 9   0.8164  0.926958486952
237 9   0.6608  0.926958486952
238 9   0.64    0.933928099636
239 9   0.7449  0.933928099636
240 9   0.7289  0.940897712319
241 9   0.6764  0.940897712319
242 9   0.7128  0.947867325003
243 9   0.7128  0.947867325003
244 9   0.5883  0.954836937687
245 9   0.6626  0.954836937687
246 9   0.675   0.961806550371
247 9   0.675   0.961806550371
350 16  0.6229  0.933928099636
351 16  0.6641  0.933928099636
352 16  0.7124  0.940897712319
353 16  0.7124  0.940897712319
354 16  0.6814  0.947867325003
355 16  0.6193  0.947867325003
596 23  0.4222  1.15695570552
597 23  0.4928  1.15695570552
598 23  0.4222  1.17089493089
599 23  0.4928  1.17089493089
600 23  0.4928  1.18483415625
709 30  0.5404  1.15695570552
710 30  0.5088  1.17089493089
711 30  0.5439  1.17089493089
712 30  0.4953  1.18483415625
713 30  0.4953  1.18483415625
714 30  0.4953  1.19877338162
715 30  0.4953  1.19877338162
716 30  0.4953  1.21271260699
717 30  0.4953  1.21271260699
718 30  0.4953  1.22665183236
719 30  0.4953  1.22665183236
778 37  0.6862  0.961806550371
799 37  0.5957  1.03150267721
800 37  0.6671  1.03847228989
801 37  0.6085  1.03847228989
802 37  0.5883  1.04544190258
826 37  0.5134  1.18483415625
827 37  0.6135  1.18483415625
874 58  0.769   0.864231972797
875 58  0.7491  0.864231972797
876 58  0.768   0.878171198165
939 58  0.4921  1.32422640993
940 58  0.4921  1.39392253677
941 58  0.4902  1.39392253677
942 58  0.4921  1.46361866361
943 58  0.4902  1.46361866361
944 114 1.1536  0.0696961268385
954 114 1.0766  0.348480634192
955 114 1.1536  0.348480634192
956 114 1.1536  0.418176761031

还有更多观察结果,但由于帖子大小限制,我需要截断。

我正试图在"网格"上进行插值。使用以下内容:

interp_A = np.array([30,60,90,180])
interp_B = np.array([1.0,1.0,1.0,1.0])
grid_z1 = griddata((data['A'],data['B']), data['C'], (interp_A, interp_B), method='nearest')

我要回来了:

675     0.6057
895     0.6492
1039    0.6884
1256    0.6996

鉴于我已经完成了一些测试,我的30,60,90,180被映射到675,895,1039和1256.

如果我改为打电话:

grid_z1 = griddata((data['A'].values,data['B'].values), data['C'].values, (interp_A, interp_B), method='nearest')

我明白了:

[ 0.54    0.6464  0.6673  0.6772]

将此库与pandas数据一起使用的正确方法是什么?

谢谢!

1 个答案:

答案 0 :(得分:0)

使用public void playSound(){ // Initialize the alert and ringtone again. MainActivity.alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); MainActivity.r = RingtoneManager.getRingtone(getApplicationContext(), alert); MainActivity.r.play(); } public void onDestroy() { MainActivity.r.stop(); super.onDestroy(); } 中的示例我计算

<Costura>
    <IncludeAssemblies>
      log4net
    </IncludeAssemblies>
</Costura>

并制作了一个数据框:

griddata

grid_z0 = interpolate.griddata(points, values, (grid_x, grid_y), method='nearest') 我得到与原始插值相同的插值:

df = pd.DataFrame({'A':points[:,0], 'B':points[:,1], 'C':values})

但如果我试图复制你的其他方法

values

我收到错误:

grid_z1 = interpolate.griddata((df['A'].values,df['B'].values), df['C'].values, (grid_x, grid_y), method='nearest')
np.allclose(grid_z1,grid_z0)   # True

错误来自grid_z2 = interpolate.griddata((df['A'],df['B']), df['C'], (grid_x, grid_y), method='nearest') 索引。我的数据框架可能与您的数据框架结构不同。

ValueError: Buffer has wrong number of dimensions (expected 1, got 2)

在任何情况下,将列值传递给pandas都是正确的方法。 In [17]: df.info() <class 'pandas.core.frame.DataFrame'> Int64Index: 1000 entries, 0 to 999 Data columns (total 3 columns): A 1000 non-null float64 B 1000 non-null float64 C 1000 non-null float64 dtypes: float64(3) memory usage: 31.2 KB 并非旨在直接处理panda griddata。它需要numpy数组,而不是包含数组的对象。