Python: finding the intersection point of two gaussian curves

前端 未结 2 2071
醉酒成梦
醉酒成梦 2020-12-11 03:11

I have two gaussian plots:

x = np.linspace(-5,9,10000)
plot1=plt.plot(x,mlab.normpdf(x,2.5,1))
plot2=plt.plot(x,mlab.normpdf(x,5,1))

and I

2条回答
  •  心在旅途
    2020-12-11 03:31

    Here's a solution based on purely numpy that is also applicable to curves other than Gaussian.

    def get_intersection_locations(y1,y2,test=False,x=None): 
        """
        return indices of the intersection point/s.
        """
        idxs=np.argwhere(np.diff(np.sign(y1 - y2))).flatten()
        if test:
            x=range(len(y1)) if x is None else x
            plt.figure(figsize=[2.5,2.5])
            ax=plt.subplot()
            ax.plot(x,y1,color='r',label='line1',alpha=0.5)
            ax.plot(x,y2,color='b',label='line2',alpha=0.5)
            _=[ax.axvline(x[i],color='k') for i in idxs]
            _=[ax.text(x[i],ax.get_ylim()[1],f"{x[i]:1.1f}",ha='center',va='bottom') for i in idxs]
            ax.legend(bbox_to_anchor=[1,1])
            ax.set(xlabel='x',ylabel='density')
        return idxs
    
    # single intersection
    x = np.arange(-10, 10, 0.001)
    y1=sc.stats.norm.pdf(x,-2,2)
    y2=sc.stats.norm.pdf(x,2,3)
    get_intersection_locations(y1=y1,y2=y2,x=x,test=True) # returns indice/s array([10173])
    

    # double intersection
    x = np.arange(-10, 10, 0.001)
    y1=sc.stats.norm.pdf(x,-2,1)
    y2=sc.stats.norm.pdf(x,2,3)
    get_intersection_locations(y1=y1,y2=y2,x=x,test=True)
    

    Based on an answer to a similar question.

提交回复
热议问题