We encounter a problem to convert the data into four groups to calculate evenness. A flash idea attacked me that clustering analysis may be suited for this work. This course 'Artificial Intelligence' once told me the method 'Clustering Analysis' last year. Result proved my mind and this method finished all work instantaneously.

#!/usr/bin/env python
# coding: utf-8
# ClassifyData
# Convert the data into several groups
import pandas as pd
from sklearn.cluster import KMeans
print('***************Classify Data***************')
try:
    while True:
        print('Please input your input file .xlsx')
        inputfile = input()
        print('Please input your output file .xlsx')
        outputfile = input()
        print('Please input the number of data groups')
        k = int(input())
        iteration = 500
        data = pd.read_excel(inputfile,index_col='id')
        data_zs = 1.0 * (data - data.mean())/data.std()
        model = KMeans(n_clusters=k,n_jobs=4,max_iter=iteration)
        model.fit(data_zs)
        r = pd.concat([data,pd.Series(model.labels_,index=data.index)],axis=1)
        r.columns=list(data.columns)+[u'categories']
        r.to_excel(outputfile)
        print('***************Finished!!!***************') 
        print('If no file is needed to classify, please input \'exit\'. Otherwise, Please input arbitrary character.')
        exit = input()
        if exit == 'exit':
            break
except:
    print('***************Error!!!***************')
    print('Your file can\'t be found!, Please try again!')
Last modified: 2020年3月30日

Comments

Devin Hu 

It looks great.

Devin Hu进行回复 取消回复

Your email address will not be published.