2016-11-13 56 views
0

如蟒蛇初学者,我开始实施一个想法,我已经在Matlab中实现。我想确定数组的交点(x,y坐标),而没有给出明确的函数。也就是说,只有int值的数组可能与浮点坐标有交点。因此,www提供了以下代码,请参阅附件。np.array路口// AttributeError的:“模块”对象有没有属性“PiecewisePolynomial”

通过PIP scipyinterpolate安装后,我解决不了即将到来的

ERROR: AttributeError: 'module' object has no attribute 'PiecewisePolynomial'. 

搜索功能并没有帮助。

我将非常高兴通过

  1. 接收一些帮助解决该ERROR

或/和

  • 不同计算交点(是SciPy的唯一的方式)
  • PS:计算经由d的交点MATLAB的。差分不是权宜之计,由于路口的一些间隔

    import scipy.interpolate as interpolate 
    import scipy.optimize as optimize 
    import numpy as np 
    
    x1=np.array([1.4,2.1,3,5.9,8,9,23]) 
    y1=np.array([2.3,3.1,1,3.9,8,9,11]) 
    x2=np.array([1,2,3,4,6,8,9]) 
    y2=np.array([4,12,7,1,6.3,8.5,12])  
    
    p1 = interpolate.PiecewisePolynomial(x1,y1[:,np.newaxis]) 
    p2 = interpolate.PiecewisePolynomial(x2,y2[:,np.newaxis]) 
    
    def pdiff(x): 
        return p1(x)-p2(x) 
    
    xs=np.r_[x1,x2] 
    xs.sort() 
    x_min=xs.min() 
    x_max=xs.max() 
    x_mid=xs[:-1]+np.diff(xs)/2 
    roots=set() 
    for val in x_mid: 
        root,infodict,ier,mesg = optimize.fsolve(pdiff,val,full_output=True) 
        # ier==1 indicates a root has been found 
        if ier==1 and x_min<root<x_max: 
         roots.add(root[0]) 
    roots=list(roots)   
    print(np.column_stack((roots,p1(roots),p2(roots)))) 
    
    +1

    较新版本的scipy似乎没有这种方法,['PPoly'](https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.interpolate.PPoly。 html#scipy.interpolate.PPoly)似乎是可用的(它具有不同的语法和语义)。 –

    +0

    你明白错误在告诉你什么吗? – wwii

    +0

    你是说你在网上找到了一些代码,你不知道它是如何工作的? – wwii

    回答

    1

    正如我在评论指出的独特性,你试图使用的方法是不是在SciPy的较新型号的,但它没”不管做什么你都希望它做什么。

    我的建议是使用numpy.interpscipy.interpolate.interp1d来构建你的函数线性内插,然后使用fsolve因为你没有找到所有可能的交叉点。由于fsolve(很像MATLAB的fzero)一次只能找到一个交点,所以您确实需要遍历数据中的各个部分以查找交点。

    import scipy.interpolate as interpolate 
    import scipy.optimize as optimize 
    import numpy as np 
    
    x1 = np.array([1.4,2.1,3,5.9,8,9,23]) 
    y1 = np.array([2.3,3.1,1,3.9,8,9,11]) 
    x2 = np.array([1,2,3,4,6,8,9]) 
    y2 = np.array([4,12,7,1,6.3,8.5,12])  
    
    # linear interpolators 
    opts = {'fill_value': 'extrapolate'} 
    f1 = interpolate.interp1d(x1,y1,**opts) 
    f2 = interpolate.interp1d(x2,y2,**opts) 
    
    # possible range for an intersection 
    xmin = np.min((x1,x2)) 
    xmax = np.max((x1,x2)) 
    
    # number of intersections 
    xuniq = np.unique((x1,x2)) 
    xvals = xuniq[(xmin<=xuniq) & (xuniq<=xmax)] 
    # note that it's bad practice to compare floats exactly 
    # but worst case here is a bit of redundance, no harm 
    
    # for each combined interval there can be at most 1 intersection, 
    # so looping over xvals should hopefully be enough 
    # one can always err on the safe side and loop over a `np.linspace` 
    
    intersects = [] 
    for xval in xvals: 
        x0, = optimize.fsolve(lambda x: f1(x)-f2(x), xval) 
        if (xmin<=x0<=xmax 
         and np.isclose(f1(x0),f2(x0)) 
         and not any(np.isclose(x0,intersects))): 
         intersects.append(x0) 
    
    print(intersects) 
    print(f1(intersects)) 
    print(f2(intersects)) 
    
    从算法的问题较多的部分中的一些运行时警告

    除此之外,上述发现你的函数的两个交点:

    plot of functions

    关键步骤检查从fsolve结果是新的(不接近你以前的根源),并且你确实在给定的x0处找到了根。

    或者,您可以使用xvals中定义的区间,检查每个区间的分段线性函数,并分析检查这两个参数(我的意思是x1=xvals[i],x2=xvals[i+1], y11=f1[x1], y12=f1[x2]等)在给定区段上是否有交集。您可能可以将这种方法进行矢量化处理,您不必担心结果中的随机性,并且您只需要注意数据中可能出现的奇点(其中np.diff(xvals)很小,并且您将以此除数) 。


    numpy.interp没有定义的内插器的功能,而直接计算在网格上的内插值。为了使用此功能执行相同的操作,您必须定义一个函数,该函数针对给定的x值调用numpy.interp。由于在零搜索期间大量的功能评估,这可能会比上述效率低。

    +0

    谢谢Andras Deak对你的大力帮助。它为我工作。我将逐步分析你的代码,以更好地理解python。尽管存在运行时警告(=效率较低),但我对Python的速度印象深刻。我想知道,算法如何与更多的数据点和交叉点一起工作,会检查这个问题。十分感谢 ! – IMHO

    +0

    @IMHO我很高兴我能帮忙:)一些代码可能不完全是惯用的,但我无法想出更优雅的解决方案。警告大多告诉你,零搜索不够好,所以它对速度的描述很少。特别是对于很多交叉点,您应该考虑实施替代路线:查看'xvals'中的每个区间并分析计算/检查交叉点(不含'fsolve')。你明白我的意思吗? –

    +1

    @ Andras Deak我尊重你的谦虚,但在我看来你做得很好,再次非常感谢。作为一个提问者,我得到了一个超级快速和详细的答案,并且您对scipy.interpolate.interp1d&numpy.interp的提示对于Python初学者来说是值得的。我甚至没有想到新版本的scipy似乎没有我的方法。我仍然不明白为什么方法应该删除在一个新的版本没有提示其他方法,只是一个错误。你的代码在〜4000个数据点和~300个交点处工作得很好。我确实了解你的分析描述,并会尝试实施它。 – IMHO

    相关问题