Visualize-ML / Book1_Python-For-Beginners

Book_1_《编程不难》 | 鸢尾花书:从加减乘除到机器学习;请多多批评指正!
4.46k stars 859 forks source link

Book_1_Ch_36__高斯混合模型聚类代码存在疑惑 #100

Closed 17314796423 closed 10 months ago

17314796423 commented 10 months ago

因为说是知其然不需要知其所以然,所以我不了解这个奇异值分解协方差矩阵后得到的U的第一根长轴列向量与椭圆画出来的实际效果是怎样的对应关系,但直觉告诉我这个U的第一列就是长轴方向,椭圆也应该是朝这个长轴方向倾斜的,但是源代码画出来的椭圆和这个长轴方向错开了90度,根据代码里的意思,major应该对应长轴,minor是短轴,但是代码里最后画长短轴的时候这里跟注释使用的也是反过来的,所以我就很疑惑 1697449407165 我调转了major和minor的位置,此时向量指向的方向是我直觉上符合预期的(因为我知其然不知其所以然),如下图 image 所以是不是椭圆也应该是遵循此时的长短轴方向对调90度才是正确的代码呢

17314796423 commented 10 months ago

这个问题我在Ch_33就想问了

17314796423 commented 10 months ago

我直觉预期当中是长这样的 1697452476182

def make_ellipses(gmm, ax):

    st.title('gmm.covariance_type=' + gmm.covariance_type)
    # 可视化不同簇
    for j in range(0, K):
        # 四种不同的协方差矩阵
        if gmm.covariance_type == 'full':
            covariances = gmm.covariances_[j]
        elif gmm.covariance_type == 'tied':
            covariances = gmm.covariances_
        elif gmm.covariance_type == 'diag':
            covariances = np.diag(gmm.covariances_[j])
        elif gmm.covariance_type == 'spherical':
            covariances = np.eye(gmm.means_.shape[1])
            covariances = covariances*gmm.covariances_[j]

        # 用奇异值分解完成特征值分解
        U, S, V_T = np.linalg.svd(covariances)
        # 计算长轴、短轴长度
        major, minor = 2 * np.sqrt(S)

        # 计算椭圆长轴旋转角度
        angle = np.arctan2(U[1, 0], U[0, 0])
        angle = 180 * angle / np.pi
        # 多元高斯分布中心
        ax.plot(gmm.means_[j, 0], gmm.means_[j, 1],
                color='k', marker='x', markersize=10)
        # ax.text(gmm.means_[j, 0], gmm.means_[j, 1], str(j), fontsize=12)
        # 绘制半长轴向量
        ax.quiver(gmm.means_[j, 0], gmm.means_[j, 1],
                  U[0, 0], U[1, 0], scale=5/major)
        # 绘制半短轴向量
        ax.quiver(gmm.means_[j, 0], gmm.means_[j, 1],
                  U[0, 1], U[1, 1], scale=5/minor)

        st.latex(r'\Sigma = ' + bmatrix(covariances))
        st.latex(r'U  = ' + bmatrix(U))
        st.latex(r'\Lambda = ' + bmatrix(np.diag(S)))
        st.latex(r'V^T = ' + bmatrix(V_T))
        st.info('angle = {}'.format(angle))

        # 绘制椭圆
        for scale in np.array([3, 2, 1]):

            ell = Ellipse(gmm.means_[j, :2],
                          scale*minor,
                          scale*major,
                          angle=angle - 90,
                          color=rgb[j, :],
                          alpha=0.18)
            ax.add_artist(ell)
17314796423 commented 10 months ago

代码angle计算时候的含义是angle = 180*(arctan(y/x)/π),那么如果angle=60,这个角度想表示的实际意义我觉着应该是椭圆长轴从x正半轴沿逆时针的60度,而Ellipse这个api的angle=60表示的含义我测了下貌似是椭圆长轴从y轴正半轴开始沿逆时针60度,也就是说代码中的angle应该减去90才是我想要的

Visualize-ML commented 10 months ago

谢谢你,我查一下代码哈!过一两天回复你。请继续批评

On Mon, Oct 16, 2023 at 6:48 AM VolcanicMachinery @.***> wrote:

代码angle计算时候的含义是angle = 180*(arctan(y/x)/π),那么如果angle=60,这个角度想表示的实际意义我觉着应该是椭圆从x正半轴沿逆时针的60度,而Ellipse这个api的angle=60表示的含义我测了下貌似是从y轴正半轴开始沿逆时针60度,也就是说代码中的angle应该减去90才是我想要的

— Reply to this email directly, view it on GitHub https://github.com/Visualize-ML/Book1_Python-For-Beginners/issues/100#issuecomment-1764206804, or unsubscribe https://github.com/notifications/unsubscribe-auth/AZHC6V7KO3HO7LDEL2ZNRR3X7UGGNAVCNFSM6AAAAAA6B4W7K6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONRUGIYDMOBQGQ . You are receiving this because you are subscribed to this thread.Message ID: @.*** com>

Visualize-ML commented 10 months ago

谢谢你,确实错了。两处major,minor写反了... 刚刚更新了代码。请继续批评指正。

On Mon, Oct 16, 2023 at 11:30 AM James Jiang @.***> wrote:

谢谢你,我查一下代码哈!过一两天回复你。请继续批评

On Mon, Oct 16, 2023 at 6:48 AM VolcanicMachinery < @.***> wrote:

代码angle计算时候的含义是angle = 180*(arctan(y/x)/π),那么如果angle=60,这个角度想表示的实际意义我觉着应该是椭圆从x正半轴沿逆时针的60度,而Ellipse这个api的angle=60表示的含义我测了下貌似是从y轴正半轴开始沿逆时针60度,也就是说代码中的angle应该减去90才是我想要的

— Reply to this email directly, view it on GitHub https://github.com/Visualize-ML/Book1_Python-For-Beginners/issues/100#issuecomment-1764206804, or unsubscribe https://github.com/notifications/unsubscribe-auth/AZHC6V7KO3HO7LDEL2ZNRR3X7UGGNAVCNFSM6AAAAAA6B4W7K6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONRUGIYDMOBQGQ . You are receiving this because you are subscribed to this thread.Message ID: @.*** .com>

Visualize-ML commented 10 months ago

谢谢你,这个纠错的含金量很高。本册出版后,送你一本书 + 签名卡,以表感谢。邮寄信息发到我的邮箱。

PDF左下角有邮箱地址。再次感谢,请继续批评指正。

17314796423 commented 10 months ago

谢谢你,这个纠错的含金量很高。本册出版后,送你一本书 + 签名卡,以表感谢。邮寄信息发到我的邮箱。

PDF左下角有邮箱地址。再次感谢,请继续批评指正。

非常感谢大佬的回答解决了我的疑惑!It‘s my honor ! 等我啃完后面三本数学书一定买下这三本回家供着支持你!