gpt4 book ai didi

python - 尝试使用 scipy 插入 2D 数据 - 只获取 NaN 数组

转载 作者:行者123 更新时间:2023-11-28 17:03:04 26 4
gpt4 key购买 nike

我正在尝试编写一段非常简单的代码,该代码将从一组现有数据中进行插值以创建值的综合分布。

我目前的代码如下所示:

import pandas as pd
import numpy as np
import scipy
from scipy.interpolate import griddata
import matplotlib

CRN_data=pd.read_table('disequilibrium data.dat',sep=',')
kzz=CRN_data['Kzz']
temperature=CRN_data['Temperature']
degree=CRN_data['Mean Degree']
points=np.ndarray(shape=(len(kzz),2),dtype='float')
for i in range(len(kzz)):
points[i][0]=kzz[i]
points[i][1]=temperature[i]
gridx,gridy= np.mgrid[0:1:100j,0:1:200j]
grid=griddata(points,degree,(gridx,gridy),method='cubic')
print grid

我插入的数据集如下所示:

Kzz,Temperature,Mean Degree,   
1.00E+06,400,7.41E+18
1.00E+06,500,4.48E+23
...
1.00E+08,400,4.67E+18
1.00E+08,500,6.88E+23
1.00E+08,750,1.88E+34
...
1.00E+10,750,2.73E+33
1.00E+10,900,2.82E+37
1.00E+10,1000,1.19E+39
...

然而,当代码运行时,我得到的主要输出是

[[ nan  nan  nan ...,  nan  nan  nan]
[ nan nan nan ..., nan nan nan]
[ nan nan nan ..., nan nan nan]
...,
[ nan nan nan ..., nan nan nan]
[ nan nan nan ..., nan nan nan]
[ nan nan nan ..., nan nan nan]]

这显然不是很有帮助。这是 Scipy 中的错误,还是(更有可能)我做错了什么?

最佳答案

您正在接收 nan 值,因为您请求的点包含在 gridxgridy 中的输入点的凸包之外 。您可以指定一个 fill_value 用于外推点,但您可能会考虑重新指定分配给 gridxgridy 的限制以产生有意义的结果结果,例如:

import pandas as pd
import numpy as np
from scipy.interpolate import griddata
import matplotlib.pyplot as plt

CRN_data = pd.DataFrame([
[1.00E+06,400,7.41E+18],
[1.00E+06,500,4.48E+23],
[1.00E+08,400,4.67E+18],
[1.00E+08,500,6.88E+23],
[1.00E+08,750,1.88E+34],
[1.00E+10,750,2.73E+33],
[1.00E+10,900,2.82E+37],
[1.00E+10,1000,1.19E+39]],
columns=['Kzz','Temperature','Mean Degree'])

kzz = CRN_data['Kzz']
temperature = CRN_data['Temperature']
degree = CRN_data['Mean Degree']

points = np.matrix([[kzz[i], temperature[i]] for i in range(len(kzz))])

gridx, gridy = np.mgrid[kzz.min():kzz.max():100j,temperature.min():temperature.max():200j]

grid = griddata(points, degree, (gridx, gridy), method='cubic')

产量:

[[7.41000000e+18 1.35147259e+22 2.70220418e+22 ...            nan
nan nan]
[ nan 1.07878728e+33 1.26216288e+33 ... nan
nan nan]
[ nan nan 1.38255505e+35 ... nan
nan nan]
...
[ nan nan nan ... 1.16569048e+39
nan nan]
[ nan nan nan ... 1.16394396e+39
1.17798560e+39 nan]
[ nan nan nan ... 1.16129655e+39
1.17564827e+39 1.19000000e+39]]

和绘图:

enter image description here

关于python - 尝试使用 scipy 插入 2D 数据 - 只获取 NaN 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53052633/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com