{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":2965251,"sourceType":"datasetVersion","datasetId":1817999}],"dockerImageVersionId":30626,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"#importing necessary libraries.\nimport numpy as np\nimport os\nimport cv2\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-17T07:48:58.216218Z","iopub.execute_input":"2023-12-17T07:48:58.216633Z","iopub.status.idle":"2023-12-17T07:48:58.484256Z","shell.execute_reply.started":"2023-12-17T07:48:58.216597Z","shell.execute_reply":"2023-12-17T07:48:58.483074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#path for train & test dataset.\ntrain_path = \"/kaggle/input/vegetable-image-dataset/Vegetable Images/train\"\ntest_path = \"/kaggle/input/vegetable-image-dataset/Vegetable Images/test\"","metadata":{"execution":{"iopub.status.busy":"2023-12-17T07:48:58.485727Z","iopub.execute_input":"2023-12-17T07:48:58.486037Z","iopub.status.idle":"2023-12-17T07:48:58.491785Z","shell.execute_reply.started":"2023-12-17T07:48:58.486012Z","shell.execute_reply":"2023-12-17T07:48:58.490372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def data_load(path):\n    X = list()\n    y = list()\n    labels = 0\n    for dir_name,x,files in os.walk(path):\n        if(dir_name != path):\n            for file in files:\n                file_path = os.path.join(dir_name,file)\n                img = cv2.imread(file_path,flags=0)\n                img = cv2.resize(img,(32,32))\n                X.append(img.tolist())\n                y.append(labels)\n            labels = labels + 1\n    return X,y","metadata":{"execution":{"iopub.status.busy":"2023-12-17T07:48:58.493740Z","iopub.execute_input":"2023-12-17T07:48:58.494033Z","iopub.status.idle":"2023-12-17T07:48:58.505315Z","shell.execute_reply.started":"2023-12-17T07:48:58.494008Z","shell.execute_reply":"2023-12-17T07:48:58.504013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, y_train = data_load(train_path)","metadata":{"execution":{"iopub.status.busy":"2023-12-17T07:48:58.508104Z","iopub.execute_input":"2023-12-17T07:48:58.508627Z","iopub.status.idle":"2023-12-17T07:51:23.166101Z","shell.execute_reply.started":"2023-12-17T07:48:58.508565Z","shell.execute_reply":"2023-12-17T07:51:23.164394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#converting train lists to np array\nX_train = np.array(X_train)\ny_train = np.array(y_train)","metadata":{"execution":{"iopub.status.busy":"2023-12-17T07:51:23.167718Z","iopub.execute_input":"2023-12-17T07:51:23.168226Z","iopub.status.idle":"2023-12-17T07:51:23.912593Z","shell.execute_reply.started":"2023-12-17T07:51:23.168187Z","shell.execute_reply":"2023-12-17T07:51:23.911271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape)\nprint(y_train.shape)","metadata":{"execution":{"iopub.status.busy":"2023-12-17T07:51:23.914161Z","iopub.execute_input":"2023-12-17T07:51:23.914517Z","iopub.status.idle":"2023-12-17T07:51:23.925747Z","shell.execute_reply.started":"2023-12-17T07:51:23.914486Z","shell.execute_reply":"2023-12-17T07:51:23.924454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.unique(y_train)","metadata":{"execution":{"iopub.status.busy":"2023-12-17T07:51:23.927222Z","iopub.execute_input":"2023-12-17T07:51:23.927649Z","iopub.status.idle":"2023-12-17T07:51:23.944677Z","shell.execute_reply.started":"2023-12-17T07:51:23.927618Z","shell.execute_reply":"2023-12-17T07:51:23.943280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(1,1))\nplt.imshow(X_train[0])\nplt.show()\n\nprint(y_train[0])","metadata":{"execution":{"iopub.status.busy":"2023-12-17T07:51:23.947755Z","iopub.execute_input":"2023-12-17T07:51:23.948217Z","iopub.status.idle":"2023-12-17T07:51:24.094725Z","shell.execute_reply.started":"2023-12-17T07:51:23.948175Z","shell.execute_reply":"2023-12-17T07:51:24.094041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test,y_test = data_load(test_path)","metadata":{"execution":{"iopub.status.busy":"2023-12-17T07:51:24.095698Z","iopub.execute_input":"2023-12-17T07:51:24.096724Z","iopub.status.idle":"2023-12-17T07:51:52.199144Z","shell.execute_reply.started":"2023-12-17T07:51:24.096694Z","shell.execute_reply":"2023-12-17T07:51:52.197893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#converting train lists to np array\nX_test = np.array(X_test)\ny_test = np.array(y_test)","metadata":{"execution":{"iopub.status.busy":"2023-12-17T07:51:52.203589Z","iopub.execute_input":"2023-12-17T07:51:52.204121Z","iopub.status.idle":"2023-12-17T07:51:52.358229Z","shell.execute_reply.started":"2023-12-17T07:51:52.204087Z","shell.execute_reply":"2023-12-17T07:51:52.356417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_test.shape)\nprint(y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-12-17T07:51:52.360435Z","iopub.execute_input":"2023-12-17T07:51:52.360855Z","iopub.status.idle":"2023-12-17T07:51:52.373319Z","shell.execute_reply.started":"2023-12-17T07:51:52.360818Z","shell.execute_reply":"2023-12-17T07:51:52.371631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(1,1))\nplt.imshow(X_test[0])\nplt.show()\n\nprint(y_test[0])","metadata":{"execution":{"iopub.status.busy":"2023-12-17T07:51:52.374621Z","iopub.execute_input":"2023-12-17T07:51:52.375162Z","iopub.status.idle":"2023-12-17T07:51:52.491023Z","shell.execute_reply.started":"2023-12-17T07:51:52.375130Z","shell.execute_reply":"2023-12-17T07:51:52.490264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# KNN","metadata":{}},{"cell_type":"code","source":"#class for knn algorithm.\nclass KNN:\n    def __init__(self, k):\n        self.k = k\n\n    def euclidean_distance(self, test, train):\n        return np.sqrt(np.sum((test - train)**2))\n\n    def train(self, X_train, Y_train, X_test):\n        self.xtrain = X_train\n        self.ytrain = Y_train\n        self.xtest = X_test\n\n        self.result = []\n\n        for x_test in self.xtest:\n            distances = [self.euclidean_distance(x_test, x_train) for x_train in self.xtrain]\n            distances = np.argsort(distances)\n            samples = distances[:self.k]\n            y_bar = int(np.mean([self.ytrain[i] for i in samples]))\n            self.result.append(y_bar)\n\n        return self.result\n\n    def accuracy(self, result, y_test):\n        correct = np.sum(result == y_test)\n        accuracy = (correct / len(y_test))\n\n        return accuracy * 100\n","metadata":{"execution":{"iopub.status.busy":"2023-12-17T07:51:52.492103Z","iopub.execute_input":"2023-12-17T07:51:52.493149Z","iopub.status.idle":"2023-12-17T07:51:52.501389Z","shell.execute_reply.started":"2023-12-17T07:51:52.493118Z","shell.execute_reply":"2023-12-17T07:51:52.500546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn = KNN(k=3)","metadata":{"execution":{"iopub.status.busy":"2023-12-17T07:51:52.502480Z","iopub.execute_input":"2023-12-17T07:51:52.503672Z","iopub.status.idle":"2023-12-17T07:51:52.518019Z","shell.execute_reply.started":"2023-12-17T07:51:52.503638Z","shell.execute_reply":"2023-12-17T07:51:52.517194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = knn.train(X_train, y_train, X_test)","metadata":{"execution":{"iopub.status.busy":"2023-12-17T07:51:52.519074Z","iopub.execute_input":"2023-12-17T07:51:52.519842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy = knn.accuracy(np.array(results), y_test)\n\nprint(f\"Accuracy of the result: {accuracy:.2f}%\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# LR","metadata":{}},{"cell_type":"code","source":"class LogisticRegression:\n    def __init__(self, epochs, lr):\n        self.epochs = epochs\n        self.lr = lr\n        self.classifiers = {}\n\n    def sigmoid(self, z):\n        return 1 / (1 + np.exp(-np.clip(z, -500, 500)))\n\n    def train(self, X, y):\n        classes = len(np.unique(y))\n\n        for i in range(classes):\n            new_label = (y == i).astype(int)\n\n            weights = np.zeros(X.shape[1])\n            bias = 0\n\n            for epoch in range(self.epochs):\n                z = np.dot(X, weights) + bias\n                h = self.sigmoid(z)\n\n                dw = np.dot(X.T, (h - new_label)) / len(new_label)\n                db = np.sum(h - new_label) / len(new_label)\n\n                weights = weights - self.lr * dw\n                bias = bias - self.lr * db\n\n            self.classifiers[i] = {'weights': weights, 'bias': bias}\n\n    def predict(self, X):\n        samples = X.shape[0]\n        classes = len(self.classifiers)\n        probability = np.zeros((samples, classes))\n\n        for i in range(classes):\n            classifier = self.classifiers[i]\n            weights, bias = classifier['weights'], classifier['bias']\n            z = np.dot(X, weights) + bias\n            h = self.sigmoid(z)\n            probability[:, i] = h\n\n        predictions = np.argmax(probability, axis=1)\n        return predictions\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"log_reg = LogisticRegression(100, 0.0001)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_X_train = X_train.reshape(X_train.shape[0], -1)\nnew_X_test = X_test.reshape(X_test.shape[0], -1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"log_reg.train(new_X_train, y_train)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_prediction = log_reg.predict(new_X_test)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy = np.mean(y_prediction == y_test)\n\nprint(f'Logistic Regression Accuracy: {accuracy * 100:.2f}%')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# KMeans","metadata":{}},{"cell_type":"code","source":"class KMeans:\n    def __init__(self, K = 3):\n        self.k = K\n        self.centroids = None\n\n    def train(self, X):\n        n = X.shape[0]\n        np.random.seed(4)\n        self.centroids = X[np.random.choice(n,self.k,replace = False)]\n\n        while True:\n            distance = np.zeros((n,self.k))\n\n            for i in range(self.k):\n                 distance[:, i] = np.sqrt(((X - self.centroids[i]) ** 2).sum(axis = (1, 2)))\n\n            add_to_cluster = np.argmin(distance,axis = 1)\n            old_centroids = self.centroids.copy()\n\n            for i in range(self.k):\n                self.centroids[i] = X[add_to_cluster == i].mean(axis = 0)\n\n            if np.all(old_centroids == self.centroids):\n                break\n\n        clusters, counts = np.unique(add_to_cluster, return_counts=True)\n        cluster_members = dict(zip(clusters, counts))\n        self.clusters = cluster_members","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kmeans = KMeans(15)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kmeans.train(X_train)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(kmeans.clusters)\nprint(sum(kmeans.clusters.values()))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}