Python 编程 - numpy polyfit 说 NAN

问题描述 投票:0回答:1

我编写的非常简单的代码遇到了一些问题。我有 4 组数据,想要使用 numpy polyfit 生成多项式最佳拟合线。使用 Polyfit 时,其中 3 个列表会产生数字,但使用 Polyfit 时,第三个数据集会产生 NAN。下面是代码和打印输出。有什么想法吗?

代码:

###all of the 'ind_#'s are the lists of data. Below converts them into numpy arrays that can then generate polynomial best fit line###

    ind_1=np.array(ind_1, np.float)
    
    dep_1=np.array(dep_1, np.float)
    
    x_1=np.arange(min(ind_1)-1, max(ind_1)+1, .01)
    
    ind_2=np.array(ind_2, np.float)
    
    dep_2=np.array(dep_2, np.float)
    
    x_2=np.arange(min(ind_2)-1, max(ind_2)+1, .01)
    
    ind_3=np.array(ind_3, np.float)
    
    dep_3=np.array(dep_3, np.float)
    
    x_3=np.arange(min(ind_3)-1, max(ind_3)+1, .01)
    
    ind_4=np.array(ind_4, np.float)
    
    dep_4=np.array(dep_4, np.float)
    
    x_4=np.arange(min(ind_4)-1, max(ind_4)+1, .01)

###Below prints off the arrays generated above, as well as the contents of the polyfit list, which are usually the coefficients of the polynomial equation, but for the third case below, all of the polyfit contents print off as NAN###

    print(ind_1)
    
    print(dep_1)
    
    print(np.polyfit(ind_1,dep_1,2))
    
    print(ind_2)
    
    print(dep_2)
    
    print(np.polyfit(ind_2,dep_2,2))
    
    print(ind_3)
    
    print(dep_3)
    
    print(np.polyfit(ind_3,dep_3,2))
    
    print(ind_4)
    
    print(dep_4)
    
    print(np.polyfit(ind_4,dep_4,2))

打印输出:

    [ 1.405  1.871  2.713 ...,  5.367  5.404  2.155]
    
    [ 0.274  0.07   0.043 ...,  0.607  0.614  0.152]
    
    [ 0.01391925 -0.00950728  0.14803846]
    
    [ 0.9760001  2.067      8.8       ...,  1.301      1.625      2.007    ]
    
    [ 0.219      0.05       0.9810001 ...,  0.163      0.161      0.163    ]
    
    [ 0.00886807 -0.00868727  0.17793324]
    
    [ 1.143      0.9120001  2.162     ...,  2.915      2.865      2.739    ]
    
    [ 0.283  0.3    0.27  ...,  0.227  0.213  0.161]
    
    [ nan  nan  nan]
    
    [ 0.167  0.315  1.938 ...,  2.641  1.799  2.719]
    
    [ 0.6810001  0.7140001  0.309     ...,  0.283      0.313      0.251    ]
    
    [ 0.00382331  0.00222269  0.16940372]

为什么第三种情况的 polyfit 常数列为 NAN?所有数据集具有相同类型的数据,并且所有代码都是一致的。请帮忙。

python numpy nan
1个回答
21
投票

刚刚查看了您的数据。发生这种情况是因为

NaN
(元素 713)中有一个
dep_3
。您可以确保在拟合中仅使用有限值,如下所示:

idx = np.isfinite(ind_3) & np.isfinite(dep_3)
print(np.polyfit(ind_3[idx], dep_3[idx], 2))

至于在大型数据集中查找错误值,numpy 使这变得非常容易。您可以这样找到索引:

print(np.where(~np.isfinite(dep_3)))
© www.soinside.com 2019 - 2024. All rights reserved.