PYTHON——自然间断点分级法

PYTHON——自然间断点分级法

import xlrd
import xlwt
import jenkspy
#输入文件
Table= xlrd.open_workbook("D:\\dxkj\\test\\因子.xlsx")
#打印Table相关信息
print(Table.sheet_names())
sheet1 = Table.sheets()[0]
print(sheet1.name,sheet1.nrows, sheet1.ncols)
print(sheet1.col_values(1))
f = xlwt.Workbook()
#遍历Table的每个sheet
for k in range(0,len(Table.sheets())):
    sheetSave = f.add_sheet(Table.sheets()[k].name,cell_overwrite_ok = True)
    sheet = Table.sheets()[k]
    #遍历sheet的列数据 从第二列开始
    for j in range(1,sheet.ncols):
        col = sheet.col_values(j)
        List = col[1:len(col)]
        newList=col[1:len(col)]
        newList.sort()
        nn=int(len(col)*0.025)
        del newList[0:nn]
        del newList[0-nn:]
        ListID = col[1:len(col)]#对类别进行标识
        # print(ListID)
        #计算间断点
        breaks = jenkspy.jenks_breaks(newList,nb_class = 5)
        #根据计算的间断点,判断每个原始数据属于哪个类别
        print(breaks)
        for i in range(len(List)):
            if List[i] >5:
                print(List[i] )
            if List[i] < breaks[1]:
                ListID[i] = 1
            elif List[i] < breaks[2] and List[i] >= breaks[1]:
                ListID[i] = 2
            elif List[i] < breaks[3] and List[i] >= breaks[2]:
                ListID[i] = 3
            elif List[i] < breaks[4] and List[i] >= breaks[3]:
                ListID[i] = 4
            elif List[i] >= breaks[4]:
                ListID[i] = 5
            else:
                ListID[i] = 5
            for u in range(0,len(ListID)):
            # print(u,j)
            sheetSave.write(u,j,ListID[u])#写数据
        print(j,'------')
    print(k,'======')
#保存数据成excel表
f.save(r'D:\demo.xls')



你可能感兴趣的:(PYTHON,自然间断点分级法,数据分析)