import numpy as np
import matplotlib.pyplot as plt from sklearn import cluster
from sklearn.metrics import adjusted_rand_score
from sklearn.datasets.samples_generator import make_blobs def create_data(centers,num=100,std=0.7):
X, labels_true = make_blobs(n_samples=num, centers=centers, cluster_std=std)
return X,labels_true #层次聚类AgglomerativeClustering模型
def test_AgglomerativeClustering(*data):
'''
测试 AgglomerativeClustering 的用法
'''
X,labels_true=data
clst=cluster.AgglomerativeClustering()
predicted_labels=clst.fit_predict(X)
print("ARI:%s"% adjusted_rand_score(labels_true,predicted_labels)) # 用于产生聚类的中心点
centers=[[1,1],[2,2],[1,2],[10,20]]
# 产生用于聚类的数据集
X,labels_true=create_data(centers,1000,0.5)
# 调用 test_AgglomerativeClustering 函数
test_AgglomerativeClustering(X,labels_true)

def test_AgglomerativeClustering_nclusters(*data):
'''
测试 AgglomerativeClustering 的聚类结果随 n_clusters 参数的影响
'''
X,labels_true=data
nums=range(1,50)
ARIs=[]
for num in nums:
clst=cluster.AgglomerativeClustering(n_clusters=num)
predicted_labels=clst.fit_predict(X)
ARIs.append(adjusted_rand_score(labels_true,predicted_labels))
## 绘图
fig=plt.figure()
ax=fig.add_subplot(1,1,1)
ax.plot(nums,ARIs,marker="+")
ax.set_xlabel("n_clusters")
ax.set_ylabel("ARI")
fig.suptitle("AgglomerativeClustering")
plt.show() # 调用 test_AgglomerativeClustering_nclusters 函数
test_AgglomerativeClustering_nclusters(X,labels_true)

def test_AgglomerativeClustering_linkage(*data):
'''
测试 AgglomerativeClustering 的聚类结果随链接方式的影响
'''
X,labels_true=data
nums=range(1,50)
fig=plt.figure()
ax=fig.add_subplot(1,1,1) linkages=['ward','complete','average']
markers="+o*"
for i, linkage in enumerate(linkages):
ARIs=[]
for num in nums:
clst=cluster.AgglomerativeClustering(n_clusters=num,linkage=linkage)
predicted_labels=clst.fit_predict(X)
ARIs.append(adjusted_rand_score(labels_true,predicted_labels))
ax.plot(nums,ARIs,marker=markers[i],label="linkage:%s"%linkage) ax.set_xlabel("n_clusters")
ax.set_ylabel("ARI")
ax.legend(loc="best")
fig.suptitle("AgglomerativeClustering")
plt.show() # 调用 test_AgglomerativeClustering_linkage 函数
test_AgglomerativeClustering_linkage(X,labels_true)

吴裕雄 python 机器学习——层次聚类AgglomerativeClustering模型的更多相关文章

  1. 吴裕雄 python 机器学习——密度聚类DBSCAN模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import cluster from sklearn.metrics ...

  2. 吴裕雄 python 机器学习——支持向量机非线性回归SVR模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import datasets, linear_model,svm fr ...

  3. 吴裕雄 python 机器学习——KNN回归KNeighborsRegressor模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import neighbors, datasets from skle ...

  4. 吴裕雄 python 机器学习——KNN分类KNeighborsClassifier模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import neighbors, datasets from skle ...

  5. 吴裕雄 python 机器学习——半监督学习LabelSpreading模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import metrics from sklearn import d ...

  6. 吴裕雄 python 机器学习——支持向量机线性回归SVR模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import datasets, linear_model,svm fr ...

  7. 吴裕雄 python 机器学习——混合高斯聚类GMM模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import mixture from sklearn.metrics ...

  8. 吴裕雄 python 机器学习——K均值聚类KMeans模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import cluster from sklearn.metrics ...

  9. 吴裕雄 python 机器学习——分类决策树模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import datasets from sklearn.model_s ...

随机推荐

  1. [改错_19/04/01] 学习Java.IO 对象数据流时出现 Exception in thread "main" java.io.EOFException ...at cn.sxt.test.Test_DataStream.main(Test_DataStream.java:31) 错误 .

    过程描述:编译可以通过,就是每次运行时出现如下的图片,百思不得其解. 错误原因: byte[] datas=baos.toByteArray(); 放在了oos.writeInt(14);oos.fl ...

  2. js获取给定时间的下一天

    //获取给定时间的下一天 yyyy-MM-dd格式 function getNextDay(d){ d = new Date(d); d = +d + 1000*60*60*24; d = new D ...

  3. 简单使用Spring Boot+JpaRepository+hibernate搭建项目

    sql: -- -------------------------------------------------------- -- 主机: 127.0.0.1 -- 服务器版本: 10.3.9-M ...

  4. linux c 获取当前时间 毫秒级的 unix网络编程

    #include <time.h> #inlcude <sys/time.h> char *gf_time(void) /* get the time */{ struct t ...

  5. Gradle Goodness: Adding Tasks to a Predefined Group

    In Gradle we can group related tasks using the group property of a task. We provide the name of our ...

  6. Gradle Goodness: Unpacking an Archive

    To create an archive with Gradle is easy. We have several tasks like Zip, Tar, Jar, War and Ear to c ...

  7. swift实现一个对象池

    1.创建一个对象池 对象池:对象池一般用来管理一组可重用的对象, 这些对象的集合叫做对象池. 组件可以从对象池中借用对象, 完成一些任务之后将它归还给对象池. 返回的对象用于满足调用组件的后续请求, ...

  8. 浅谈React和VDom关系

    组件化 组件的封装 组件的复用 组件的封装 视图 数据 视图和数据之间的变化逻辑 import React, {Component} from 'react'; export default clas ...

  9. Const 关键字详解

    const 标识符 在c++中作为常量修饰符 用来修饰 函数 变量  指针 const 修饰的变量不可以改变值 const 在修饰指针的时候 const 标识符出现在*的左边表示 指向的变量为常量不能 ...

  10. Centos6.5 VM网络故障,可以Ping 通网关,无法上网或者访问其它网段

    首先查看cat /etc/sysconfig/network-scripts/ifcfg-eth0  配置是否正确 查看cat /etc/udev/rules.d/70-persistent-net. ...