ImportError: cannot import name ‘GMM‘ from ‘sklearn.mixture‘ 问题解决

在《Python数据科学手册》的第308页,如果按照书上写的代码运行

from sklearn.mixture import GMM      # 1. Choose the model class
model = GMM(n_components=3,
            covariance_type='full')  # 2. Instantiate the model with hyperparameters
model.fit(X_iris)                    # 3. Fit to data. Notice y is not specified!
y_gmm = model.predict(X_iris)        # 4. Determine cluster labels

会出现如下报错

ImportError                               Traceback (most recent call last)
<ipython-input-2-df85f2dd883f> in <module>
----> 1 from sklearn.mixture import GMM      # 1. Choose the model class
      2 model = GMM(n_components=3,
      3             covariance_type='full')  # 2. Instantiate the model with hyperparameters
      4 model.fit(X_iris)                    # 3. Fit to data. Notice y is not specified!
      5 y_gmm = model.predict(X_iris)        # 4. Determine cluster labels

ImportError: cannot import name 'GMM' from 'sklearn.mixture'

这是因为导入高斯混合模型的代码不对,应该改为如下:

from sklearn.mixture import GaussianMixture                      #选择模型类

之后:

model =GaussianMixture(n_components = 3,covariance_type='full')  #设置超参数,初始化模型
model.fit(X_iris)                                                #拟合数据,注意不需要Y变量
y_gmm = model.predict(X_iris)                                    #确定簇标签

#将簇标签添加到鸢尾花的DataFrame中,然后用Seaborn画出结果
iris['cluster'] = y_gmm
sns.lmplot("PCA1","PCA2",data=iris,hue='species',col='cluster',fit_reg=False)

 就可以正确运行如下

这样问题就解决了 

如果各位觉着有用的话劳您动动小手给我点个赞,谢谢啦

  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

橡胶张小说

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值