zoukankan      html  css  js  c++  java
  • Python kmean

    # -*- coding: utf-8 -*-
    from sklearn.cluster import KMeans
    from sklearn.externals import joblib
    import numpy

    feature =numpy.random.rand(40,20)

    #调用kmeans类
    clf = KMeans(n_clusters=9)
    s = clf.fit(feature)
    print s

    #9个中心
    print clf.cluster_centers_

    #每个样本所属的簇
    print clf.labels_

    #用来评估簇的个数是否合适,距离越小说明簇分的越好,选取临界点的簇个数
    print clf.inertia_

    #进行预测
    print clf.predict(feature)

    #保存模型
    joblib.dump(clf , 'c:/km.pkl')

    #载入保存的模型
    clf = joblib.load('c:/km.pkl')

    '''
    #用来评估簇的个数是否合适,距离越小说明簇分的越好,选取临界点的簇个数
    for i in range(5,30,1):
        clf = KMeans(n_clusters=i)
        s = clf.fit(feature)
        print i , clf.inertia_
    '''

  • 相关阅读:
    RPC(简单实现)
    观察者模式
    自省(Introspector)
    Mybatis学习笔记
    Nginx
    AJAX跨域
    手写Tomcat
    监听器模式
    回调
    Temporal Segment Networks
  • 原文地址:https://www.cnblogs.com/ahuo/p/5345690.html
Copyright © 2011-2022 走看看