Add Topological Sort (#1302)

* add topological sort

* fix topological sort?

* running black

* renaming file
This commit is contained in:
Phyllipe Bezerra
2019-10-18 03:13:58 -03:00
committed by Christian Clauss
parent ddb094919b
commit 455509acee
8 changed files with 84 additions and 27 deletions

View File

@@ -5,12 +5,13 @@ from sklearn.model_selection import train_test_split
data = datasets.load_iris()
X = np.array(data['data'])
y = np.array(data['target'])
classes = data['target_names']
X = np.array(data["data"])
y = np.array(data["target"])
classes = data["target_names"]
X_train, X_test, y_train, y_test = train_test_split(X, y)
def euclidean_distance(a, b):
"""
Gives the euclidean distance between two points
@@ -21,6 +22,7 @@ def euclidean_distance(a, b):
"""
return np.linalg.norm(np.array(a) - np.array(b))
def classifier(train_data, train_target, classes, point, k=5):
"""
Classifies the point using the KNN algorithm
@@ -43,13 +45,13 @@ def classifier(train_data, train_target, classes, point, k=5):
for data_point in data:
distance = euclidean_distance(data_point[0], point)
distances.append((distance, data_point[1]))
# Choosing 'k' points with the least distances.
# Choosing 'k' points with the least distances.
votes = [i[1] for i in sorted(distances)[:k]]
# Most commonly occuring class among them
# Most commonly occuring class among them
# is the class into which the point is classified
result = Counter(votes).most_common(1)[0][0]
return classes[result]
if __name__ == "__main__":
print(classifier(X_train, y_train, classes, [4.4, 3.1, 1.3, 1.4]))
print(classifier(X_train, y_train, classes, [4.4, 3.1, 1.3, 1.4]))