将空间区域分成几组并为其上色

时间:2019-01-16 17:04:57

标签: python matplotlib classification

我有n个标量函数f_1(x)... f_n(x),它们将2d中的向量作为参数并给出正数作为结果。 我想考虑一个空间区域,例如[0.5,1.5] x [0.5,1.5],并在每个点上评估每个函数。然后,我想为每个点考虑返回更大数字的函数,并最终为每个区域着色。如下图所示

enter image description here

我唯一的想法是使用网格,例如:

xspace = np.linspace(0.5, 1.5, 100)
yspace =np.linspace(0.5, 1.5, 100)

然后

for x,y in zip(xspace,yspace):
     #evaluate each function, put it in an array and find the argmax
     #plot accordingly

但是,这需要花费大量时间,并且不会在点之间“填充空白”。

在这里提供一个函数示例(它使用一个向量x,一个向量均值和一个2x2协方差矩阵。如果我更改这两个参数中的任何一个,我将获得不同的结果):

def evaluatenormal(x,mean,covariance):
    xmat = np.matrix(x-mean)

    product = np.matmul(xmat,np.linalg.inv(covariance)).dot(x-mean)
    det = 1.0/np.sqrt((np.linalg.det(2*np.pi*covariance)))

    return np.array(det*np.exp(-0.5*product))

covmatrixlist = [array([[8.98752810e-05, 4.36396621e-05],
   [4.36396621e-05, 8.90258777e-05]]), array([[6.52653161e-04, 4.72522484e-05],
   [4.72522484e-05, 2.06834746e-04]]), array([[0.00013799, 0.00014587],
   [0.00014587, 0.00066962]]), array([[0.00026506, 0.00010979],
   [0.00010979, 0.00020168]]), array([[5.60530822e-06, 7.61528333e-06],
   [7.61528333e-06, 1.28104667e-05]])]   

meanvectorlist=[array([0.985294  , 0.99022067]), array([0.93870517, 0.943673  ]), array([0.99626442, 0.89631108]), array([0.81331842, 1.03552492]), array([1.00284467, 1.00364   ])]

point = np.array(1,1)

for x in xspace:
    for y in yspace:

        answerarray=[]
        for mean,cov in zip(meanvectorlist,covmatrixlist):
           answerarray.append(evaluatenormal(x,mean,cov))
        argmax=np.argmax(answerarray)
        if  argmax == 0:
            plt.plot(x,y, color = 'g', marker ='.',markersize = 4)
        elif argmax ==1:
            plt.plot(x,y, color = 'b', marker ='.',markersize = 4)
        elif argmax ==2:
            plt.plot(x,y, color = 'r', marker ='.',markersize = 4)
        elif argmax ==3:
            plt.plot(x,y, color = 'y', marker ='.',markersize = 4)
        elif argmax ==4:
            plt.plot(x,y, color = 'k', marker ='.',markersize = 4)
plt.show()

如何使所有空间充满颜色而不是点阵网格?

0 个答案:

没有答案