From 08da36c175eee583dfa8fb8659c33f53dd98f477 Mon Sep 17 00:00:00 2001 From: BipanjitGill <120119876+BipanjitGill@users.noreply.github.com> Date: Wed, 25 Oct 2023 13:35:12 +0530 Subject: [PATCH 1/5] Add files via upload --- ML_Algorithms/K-Means/kMeans.py | 48 +++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 ML_Algorithms/K-Means/kMeans.py diff --git a/ML_Algorithms/K-Means/kMeans.py b/ML_Algorithms/K-Means/kMeans.py new file mode 100644 index 0000000..f87dc29 --- /dev/null +++ b/ML_Algorithms/K-Means/kMeans.py @@ -0,0 +1,48 @@ +import numpy as np + +class K_Means: + def __init__(self, k=2, max_iter=100, tol=0.001): + self.k = k + self.max_iter = max_iter + self.tol = tol + + + def fit(self,data): + + self.centroids = {} + + for i in range(self.k): + self.centroids[i] = data[i] + + for i in range(self.max_iter): + self.classifications = {} + + for i in range(self.k): + self.classifications[i] = [] + + for featureset in data: + distances = [np.linalg.norm(featureset-self.centroids[centroid]) for centroid in self.centroids] + classification = distances.index(min(distances)) + self.classifications[classification].append(featureset) + + prev_centroids = dict(self.centroids) + + for classification in self.classifications: + self.centroids[classification] = np.average(self.classifications[classification],axis=0) + + optimized = True + + for c in self.centroids: + original_centroid = prev_centroids[c] + current_centroid = self.centroids[c] + if np.sum((current_centroid-original_centroid)/original_centroid*100.0) > self.tol: + # print(np.sum((current_centroid-original_centroid)/original_centroid*100.0)) + optimized = False + + if optimized: + break + + def predict(self,data): + distances = [np.linalg.norm(data-self.centroids[centroid]) for centroid in self.centroids] + classification = distances.index(min(distances)) + return classification \ No newline at end of file From 2aa13a7ce2d94faa3448148dd15fe09b5e0f1d16 Mon Sep 17 00:00:00 2001 From: BipanjitGill <120119876+BipanjitGill@users.noreply.github.com> Date: Wed, 25 Oct 2023 15:16:04 +0530 Subject: [PATCH 2/5] create knn.py --- ML_Algorithms/KNN/iris_test.csv | 31 ++++++++ ML_Algorithms/KNN/iris_train.csv | 121 +++++++++++++++++++++++++++++++ ML_Algorithms/KNN/knn.py | 32 ++++++++ 3 files changed, 184 insertions(+) create mode 100644 ML_Algorithms/KNN/iris_test.csv create mode 100644 ML_Algorithms/KNN/iris_train.csv create mode 100644 ML_Algorithms/KNN/knn.py diff --git a/ML_Algorithms/KNN/iris_test.csv b/ML_Algorithms/KNN/iris_test.csv new file mode 100644 index 0000000..c0337a2 --- /dev/null +++ b/ML_Algorithms/KNN/iris_test.csv @@ -0,0 +1,31 @@ +SepalLengthCm,SepalWidthCm,PetalLengthCm,PetalWidthCm,Species +6.1,2.8,4.7,1.2,Iris-versicolor +5.7,3.8,1.7,0.3,Iris-setosa +7.7,2.6,6.9,2.3,Iris-virginica +6,2.9,4.5,1.5,Iris-versicolor +6.8,2.8,4.8,1.4,Iris-versicolor +5.4,3.4,1.5,0.4,Iris-setosa +5.6,2.9,3.6,1.3,Iris-versicolor +6.9,3.1,5.1,2.3,Iris-virginica +6.2,2.2,4.5,1.5,Iris-versicolor +5.8,2.7,3.9,1.2,Iris-versicolor +6.5,3.2,5.1,2,Iris-virginica +4.8,3,1.4,0.1,Iris-setosa +5.5,3.5,1.3,0.2,Iris-setosa +4.9,3.1,1.5,0.1,Iris-setosa +5.1,3.8,1.5,0.3,Iris-setosa +6.3,3.3,4.7,1.6,Iris-versicolor +6.5,3,5.8,2.2,Iris-virginica +5.6,2.5,3.9,1.1,Iris-versicolor +5.7,2.8,4.5,1.3,Iris-versicolor +6.4,2.8,5.6,2.2,Iris-virginica +4.7,3.2,1.6,0.2,Iris-setosa +6.1,3,4.9,1.8,Iris-virginica +5,3.4,1.6,0.4,Iris-setosa +6.4,2.8,5.6,2.1,Iris-virginica +7.9,3.8,6.4,2,Iris-virginica +6.7,3,5.2,2.3,Iris-virginica +6.7,2.5,5.8,1.8,Iris-virginica +6.8,3.2,5.9,2.3,Iris-virginica +4.8,3,1.4,0.3,Iris-setosa +4.8,3.1,1.6,0.2,Iris-setosa diff --git a/ML_Algorithms/KNN/iris_train.csv b/ML_Algorithms/KNN/iris_train.csv new file mode 100644 index 0000000..7a603e6 --- /dev/null +++ b/ML_Algorithms/KNN/iris_train.csv @@ -0,0 +1,121 @@ +SepalLengthCm,SepalWidthCm,PetalLengthCm,PetalWidthCm,Species +4.6,3.6,1,0.2,Iris-setosa +5.7,4.4,1.5,0.4,Iris-setosa +6.7,3.1,4.4,1.4,Iris-versicolor +4.8,3.4,1.6,0.2,Iris-setosa +4.4,3.2,1.3,0.2,Iris-setosa +6.3,2.5,5,1.9,Iris-virginica +6.4,3.2,4.5,1.5,Iris-versicolor +5.2,3.5,1.5,0.2,Iris-setosa +5,3.6,1.4,0.2,Iris-setosa +5.2,4.1,1.5,0.1,Iris-setosa +5.8,2.7,5.1,1.9,Iris-virginica +6,3.4,4.5,1.6,Iris-versicolor +6.7,3.1,4.7,1.5,Iris-versicolor +5.4,3.9,1.3,0.4,Iris-setosa +5.4,3.7,1.5,0.2,Iris-setosa +5.5,2.4,3.7,1,Iris-versicolor +6.3,2.8,5.1,1.5,Iris-virginica +6.4,3.1,5.5,1.8,Iris-virginica +6.6,3,4.4,1.4,Iris-versicolor +7.2,3.6,6.1,2.5,Iris-virginica +5.7,2.9,4.2,1.3,Iris-versicolor +7.6,3,6.6,2.1,Iris-virginica +5.6,3,4.5,1.5,Iris-versicolor +5.1,3.5,1.4,0.2,Iris-setosa +7.7,2.8,6.7,2,Iris-virginica +5.8,2.7,4.1,1,Iris-versicolor +5.2,3.4,1.4,0.2,Iris-setosa +5,3.5,1.3,0.3,Iris-setosa +5.1,3.8,1.9,0.4,Iris-setosa +5,2,3.5,1,Iris-versicolor +6.3,2.7,4.9,1.8,Iris-virginica +4.8,3.4,1.9,0.2,Iris-setosa +5,3,1.6,0.2,Iris-setosa +5.1,3.3,1.7,0.5,Iris-setosa +5.6,2.7,4.2,1.3,Iris-versicolor +5.1,3.4,1.5,0.2,Iris-setosa +5.7,3,4.2,1.2,Iris-versicolor +7.7,3.8,6.7,2.2,Iris-virginica +4.6,3.2,1.4,0.2,Iris-setosa +6.2,2.9,4.3,1.3,Iris-versicolor +5.7,2.5,5,2,Iris-virginica +5.5,4.2,1.4,0.2,Iris-setosa +6,3,4.8,1.8,Iris-virginica +5.8,2.7,5.1,1.9,Iris-virginica +6,2.2,4,1,Iris-versicolor +5.4,3,4.5,1.5,Iris-versicolor +6.2,3.4,5.4,2.3,Iris-virginica +5.5,2.3,4,1.3,Iris-versicolor +5.4,3.9,1.7,0.4,Iris-setosa +5,2.3,3.3,1,Iris-versicolor +6.4,2.7,5.3,1.9,Iris-virginica +5,3.3,1.4,0.2,Iris-setosa +5,3.2,1.2,0.2,Iris-setosa +5.5,2.4,3.8,1.1,Iris-versicolor +6.7,3,5,1.7,Iris-versicolor +4.9,3.1,1.5,0.1,Iris-setosa +5.8,2.8,5.1,2.4,Iris-virginica +5,3.4,1.5,0.2,Iris-setosa +5,3.5,1.6,0.6,Iris-setosa +5.9,3.2,4.8,1.8,Iris-versicolor +5.1,2.5,3,1.1,Iris-versicolor +6.9,3.2,5.7,2.3,Iris-virginica +6,2.7,5.1,1.6,Iris-versicolor +6.1,2.6,5.6,1.4,Iris-virginica +7.7,3,6.1,2.3,Iris-virginica +5.5,2.5,4,1.3,Iris-versicolor +4.4,2.9,1.4,0.2,Iris-setosa +4.3,3,1.1,0.1,Iris-setosa +6,2.2,5,1.5,Iris-virginica +7.2,3.2,6,1.8,Iris-virginica +4.6,3.1,1.5,0.2,Iris-setosa +5.1,3.5,1.4,0.3,Iris-setosa +4.4,3,1.3,0.2,Iris-setosa +6.3,2.5,4.9,1.5,Iris-versicolor +6.3,3.4,5.6,2.4,Iris-virginica +4.6,3.4,1.4,0.3,Iris-setosa +6.8,3,5.5,2.1,Iris-virginica +6.3,3.3,6,2.5,Iris-virginica +4.7,3.2,1.3,0.2,Iris-setosa +6.1,2.9,4.7,1.4,Iris-versicolor +6.5,2.8,4.6,1.5,Iris-versicolor +6.2,2.8,4.8,1.8,Iris-virginica +7,3.2,4.7,1.4,Iris-versicolor +6.4,3.2,5.3,2.3,Iris-virginica +5.1,3.8,1.6,0.2,Iris-setosa +6.9,3.1,5.4,2.1,Iris-virginica +5.9,3,4.2,1.5,Iris-versicolor +6.5,3,5.2,2,Iris-virginica +5.7,2.6,3.5,1,Iris-versicolor +5.2,2.7,3.9,1.4,Iris-versicolor +6.1,3,4.6,1.4,Iris-versicolor +4.5,2.3,1.3,0.3,Iris-setosa +6.6,2.9,4.6,1.3,Iris-versicolor +5.5,2.6,4.4,1.2,Iris-versicolor +5.3,3.7,1.5,0.2,Iris-setosa +5.6,3,4.1,1.3,Iris-versicolor +7.3,2.9,6.3,1.8,Iris-virginica +6.7,3.3,5.7,2.1,Iris-virginica +5.1,3.7,1.5,0.4,Iris-setosa +4.9,2.4,3.3,1,Iris-versicolor +6.7,3.3,5.7,2.5,Iris-virginica +7.2,3,5.8,1.6,Iris-virginica +4.9,3.1,1.5,0.1,Iris-setosa +6.7,3.1,5.6,2.4,Iris-virginica +4.9,3,1.4,0.2,Iris-setosa +6.9,3.1,4.9,1.5,Iris-versicolor +7.4,2.8,6.1,1.9,Iris-virginica +6.3,2.9,5.6,1.8,Iris-virginica +5.7,2.8,4.1,1.3,Iris-versicolor +6.5,3,5.5,1.8,Iris-virginica +6.3,2.3,4.4,1.3,Iris-versicolor +6.4,2.9,4.3,1.3,Iris-versicolor +5.6,2.8,4.9,2,Iris-virginica +5.9,3,5.1,1.8,Iris-virginica +5.4,3.4,1.7,0.2,Iris-setosa +6.1,2.8,4,1.3,Iris-versicolor +4.9,2.5,4.5,1.7,Iris-virginica +5.8,4,1.2,0.2,Iris-setosa +5.8,2.6,4,1.2,Iris-versicolor +7.1,3,5.9,2.1,Iris-virginica diff --git a/ML_Algorithms/KNN/knn.py b/ML_Algorithms/KNN/knn.py new file mode 100644 index 0000000..cdda149 --- /dev/null +++ b/ML_Algorithms/KNN/knn.py @@ -0,0 +1,32 @@ +import numpy as np +import pandas as pd + +def euclidean_distance(x1,x2): + distance=0 + for i in range(len(x1)): + distance+=np.square(x1[i]-x2[i]) + return distance + +def knn(training_x,training_y,testing_x): + predictions=[] + for i in range(len(testing_x)): + distance=[] + for j in range(len(training_x)): + distance.append(euclidean_distance(testing_x[i],training_x[j])) + mydict={"class":training_y,"Distances":distance} + distance_df=pd.DataFrame.from_dict(mydict) + sorted_distance = distance_df.sort_values(by=["Distances"]) + predict_labels=sorted_distance["class"][:5] + predicted_label=predict_labels.value_counts().index.tolist()[0] + predictions.append(predicted_label) + return predictions + +train_df=pd.read_csv("Code-Forge\ML_Algorithms\KNN\iris_train.csv") +train_x=train_df.drop("Species",axis=1).to_numpy() +train_y=train_df["Species"].to_numpy() + +test_df=pd.read_csv("Code-Forge\ML_Algorithms\KNN\iris_test.csv") +test_x=test_df.drop("Species",axis=1).to_numpy() +test_y=test_df["Species"].to_numpy() + +predictions=knn(train_x,train_y,test_x) From 0c33718ca770092987667924f9a23aeb8a1c2877 Mon Sep 17 00:00:00 2001 From: BipanjitGill <120119876+BipanjitGill@users.noreply.github.com> Date: Wed, 25 Oct 2023 15:20:16 +0530 Subject: [PATCH 3/5] Add files via upload From ddf432ce17089778a653e923c3c90fc3fbb6aa83 Mon Sep 17 00:00:00 2001 From: BipanjitGill <120119876+BipanjitGill@users.noreply.github.com> Date: Fri, 27 Oct 2023 19:03:20 +0530 Subject: [PATCH 4/5] Delete ML_Algorithms/KNN directory --- ML_Algorithms/KNN/iris_test.csv | 31 -------- ML_Algorithms/KNN/iris_train.csv | 121 ------------------------------- ML_Algorithms/KNN/knn.py | 32 -------- 3 files changed, 184 deletions(-) delete mode 100644 ML_Algorithms/KNN/iris_test.csv delete mode 100644 ML_Algorithms/KNN/iris_train.csv delete mode 100644 ML_Algorithms/KNN/knn.py diff --git a/ML_Algorithms/KNN/iris_test.csv b/ML_Algorithms/KNN/iris_test.csv deleted file mode 100644 index c0337a2..0000000 --- a/ML_Algorithms/KNN/iris_test.csv +++ /dev/null @@ -1,31 +0,0 @@ -SepalLengthCm,SepalWidthCm,PetalLengthCm,PetalWidthCm,Species -6.1,2.8,4.7,1.2,Iris-versicolor -5.7,3.8,1.7,0.3,Iris-setosa -7.7,2.6,6.9,2.3,Iris-virginica -6,2.9,4.5,1.5,Iris-versicolor -6.8,2.8,4.8,1.4,Iris-versicolor -5.4,3.4,1.5,0.4,Iris-setosa -5.6,2.9,3.6,1.3,Iris-versicolor -6.9,3.1,5.1,2.3,Iris-virginica -6.2,2.2,4.5,1.5,Iris-versicolor -5.8,2.7,3.9,1.2,Iris-versicolor -6.5,3.2,5.1,2,Iris-virginica -4.8,3,1.4,0.1,Iris-setosa -5.5,3.5,1.3,0.2,Iris-setosa -4.9,3.1,1.5,0.1,Iris-setosa -5.1,3.8,1.5,0.3,Iris-setosa -6.3,3.3,4.7,1.6,Iris-versicolor -6.5,3,5.8,2.2,Iris-virginica -5.6,2.5,3.9,1.1,Iris-versicolor -5.7,2.8,4.5,1.3,Iris-versicolor -6.4,2.8,5.6,2.2,Iris-virginica -4.7,3.2,1.6,0.2,Iris-setosa -6.1,3,4.9,1.8,Iris-virginica -5,3.4,1.6,0.4,Iris-setosa -6.4,2.8,5.6,2.1,Iris-virginica -7.9,3.8,6.4,2,Iris-virginica -6.7,3,5.2,2.3,Iris-virginica -6.7,2.5,5.8,1.8,Iris-virginica -6.8,3.2,5.9,2.3,Iris-virginica -4.8,3,1.4,0.3,Iris-setosa -4.8,3.1,1.6,0.2,Iris-setosa diff --git a/ML_Algorithms/KNN/iris_train.csv b/ML_Algorithms/KNN/iris_train.csv deleted file mode 100644 index 7a603e6..0000000 --- a/ML_Algorithms/KNN/iris_train.csv +++ /dev/null @@ -1,121 +0,0 @@ -SepalLengthCm,SepalWidthCm,PetalLengthCm,PetalWidthCm,Species -4.6,3.6,1,0.2,Iris-setosa -5.7,4.4,1.5,0.4,Iris-setosa -6.7,3.1,4.4,1.4,Iris-versicolor -4.8,3.4,1.6,0.2,Iris-setosa -4.4,3.2,1.3,0.2,Iris-setosa -6.3,2.5,5,1.9,Iris-virginica -6.4,3.2,4.5,1.5,Iris-versicolor -5.2,3.5,1.5,0.2,Iris-setosa -5,3.6,1.4,0.2,Iris-setosa -5.2,4.1,1.5,0.1,Iris-setosa -5.8,2.7,5.1,1.9,Iris-virginica -6,3.4,4.5,1.6,Iris-versicolor -6.7,3.1,4.7,1.5,Iris-versicolor -5.4,3.9,1.3,0.4,Iris-setosa -5.4,3.7,1.5,0.2,Iris-setosa -5.5,2.4,3.7,1,Iris-versicolor -6.3,2.8,5.1,1.5,Iris-virginica -6.4,3.1,5.5,1.8,Iris-virginica -6.6,3,4.4,1.4,Iris-versicolor -7.2,3.6,6.1,2.5,Iris-virginica -5.7,2.9,4.2,1.3,Iris-versicolor -7.6,3,6.6,2.1,Iris-virginica -5.6,3,4.5,1.5,Iris-versicolor -5.1,3.5,1.4,0.2,Iris-setosa -7.7,2.8,6.7,2,Iris-virginica -5.8,2.7,4.1,1,Iris-versicolor -5.2,3.4,1.4,0.2,Iris-setosa -5,3.5,1.3,0.3,Iris-setosa -5.1,3.8,1.9,0.4,Iris-setosa -5,2,3.5,1,Iris-versicolor -6.3,2.7,4.9,1.8,Iris-virginica -4.8,3.4,1.9,0.2,Iris-setosa -5,3,1.6,0.2,Iris-setosa -5.1,3.3,1.7,0.5,Iris-setosa -5.6,2.7,4.2,1.3,Iris-versicolor -5.1,3.4,1.5,0.2,Iris-setosa -5.7,3,4.2,1.2,Iris-versicolor -7.7,3.8,6.7,2.2,Iris-virginica -4.6,3.2,1.4,0.2,Iris-setosa -6.2,2.9,4.3,1.3,Iris-versicolor -5.7,2.5,5,2,Iris-virginica -5.5,4.2,1.4,0.2,Iris-setosa -6,3,4.8,1.8,Iris-virginica -5.8,2.7,5.1,1.9,Iris-virginica -6,2.2,4,1,Iris-versicolor -5.4,3,4.5,1.5,Iris-versicolor -6.2,3.4,5.4,2.3,Iris-virginica -5.5,2.3,4,1.3,Iris-versicolor -5.4,3.9,1.7,0.4,Iris-setosa -5,2.3,3.3,1,Iris-versicolor -6.4,2.7,5.3,1.9,Iris-virginica -5,3.3,1.4,0.2,Iris-setosa -5,3.2,1.2,0.2,Iris-setosa -5.5,2.4,3.8,1.1,Iris-versicolor -6.7,3,5,1.7,Iris-versicolor -4.9,3.1,1.5,0.1,Iris-setosa -5.8,2.8,5.1,2.4,Iris-virginica -5,3.4,1.5,0.2,Iris-setosa -5,3.5,1.6,0.6,Iris-setosa -5.9,3.2,4.8,1.8,Iris-versicolor -5.1,2.5,3,1.1,Iris-versicolor -6.9,3.2,5.7,2.3,Iris-virginica -6,2.7,5.1,1.6,Iris-versicolor -6.1,2.6,5.6,1.4,Iris-virginica -7.7,3,6.1,2.3,Iris-virginica -5.5,2.5,4,1.3,Iris-versicolor -4.4,2.9,1.4,0.2,Iris-setosa -4.3,3,1.1,0.1,Iris-setosa -6,2.2,5,1.5,Iris-virginica -7.2,3.2,6,1.8,Iris-virginica -4.6,3.1,1.5,0.2,Iris-setosa -5.1,3.5,1.4,0.3,Iris-setosa -4.4,3,1.3,0.2,Iris-setosa -6.3,2.5,4.9,1.5,Iris-versicolor -6.3,3.4,5.6,2.4,Iris-virginica -4.6,3.4,1.4,0.3,Iris-setosa -6.8,3,5.5,2.1,Iris-virginica -6.3,3.3,6,2.5,Iris-virginica -4.7,3.2,1.3,0.2,Iris-setosa -6.1,2.9,4.7,1.4,Iris-versicolor -6.5,2.8,4.6,1.5,Iris-versicolor -6.2,2.8,4.8,1.8,Iris-virginica -7,3.2,4.7,1.4,Iris-versicolor -6.4,3.2,5.3,2.3,Iris-virginica -5.1,3.8,1.6,0.2,Iris-setosa -6.9,3.1,5.4,2.1,Iris-virginica -5.9,3,4.2,1.5,Iris-versicolor -6.5,3,5.2,2,Iris-virginica -5.7,2.6,3.5,1,Iris-versicolor -5.2,2.7,3.9,1.4,Iris-versicolor -6.1,3,4.6,1.4,Iris-versicolor -4.5,2.3,1.3,0.3,Iris-setosa -6.6,2.9,4.6,1.3,Iris-versicolor -5.5,2.6,4.4,1.2,Iris-versicolor -5.3,3.7,1.5,0.2,Iris-setosa -5.6,3,4.1,1.3,Iris-versicolor -7.3,2.9,6.3,1.8,Iris-virginica -6.7,3.3,5.7,2.1,Iris-virginica -5.1,3.7,1.5,0.4,Iris-setosa -4.9,2.4,3.3,1,Iris-versicolor -6.7,3.3,5.7,2.5,Iris-virginica -7.2,3,5.8,1.6,Iris-virginica -4.9,3.1,1.5,0.1,Iris-setosa -6.7,3.1,5.6,2.4,Iris-virginica -4.9,3,1.4,0.2,Iris-setosa -6.9,3.1,4.9,1.5,Iris-versicolor -7.4,2.8,6.1,1.9,Iris-virginica -6.3,2.9,5.6,1.8,Iris-virginica -5.7,2.8,4.1,1.3,Iris-versicolor -6.5,3,5.5,1.8,Iris-virginica -6.3,2.3,4.4,1.3,Iris-versicolor -6.4,2.9,4.3,1.3,Iris-versicolor -5.6,2.8,4.9,2,Iris-virginica -5.9,3,5.1,1.8,Iris-virginica -5.4,3.4,1.7,0.2,Iris-setosa -6.1,2.8,4,1.3,Iris-versicolor -4.9,2.5,4.5,1.7,Iris-virginica -5.8,4,1.2,0.2,Iris-setosa -5.8,2.6,4,1.2,Iris-versicolor -7.1,3,5.9,2.1,Iris-virginica diff --git a/ML_Algorithms/KNN/knn.py b/ML_Algorithms/KNN/knn.py deleted file mode 100644 index cdda149..0000000 --- a/ML_Algorithms/KNN/knn.py +++ /dev/null @@ -1,32 +0,0 @@ -import numpy as np -import pandas as pd - -def euclidean_distance(x1,x2): - distance=0 - for i in range(len(x1)): - distance+=np.square(x1[i]-x2[i]) - return distance - -def knn(training_x,training_y,testing_x): - predictions=[] - for i in range(len(testing_x)): - distance=[] - for j in range(len(training_x)): - distance.append(euclidean_distance(testing_x[i],training_x[j])) - mydict={"class":training_y,"Distances":distance} - distance_df=pd.DataFrame.from_dict(mydict) - sorted_distance = distance_df.sort_values(by=["Distances"]) - predict_labels=sorted_distance["class"][:5] - predicted_label=predict_labels.value_counts().index.tolist()[0] - predictions.append(predicted_label) - return predictions - -train_df=pd.read_csv("Code-Forge\ML_Algorithms\KNN\iris_train.csv") -train_x=train_df.drop("Species",axis=1).to_numpy() -train_y=train_df["Species"].to_numpy() - -test_df=pd.read_csv("Code-Forge\ML_Algorithms\KNN\iris_test.csv") -test_x=test_df.drop("Species",axis=1).to_numpy() -test_y=test_df["Species"].to_numpy() - -predictions=knn(train_x,train_y,test_x) From 59c6ed1a9fa54e17430c5f3746c7290ec35ada66 Mon Sep 17 00:00:00 2001 From: BipanjitGill <120119876+BipanjitGill@users.noreply.github.com> Date: Sun, 29 Oct 2023 21:34:39 +0530 Subject: [PATCH 5/5] Add comments in kMeans.py --- ML_Algorithms/K-Means/kMeans.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/ML_Algorithms/K-Means/kMeans.py b/ML_Algorithms/K-Means/kMeans.py index f87dc29..a83045a 100644 --- a/ML_Algorithms/K-Means/kMeans.py +++ b/ML_Algorithms/K-Means/kMeans.py @@ -1,16 +1,22 @@ +# Code for kMeans algorithm which is an Unsupervised Learning algorithm, which groups the unlabeled dataset into different clusters + import numpy as np class K_Means: - def __init__(self, k=2, max_iter=100, tol=0.001): + def __init__(self, k=2, max_iter=100, tol=0.001): self.k = k + # k= number of clusters self.max_iter = max_iter + # max_iter= maximum number of times to iterate through the dataset self.tol = tol + # tol= maximum tolerance level in the output def fit(self,data): - + # Initializing the centroid for each cluster self.centroids = {} + # Taking random points as centroid for i in range(self.k): self.centroids[i] = data[i] @@ -21,28 +27,39 @@ def fit(self,data): self.classifications[i] = [] for featureset in data: + + # Calculating distance from each centroid distances = [np.linalg.norm(featureset-self.centroids[centroid]) for centroid in self.centroids] + + #Finding the centroid from which distance is minimum classification = distances.index(min(distances)) self.classifications[classification].append(featureset) prev_centroids = dict(self.centroids) + # Finding the new centroid on the basis of new classified points for classification in self.classifications: self.centroids[classification] = np.average(self.classifications[classification],axis=0) optimized = True + #Checking if the centroid each cluster converge for c in self.centroids: original_centroid = prev_centroids[c] current_centroid = self.centroids[c] + + # Comparing the previous and new centroids if np.sum((current_centroid-original_centroid)/original_centroid*100.0) > self.tol: # print(np.sum((current_centroid-original_centroid)/original_centroid*100.0)) optimized = False - + + # If the centroids converge then it is done else again iterate through the whole dataset and find the new centroids if optimized: break def predict(self,data): + # Calculating the distance of test data from the centroid of each cluster distances = [np.linalg.norm(data-self.centroids[centroid]) for centroid in self.centroids] + # Classify the data with the cluster from which distance is minimum classification = distances.index(min(distances)) - return classification \ No newline at end of file + return classification