Skip to content

Instantly share code, notes, and snippets.

@azizkayumov
Created November 21, 2019 08:20
Show Gist options
  • Save azizkayumov/c0131396aa6793a49ed16047ea7a357c to your computer and use it in GitHub Desktop.
Save azizkayumov/c0131396aa6793a49ed16047ea7a357c to your computer and use it in GitHub Desktop.
import numpy as np
import random
import matplotlib.pyplot as plt
def clustering(x1, x2):
# randomly initialize centroids
c1 = [random.randint(10, 40), random.randint(10, 40)]
c2 = [random.randint(50, 100), random.randint(50, 100)]
m = len(x1)
labels = [""] * m
for t in range(20000):
prev_labels = labels
for i in range(m):
x = [x1[i], x2[i]]
# calculate the distance between the current x and centroids
distance_to_c1 = np.sqrt(np.square(c1[0] - x[0]) + np.square(c1[1] - x[1]))
distance_to_c2 = np.sqrt(np.square(c2[0] - x[0]) + np.square(c2[1] - x[1]))
if distance_to_c1 > distance_to_c2:
labels[i] = "dangerous"
else:
labels[i] = "safe"
# move the centroids
sum1 = [0, 0]
sum2 = [0, 0]
for i in range(m):
x = [x1[i], x2[i]]
if labels[i] == 0:
sum1[0] += x[0]
sum1[1] += x[1]
else:
sum2[0] += x[0]
sum2[1] += x[1]
c1[0] = sum1[0] / len(labels)
c1[1] = sum1[1] / len(labels)
c2[0] = sum2[0] / len(labels)
c2[1] = sum2[1] / len(labels)
# check if labels converged
changed = False
for i in range(len(labels)):
if labels[i] != prev_labels[i]:
changed = True
if not changed:
break
return labels
# slope degrees: for ex. 27°
slopes = [77, 33, 75, 9, 72, 34, 73, 21, 99, 14,
92, 20, 55, 19, 75, 30, 70, 0, 93, 37,
98, 39, 80, 32, 78, 39, 89, 37, 55, 8,
65, 15, 63, 7, 57, 10, 82, 12, 70, 39]
# average precipitation: for ex. 60%
precis = [81, 32, 56, 45, 91, 37, 93, 22, 82, 3,
70, 26, 57, 1, 59, 22, 67, 25, 97, 30,
70, 8, 58, 13, 91, 34, 81, 34, 83, 28,
91, 3, 82, 35, 72, 35, 92, 3, 87, 6]
labels = clustering(slopes, precis)
safe_slopes = [[], []]
dangerous_slopes = [[], []]
for i in range(len(labels)):
if labels[i] == "safe":
safe_slopes[0].append(slopes[i])
safe_slopes[1].append(precis[i])
else:
dangerous_slopes[0].append(slopes[i])
dangerous_slopes[1].append(precis[i])
scatter_c1 = plt.scatter(safe_slopes[0], safe_slopes[1], c='green', marker='o')
scatter_c2 = plt.scatter(dangerous_slopes[0], dangerous_slopes[1], c='red', marker='x')
plt.legend((scatter_c1, scatter_c2), ("Safe", "Dangerous"),
scatterpoints=1,
loc='lower right', ncol=3,
fontsize=10)
plt.title("Clustering cut-slopes")
plt.ylabel("Slope degrees")
plt.xlabel("Average precipitation (yearly)")
plt.grid()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment