Пример #1
0
def kmeans(request):
    k = 2
    global norm, champs, name_champ
    norm = Normalizer()
    workpath = os.path.dirname(os.path.abspath(__file__)) #Returns the Path your .py file is in
    datafile = os.path.join(workpath, 'dataset/spambase.data.txt')
    norm.load_csv(os.path.join(workpath, 'dataset/spambase.data.txt'))
    champs = []
    name_champ = []
    if request.method == 'GET':
        if(request.GET['nb'] == '3'):
            champs.append(int(request.GET['champs1']))
            champs.append(int(request.GET['champs2']))
            champs.append(int(request.GET['champs3']))
            name_champ.append(nomChamp[champs[0]])
            name_champ.append(nomChamp[champs[1]])
            name_champ.append(nomChamp[champs[2]])
        elif (request.GET['nb'] == '2'):
            champs.append(int(request.GET['champs1']))
            champs.append(int(request.GET['champs2']))
            name_champ.append(nomChamp[champs[0]])
            name_champ.append(nomChamp[champs[1]])

        global kMeanClusterer
        kMeanClusterer = KMeanClusterer(k, datafile, champs)
        kMeanClusterer.assignement()
        centroids = []
        clusters = []
        for i in range(k):
            centroids.append(kMeanClusterer.getCluster(i).getCentroid())
            #centroids.append(kMeanClusterer.getCluster(i).normalizeCentroid(0.0, 1.0, len(champs)))
        for i in range(k):
            clusters.append(kMeanClusterer.getCluster(i).getPoints())
            #clusters.append(norm.normalization(kMeanClusterer.getCluster(i).getPoints(), 0.0, 1.0, len(champs)))


        splitedData = norm.get_splitedData(champs)
        spams = splitedData[0]
        nospams = splitedData[1]

        html = render_to_string('kmeans.html', {'k': len(champs), 'centroids': centroids, 'clusters': clusters,
                                                'spams': spams, 'no_spams': nospams, 'nomChamps': name_champ})
        return HttpResponse(html)
    else:
        form = DocumentForm() # A empty, unbound form
        return redirect('index.html', {'form': form})