K-means之亚洲杯

import numpy as np
import xlrd
from sklearn.cluster import KMeans
from sklearn import preprocessing
#胜 平 负 进球 失球 控球率 传球成功率 抢断成功率 射正
# =============================================================================
# data=[[1,1,0,3,1,65.5,77.6,53.8,3,4],[1,0,1,4,3,34.5,57.4,50,2,3],[1,0,1,2,4,41.8,60.5,85.7,2,3],[0,1,1,1,2,58.2,70.8,50,3,1],
#       [2,0,0,3,0,34.8,70.3,64.3,4,6],[1,0,1,3,1,68,85.2,50,6,3],[0,1,1,0,2,65.2,79.8,50,2,1],[0,1,1,0,3,32,69.9,66.7,0,1],
#       [2,0,0,5,1,54.9,77.2,61.5,7,6],[2,0,0,2,0,70.9,87.6,61.1,7,6],[0,0,2,1,3,29.1,67.5,52.9,2,0],[0,0,2,0,4,45.1,69.8,91.7,1,0],
#       [2,0,0,7,0,68,83.7,68.8,5,6],[2,0,0,6,2,61.8,88.2,70.6,5,6],[0,0,2,2,5,32,65.5,50,2,0],[0,0,2,0,8,38.2,79.5,90.9,2,0],
#       [2,0,0,6,0,69.5,87.8,81.2,7,6],[2,0,0,8,0,64,84.9,71.4,6,6],[0,0,2,0,10,36,78.3,53.8,0,0],[0,0,2,0,4,30.5,71.5,53.3,0,0],
#       [2,0,0,4,2,62.6,81.5,63.6,7,6],[1,0,0,2,1,40,78.7,64.7,3,6],[0,0,1,2,3,30.1,68.8,50,4,0],[0,0,2,1,3,37.4,72.1,80,1,0]]
# =============================================================================
def xlrd_read_data(path):
    table = xlrd.open_workbook(path).sheets()[0] #读取第一个表格
    row = table.nrows  # 行数
    col = table.ncols  # 列数
    datamatrix = np.zeros((row, col))#生成一个nrows行ncols列,且元素均为0的初始矩阵
    for x in range(col):
        cols = np.matrix(table.col_values(x))  # 把list转换为矩阵进行矩阵操作
        datamatrix[:, x] = cols # 按列把数据存进矩阵中
    return datamatrix
def standardScaler(datamatrix):
    #标准化
    scaler=preprocessing.StandardScaler().fit(datamatrix)
    return (scaler.transform(datamatrix))
def kmeans(data_stand):
    estimator = KMeans(n_clusters=3) #聚为三类球队,构造聚类器    
    estimator.fit(data_stand)#聚类    
    label_pred = estimator.labels_#获取聚类标签
    centroids = estimator.cluster_centers_#获取聚类中心    
    inertia = estimator.inertia_ #获取聚类准则的总和
    dis=estimator.precompute_distances
    print(dis,inertia,centroids)
    return label_pred
path = r'c:\Users\Liugengxin\Desktop\亚洲杯.xlsx'
data=xlrd_read_data(path)
data_stand=standardScaler(data) #获得标准化数据
label_pred=kmeans(data_stand)
# =============================================================================
# team=[['阿联酋'],['印度'],['泰国'],['巴林'],
#       ['约旦'],['澳大利亚'],['叙利亚'],['巴勒斯坦'],
#       ['中国'],['韩国'],['吉尔吉斯斯坦'],['菲律宾'],
#       ['伊朗'],['伊拉克'],['越南'],['也门'],
#       ['沙特'],['卡塔尔'],['朝鲜'],['黎巴嫩'],
#       ['日本'],['乌兹别克斯坦'],['土库曼斯坦'],['阿曼']]
# =============================================================================
team=[['阿联酋'],['印度'],['泰国'],['巴林'],
      ['约旦'],['澳大利亚'],['叙利亚'],['巴勒斯坦'],
      ['中国'],['韩国'],['吉尔吉斯斯坦'],['菲律宾'],
      ['伊朗'],['伊拉克'],['越南'],['也门']]
clustering_predict = np.column_stack((team,label_pred))#合并
first = clustering_predict[12][1]#一流
third = clustering_predict[15][1]#三流

for i in range(len(team)):
        if clustering_predict[i][1]==first:clustering_predict[i][1]='亚洲一流'
        elif clustering_predict[i][1]==third:clustering_predict[i][1]='亚洲三流'
        else :clustering_predict[i][1]='亚洲二流'

 

posted @ 2019-01-17 20:21  GXLiu  阅读(288)  评论(0编辑  收藏  举报