Exemple #1
0
def main(input_f):
    (countries, vectors) = k_means.read_file(input_f)

    clusters = utils.hcluster(vectors, distance=distance_function)
    utils.drawdendrogram(clusters,
                         list(map(lambda x: x[1], countries)),
                         jpeg='data/hierarchical.jpg')

    # Self-picked clusters from the graph which I considered good
    good_clusters = [
        clusters.left, clusters.right.left, clusters.right.right.left.left,
        clusters.right.right.left.right.left,
        clusters.right.right.left.right.right,
        clusters.right.right.right.right, clusters.right.right.right.left
    ]

    cluster_level = []

    for cluster in good_clusters:
        country_ids = []
        get_all(cluster, country_ids)
        cluster_level.append(country_ids)

    for i in range(num_clusters):
        print('cluster {}:'.format(i + 1))
        print([countries[r] for r in cluster_level[i]])

    print("SSE: " + str(k_means.sse(cluster_level, vectors)))
Exemple #2
0
def analysis(request):
	if request.method == 'POST':
		num_centroids = int(request.POST.get('num_centroids'))
		num_points = int(request.POST.get('num_points'))

		pc = MongoDB.list_all_postal_codes()

		list_analysis = []
		i = 0
		while i < num_points:
			r = random.randint(0, (len(pc)-1))
			list_analysis.append(pc.pop(r))
			i += 1

		points, centroids = k_means.k_means_lists(num_centroids, 20, list_analysis)
		dic = k_means.sse(points, centroids)
		regions, table = viewLists.listTableMoreInputs()

		t = get_template('analysis.html')
		html = t.render(
			Context(
				{
				'regions' : regions,
				'table': table,
				'center' : "-23.2062436,-45.900007" ,
				'localizations' : points,
				'colors' : ColorsRandom.generate_colors(num_centroids),
				'centroids' : centroids,
				'sse' : dic,
				'hospitals' : MongoDB.list_all_healths(),
				'csrf_token' : csrf(request)['csrf_token']
				}
			)
		)
		html = html.replace("&#39;", "'")
		return HttpResponse(html)

	# return render_to_response(
	# 	'index.html',
	# 	context_instance=RequestContext(request))
	regions, table = viewLists.listTableMoreInputs()
	t = get_template('index.html')
	html = t.render(
		Context(
			{
			'regions' : regions,
			'table': table,
			'csrf_token' : csrf(request)['csrf_token']
			}
		)
	)
	html = html.replace("&#39;", "'")
	return HttpResponse(html)
def main(input_f):
    (countries, vectors) = k_means.read_file(input_f)

    clusters = kcluster_bisect([list(range(len(vectors)))], vectors, distance=distance_function, k=num_clusters)
    proper_clusters = []  # Nonempty clusters
    for i in range(num_clusters):
        if len(clusters[i]) == 0:
            continue

        proper_clusters.append(clusters[i])
        print('cluster {}:'.format(i + 1))
        print([countries[r] for r in clusters[i]])

    print("SSE: " + str(k_means.sse(proper_clusters, vectors)))
Exemple #4
0
def main(input_f):
    (countries, vectors) = k_means.read_file(input_f)
    k_arr = []
    sse_arr = []

    for num in range_clusters:
        clusters = utils.kcluster(vectors,
                                  distance=k_means.distance_function,
                                  k=num)
        proper_clusters = []

        print(str(round((float(num) / 50) * 100)) + "%")
        for i in range(num):
            if len(clusters[i]) != 0:
                proper_clusters.append(clusters[i])

        k_arr.append(num)
        sse_arr.append(k_means.sse(proper_clusters, vectors))

    plt.plot(k_arr, sse_arr)
    plt.ylabel("SSE")
    plt.xlabel("k")
    plt.xticks(np.arange(0, 50, 2))
    plt.show()
Exemple #5
0
def locations(request):
	regions, table = viewLists.listTableMoreInputs()

	if request.method == 'POST':
		# center's maps
		num_clusters = int(request.POST.get('num_clusters'))

		colors = ColorsRandom.generate_colors(num_clusters)

		country = request.POST.get('country')
		state = request.POST.get('state')
		city = request.POST.get('city')
		center = Geocode.geocode([country + ' ' + state + ' ' + city, city], isCenter=True)
		center = str(center['lat']) + "," + str(center['lng'])
		
		# ceps's locations
		ceps_list = [ 
			[country + ' ' + 
			state + ' ' +
			city + ' ' +  
			i, i] for i in request.POST.get('ceps').split(';')]

		# items = Geocode.parallelGeocode(ceps_list)
		items = []
		for i in ceps_list:
			items.append(Geocode.geocode(i))

		points, centroids = k_means.k_means_lists(num_clusters, 20, items)

		dic = k_means.sse(points, centroids)

		t = get_template('maps.html')
		html = t.render(
			Context(
				{
				'regions' : regions,
				'table': table,
				'center' : center ,
				'localizations' : points,
				'colors' : colors,
				'centroids' : centroids,
				'sse' : dic,
				'hospitals' : MongoDB.list_all_healths(),
				'csrf_token' : csrf(request)['csrf_token']
				}
			)
		)
		html = html.replace("&#39;", "'")

		return HttpResponse(html)

	# return render_to_response(
	# 	'index.html',
	# 	context_instance=RequestContext(request))

	t = get_template('index.html')
	html = t.render(
		Context(
			{
			'regions' : regions,
			'table': table,
			'csrf_token' : csrf(request)['csrf_token']
			}
		)
	)
	html = html.replace("&#39;", "'")
	return HttpResponse(html)