目录

  • 1 课程简介
    • 1.1 教学大纲
    • 1.2 授课计划
    • 1.3 课内实训指导书
  • 2 绪论
    • 2.1 数据挖掘的概念和任务
    • 2.2 十大经典数据挖掘算法
    • 2.3 开放数据获取来源-数据挖掘常见误区
    • 2.4 数据挖掘中的隐私保护
  • 3 认识数据
    • 3.1 数据对象和数据属性
    • 3.2 数据的统计描述
    • 3.3 数据可视化
    • 3.4 数据相似性度量
    • 3.5 数据可视化案例综合应用
  • 4 数据预处理
    • 4.1 数据清洗
    • 4.2 数据集成与数据规约
    • 4.3 数据离散化与概念分层
  • 5 分类与预测
    • 5.1 分类与预测:基本概念
    • 5.2 决策树分类
    • 5.3 朴素贝叶斯分类
    • 5.4 决策树方法的分析比较
    • 5.5 KNN分类算法
    • 5.6 分类与预测算法的性能评价方法
    • 5.7 高级分类算法
  • 6 回归分析
    • 6.1 基本概念
    • 6.2 线性回归编程案例
    • 6.3 逻辑回归
    • 6.4 岭回归
    • 6.5 CART分类回归树
    • 6.6 从线性回归到神经网络
    • 6.7 神经网络训练&神经网络设计原则
    • 6.8 过拟合与正则化
  • 7 关联规则挖掘
    • 7.1 基本概念
    • 7.2 闭项集和极大频繁项
    • 7.3 Apriori算法及其应用
    • 7.4 关联挖掘的常见误区
    • 7.5 FP树及软件实践
    • 7.6 课堂实录 Apriori基础与算法
    • 7.7 课堂实录 Apriori算法分析与案例应用
  • 8 聚类分析
    • 8.1 聚类概述
    • 8.2 聚类的划分方法
    • 8.3 聚类的层次方法
    • 8.4 聚类的密度方法
    • 8.5 孤立点分析
    • 8.6 Kmeans简单实战
    • 8.7 Kmeans常见错误解析
    • 8.8 Kmeans实现数据无监督分类
  • 9 案例开发与综合应用
    • 9.1 scikitlearn安装与配置
    • 9.2 KNN预测男女
    • 9.3 KNN测试自带数据评分对比以及绘图
    • 9.4 KNN用于分类
    • 9.5 KNN用于数据回归预测
    • 9.6 KNN基于历史数据预测未来
  • 10 课程实验
    • 10.1 实验一:数据可视化实践
    • 10.2 实验二:数据规范化实践
    • 10.3 实验三:朴素贝叶斯分类与预测
    • 10.4 实验四:决策树分类及可视化
    • 10.5 实验五:关联规则挖掘
    • 10.6 实验六:Kmeans聚类
    • 10.7 实验七:密度聚类
    • 10.8 实验八:案例综合应用
  • 11 python数据挖掘编程讲解
    • 11.1 函数练习
    • 11.2 函数编程
    • 11.3 可视化编程
    • 11.4 机器学习库的基本编程
实验六:Kmeans聚类

from numpy import *

import time

import matplotlib.pyplot as plt



# calculate Euclidean distance

def euclDistance(vector1, vector2):

    return sqrt(sum(power(vector2 - vector1, 2)))



# init centroids with random samples

def initCentroids(dataSet, k):

    numSamples, dim = dataSet.shape

    centroids = zeros((k, dim))

    for i in range(k):

        index = int(random.uniform(0, numSamples))

        centroids[i, :] = dataSet[index, :]

    return centroids



# k-means cluster

def kmeans(dataSet, k):

    numSamples = dataSet.shape[0]

    # first column stores which cluster this sample belongs to,

    # second column stores the error between this sample and its centroid

    clusterAssment = mat(zeros((numSamples, 2)))

    clusterChanged = True


    ## step 1: init centroids

    centroids = initCentroids(dataSet, k)


    while clusterChanged:

        clusterChanged = False

        ## for each sample

        for i in range(numSamples):

            minDist = 100000.0

            minIndex = 0

            ## for each centroid

            ## step 2: find the centroid who is closest

            for j in range(k):

                distance = euclDistance(centroids[j, :], dataSet[i, :])

                if distance < minDist:

                    minDist = distance

                    minIndex = j


            ## step 3: update its cluster

            if clusterAssment[i, 0] != minIndex:

                clusterChanged = True

                clusterAssment[i, :] = minIndex, minDist ** 2


        ## step 4: update centroids

        for j in range(k):

            pointsInCluster = dataSet[nonzero(clusterAssment[:, 0].A == j)[0]]

            centroids[j, :] = mean(pointsInCluster, axis=0)


    print('Congratulations, cluster complete!')


    return centroids, clusterAssment



# show your cluster only available with 2-D data

def showCluster(dataSet, k, centroids, clusterAssment):

    numSamples, dim = dataSet.shape

    if dim != 2:

        print("Sorry! I can not draw because the dimension of your data is not2!")


        return 1


    mark = ['or', 'ob', 'og', 'ok', '^r', '+r', 'sr', 'dr', '<r', 'pr']

    if k > len(mark):

        print("Sorry! Your k is too large! please contact Zouxy")

        return 1


    # draw all samples

    for i in range(numSamples):

        markIndex = int(clusterAssment[i, 0])

        plt.plot(dataSet[i, 0], dataSet[i, 1], mark[markIndex])


    mark = ['Dr', 'Db', 'Dg', 'Dk', '^b', '+b', 'sb', 'db', '<b', 'pb']

    # draw the centroids

    for i in range(k):

        plt.plot(centroids[i, 0], centroids[i, 1], mark[i], markersize=12)


    plt.show()