{"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":"gpu","dataSources":[{"sourceId":3649,"databundleVersionId":46718,"sourceType":"competition"},{"sourceId":2965251,"sourceType":"datasetVersion","datasetId":1817999}],"dockerImageVersionId":30627,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-18T06:00:23.8212Z","iopub.execute_input":"2023-12-18T06:00:23.821807Z","iopub.status.idle":"2023-12-18T06:00:23.82976Z","shell.execute_reply.started":"2023-12-18T06:00:23.82178Z","shell.execute_reply":"2023-12-18T06:00:23.828911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls","metadata":{"execution":{"iopub.status.busy":"2023-12-18T06:00:36.911158Z","iopub.execute_input":"2023-12-18T06:00:36.911562Z","iopub.status.idle":"2023-12-18T06:00:37.838321Z","shell.execute_reply.started":"2023-12-18T06:00:36.911528Z","shell.execute_reply":"2023-12-18T06:00:37.837199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # This Python 3 environment comes with many helpful analytics libraries installed\n# # It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# # For example, here's several helpful packages to load\n\n# import numpy as np # linear algebra\n# import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# # Input data files are available in the read-only \"../input/\" directory\n# # For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\n# import os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n# # You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# # You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"execution":{"iopub.status.busy":"2023-12-18T06:00:37.861351Z","iopub.execute_input":"2023-12-18T06:00:37.861673Z","iopub.status.idle":"2023-12-18T06:00:37.86672Z","shell.execute_reply.started":"2023-12-18T06:00:37.861646Z","shell.execute_reply":"2023-12-18T06:00:37.865735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import numpy as np\n# import os","metadata":{"execution":{"iopub.status.busy":"2023-12-18T06:00:38.487449Z","iopub.execute_input":"2023-12-18T06:00:38.488257Z","iopub.status.idle":"2023-12-18T06:00:38.492063Z","shell.execute_reply.started":"2023-12-18T06:00:38.488223Z","shell.execute_reply":"2023-12-18T06:00:38.491033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for dir_name,_,files in os.walk('/kaggle/input/cifar-10'):\n#     for file in files:\n#         print(file)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T06:00:39.069623Z","iopub.execute_input":"2023-12-18T06:00:39.069993Z","iopub.status.idle":"2023-12-18T06:00:39.074025Z","shell.execute_reply.started":"2023-12-18T06:00:39.069964Z","shell.execute_reply":"2023-12-18T06:00:39.073088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pip install py7zr","metadata":{"execution":{"iopub.status.busy":"2023-12-18T06:00:39.551051Z","iopub.execute_input":"2023-12-18T06:00:39.551359Z","iopub.status.idle":"2023-12-18T06:00:39.555347Z","shell.execute_reply.started":"2023-12-18T06:00:39.551333Z","shell.execute_reply":"2023-12-18T06:00:39.554411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import py7zr\n\n# with py7zr.SevenZipFile('/kaggle/input/cifar-10/train.7z','r') as fp:\n#     fp.extract('/kaggle/working/')\n    \n# import cv2\n\n# images = list()\n# for dir_name,_,files in os.walk('/kaggle/working/train'):\n#     for file in files:\n#         path = os.path.join(dir_name,file)\n#         img = cv2.imread(path)\n#         images.append(img.tolist())\n# images = np.array(images)\n# images.shape\n\n# import matplotlib.pyplot as plt\n# img = images[0]\n# img.shape\n\n# plt.figure(figsize=(1,1))\n# plt.imshow(img)\n# plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T06:00:40.099708Z","iopub.execute_input":"2023-12-18T06:00:40.10056Z","iopub.status.idle":"2023-12-18T06:00:40.105305Z","shell.execute_reply.started":"2023-12-18T06:00:40.100519Z","shell.execute_reply":"2023-12-18T06:00:40.104367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# pip install py7zr","metadata":{"execution":{"iopub.status.busy":"2023-12-18T06:00:42.131606Z","iopub.execute_input":"2023-12-18T06:00:42.131997Z","iopub.status.idle":"2023-12-18T06:00:42.13621Z","shell.execute_reply.started":"2023-12-18T06:00:42.131965Z","shell.execute_reply":"2023-12-18T06:00:42.135266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport numpy as np\nimport cv2\nimport pandas as pd\n# import py7zr","metadata":{"execution":{"iopub.status.busy":"2023-12-18T06:00:42.817466Z","iopub.execute_input":"2023-12-18T06:00:42.817789Z","iopub.status.idle":"2023-12-18T06:00:42.822502Z","shell.execute_reply.started":"2023-12-18T06:00:42.817762Z","shell.execute_reply":"2023-12-18T06:00:42.82164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Extract images from the 7z file\n# with py7zr.SevenZipFile('/kaggle/input/cifar-10/train.7z','r') as fp:\n#   fp.extractall('/kaggle/working/')","metadata":{"execution":{"iopub.status.busy":"2023-12-18T06:00:43.525082Z","iopub.execute_input":"2023-12-18T06:00:43.525415Z","iopub.status.idle":"2023-12-18T06:00:43.529467Z","shell.execute_reply.started":"2023-12-18T06:00:43.525382Z","shell.execute_reply":"2023-12-18T06:00:43.528479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train","metadata":{}},{"cell_type":"code","source":"images_train = []\nlabels_train = []\n\nfor dir_name, _, files in os.walk('/kaggle/input/vegetable-image-dataset/Vegetable Images/train/'):\n#     print(dir_name)\n    for file in files:\n        path = os.path.join(dir_name, file)\n        img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)\n        img = cv2.resize(img, (32, 32))\n        images_train.append(img.flatten())\n#         images_train.append(img)\n        labels_train.append(dir_name.split(\"/\")[-1])\n    \n#         print(path)\nnp.random.shuffle(images_train)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-18T06:00:44.487477Z","iopub.execute_input":"2023-12-18T06:00:44.488173Z","iopub.status.idle":"2023-12-18T06:02:30.500006Z","shell.execute_reply.started":"2023-12-18T06:00:44.488138Z","shell.execute_reply":"2023-12-18T06:02:30.498971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\n\nimages_train = np.array(images_train[:1500])\nlabels_train = np.array(labels_train[:1500])\n\nt = LabelEncoder()\nlabels_train = t.fit_transform(labels_train)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T06:02:30.502027Z","iopub.execute_input":"2023-12-18T06:02:30.502704Z","iopub.status.idle":"2023-12-18T06:02:30.983253Z","shell.execute_reply.started":"2023-12-18T06:02:30.502668Z","shell.execute_reply":"2023-12-18T06:02:30.982474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(labels_train)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T06:02:30.984391Z","iopub.execute_input":"2023-12-18T06:02:30.984747Z","iopub.status.idle":"2023-12-18T06:02:30.990097Z","shell.execute_reply.started":"2023-12-18T06:02:30.984715Z","shell.execute_reply":"2023-12-18T06:02:30.98911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(images_train))\nprint(len(labels_train))","metadata":{"execution":{"iopub.status.busy":"2023-12-18T06:02:30.99289Z","iopub.execute_input":"2023-12-18T06:02:30.993218Z","iopub.status.idle":"2023-12-18T06:02:31.000038Z","shell.execute_reply.started":"2023-12-18T06:02:30.993187Z","shell.execute_reply":"2023-12-18T06:02:30.999119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(images_train[0])","metadata":{"execution":{"iopub.status.busy":"2023-12-18T06:02:31.001461Z","iopub.execute_input":"2023-12-18T06:02:31.001808Z","iopub.status.idle":"2023-12-18T06:02:31.008967Z","shell.execute_reply.started":"2023-12-18T06:02:31.001776Z","shell.execute_reply":"2023-12-18T06:02:31.008072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(images_train[0])","metadata":{"execution":{"iopub.status.busy":"2023-12-18T06:02:31.01009Z","iopub.execute_input":"2023-12-18T06:02:31.010431Z","iopub.status.idle":"2023-12-18T06:02:31.019869Z","shell.execute_reply.started":"2023-12-18T06:02:31.010401Z","shell.execute_reply":"2023-12-18T06:02:31.018871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Validation","metadata":{}},{"cell_type":"code","source":"# Load the testing images\nimages_val = []\nlabels_val = []\n\nfor dir_name, _, files in os.walk('/kaggle/input/vegetable-image-dataset/Vegetable Images/validation/'):\n    for file in files:\n        path = os.path.join(dir_name, file)\n        img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)\n        img = cv2.resize(img, (32, 32))\n        images_val.append(img.flatten())\n#         images_test.append(img)\n        labels_val.append(dir_name.split(\"/\")[-1])\n    \nnp.random.shuffle(images_val)\n\n    \n","metadata":{"execution":{"iopub.status.busy":"2023-12-18T06:02:31.021Z","iopub.execute_input":"2023-12-18T06:02:31.021268Z","iopub.status.idle":"2023-12-18T06:02:52.379085Z","shell.execute_reply.started":"2023-12-18T06:02:31.021245Z","shell.execute_reply":"2023-12-18T06:02:52.378213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\n\nimages_val = np.array(images_val[:500])\nlabels_val = np.array(labels_val[:500])\n\nt = LabelEncoder()\nlabels_val = t.fit_transform(labels_val)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T06:02:52.380376Z","iopub.execute_input":"2023-12-18T06:02:52.380743Z","iopub.status.idle":"2023-12-18T06:02:52.389182Z","shell.execute_reply.started":"2023-12-18T06:02:52.38071Z","shell.execute_reply":"2023-12-18T06:02:52.388271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(len(images_test))\n# print(len(labels_test))","metadata":{"execution":{"iopub.status.busy":"2023-12-18T06:02:52.3904Z","iopub.execute_input":"2023-12-18T06:02:52.391009Z","iopub.status.idle":"2023-12-18T06:02:52.39827Z","shell.execute_reply.started":"2023-12-18T06:02:52.390975Z","shell.execute_reply":"2023-12-18T06:02:52.397564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test","metadata":{}},{"cell_type":"code","source":"# Load the testing images\nimages_test = []\nlabels_test = []\n\nfor dir_name, _, files in os.walk('/kaggle/input/vegetable-image-dataset/Vegetable Images/test/'):\n    for file in files:\n        path = os.path.join(dir_name, file)\n        img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)\n        img = cv2.resize(img, (32, 32))\n        images_test.append(img.flatten())\n#         images_test.append(img)\n        labels_test.append(dir_name.split(\"/\")[-1])\n    \nnp.random.shuffle(images_test)\n\n    \n","metadata":{"execution":{"iopub.status.busy":"2023-12-18T06:02:52.400899Z","iopub.execute_input":"2023-12-18T06:02:52.401185Z","iopub.status.idle":"2023-12-18T06:03:16.02871Z","shell.execute_reply.started":"2023-12-18T06:02:52.401162Z","shell.execute_reply":"2023-12-18T06:03:16.027882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\n\nimages_test = np.array(images_test[:500])\nlabels_test = np.array(labels_test[:500])\n\nt = LabelEncoder()\nlabels_test = t.fit_transform(labels_test)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T06:03:16.030051Z","iopub.execute_input":"2023-12-18T06:03:16.030441Z","iopub.status.idle":"2023-12-18T06:03:16.038773Z","shell.execute_reply.started":"2023-12-18T06:03:16.030405Z","shell.execute_reply":"2023-12-18T06:03:16.037882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(images_test))\nprint(len(labels_test))","metadata":{"execution":{"iopub.status.busy":"2023-12-18T06:03:16.040091Z","iopub.execute_input":"2023-12-18T06:03:16.04038Z","iopub.status.idle":"2023-12-18T06:03:16.050455Z","shell.execute_reply.started":"2023-12-18T06:03:16.040355Z","shell.execute_reply":"2023-12-18T06:03:16.04934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(images_test[:9])","metadata":{"execution":{"iopub.status.busy":"2023-12-18T06:03:16.051477Z","iopub.execute_input":"2023-12-18T06:03:16.051767Z","iopub.status.idle":"2023-12-18T06:03:16.061932Z","shell.execute_reply.started":"2023-12-18T06:03:16.051744Z","shell.execute_reply":"2023-12-18T06:03:16.061046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"k-means","metadata":{}},{"cell_type":"code","source":"import random\n\nimport numpy as np\nimport math as m\nimport matplotlib.pyplot as plt\n\n\n\n\n# my_data = np.genfromtxt(data_path, delimiter=',')\n# data = images_train\n# data.pop(0)\n\n\ndef xyvalue(data_set):\n    a = m.ceil(len(data_set[0])/2)\n    x = [0.0]*len(data_set)\n    y = [0.0]*len(data_set)\n    for i in range(len(data_set)):\n        sum_x = 0.0\n        sum_y = 0.0\n        for w in range(0,a):\n            sum_x = sum_x + data_set[i][w]\n        x[i] = sum_x\n        for z in range(a,len(data_set[0])):\n            sum_y = sum_y + data_set[i][z]\n        y[i] = sum_y\n    return x,y\n    \n\n\n\n\n\nimages_tra = images_train.tolist()\nimages_v = images_val.tolist()\ntrain_set = images_train.tolist()\ntest_set = images_test.tolist()\n\nrow_elementlength = len(images_train[0])\n\n# k = int(input(\"enter : \"))\nk = 5\nrandom_k = random.sample(train_set, k)\n\n\n\ninit = 0\nc = 0\nnew_k = [0.0] * k\ndic = {}\n\nwhile True:\n    \n    if init == 0:\n        for i in train_set:\n            kdis = [0.0] * k\n            for j in random_k:\n                s = 0.0\n                for f in range(0, len(j)):\n                    s = s + m.pow((i[f] - j[f]), 2)\n                s = m.sqrt(s)\n                kdis[random_k.index(j)] = m.sqrt(s)\n            if kdis.index(min(kdis)) in dic:\n                dic[kdis.index(min(kdis))].append(i)\n            else:\n                dic[kdis.index(min(kdis))] = []\n                dic[kdis.index(min(kdis))].append([0].insert(0, i))\n                dic[kdis.index(min(kdis))].pop(0)\n        init = 1\n        \n    \n    for i in range(k):\n        # print(i,dic[i])\n        if i in dic and len(dic[i]) != 0:\n            # print(len(dic[i][i]))\n            lenli = [0.0] * row_elementlength\n            for j in range(0, row_elementlength):\n                s = 0.0\n                li = dic[i]\n                for l in range(len(dic[i])):\n                    s = s + li[l][j]\n                avg = s / len(dic[i])\n                lenli[j] = avg\n            new_k[i] = lenli\n        else:\n            new_k[i] = [0.0] * row_elementlength\n\n    new_dic = {}\n    for i in train_set:\n        kdis = [0.0] * k\n        for j in new_k:\n            s = 0.0\n            for f in range(0, len(j)):\n                s = s + m.pow((i[f] - j[f]), 2)\n            s = m.sqrt(s)\n            kdis[new_k.index(j)] = m.sqrt(s)\n        if kdis.index(min(kdis)) in new_dic:\n            new_dic[kdis.index(min(kdis))].append(i)\n        else:\n            new_dic[kdis.index(min(kdis))] = []\n            new_dic[kdis.index(min(kdis))].append([0].insert(0, i))\n            new_dic[kdis.index(min(kdis))].pop(0)\n\n\n    if new_dic == dic:\n        break\n    else:\n        dic = new_dic\n\n\ntest_dic = {}\nfor i in test_set:\n    kdis = [0.0] * k\n    for j in random_k:\n        s = 0.0\n        for f in range(0, len(j)):\n            s = s + m.pow((i[f] - j[f]), 2)\n        s = m.sqrt(s)\n        kdis[random_k.index(j)] = m.sqrt(s)\n    if kdis.index(min(kdis)) in test_dic:\n        test_dic[kdis.index(min(kdis))].append(i)\n    else:\n        test_dic[kdis.index(min(kdis))] = []\n        test_dic[kdis.index(min(kdis))].append([0].insert(0, i))\n        test_dic[kdis.index(min(kdis))].pop(0)\n\n\n\n\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-12-18T06:03:16.063132Z","iopub.execute_input":"2023-12-18T06:03:16.063424Z","iopub.status.idle":"2023-12-18T06:07:10.835919Z","shell.execute_reply.started":"2023-12-18T06:03:16.063399Z","shell.execute_reply":"2023-12-18T06:07:10.835045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"col = ['purple','blue','cyan','purple','black','yollow','purple','blue','cyan','purple','blue','cyan','purple','blue','cyan','purple','blue','cyan']\n\n\n\nfor i in dic:\n    if(len(dic[i])>=1):\n        x,y = xyvalue(dic[i])\n        plt.scatter(x,y,s=15,marker='.',edgecolors=col[i])\n    x,y = xyvalue(new_k)\n    plt.scatter(x[i],y[i],s=50,marker='s',edgecolors=col[i])\n\n\n\nplt.show()\n  ","metadata":{"execution":{"iopub.status.busy":"2023-12-18T06:07:10.837164Z","iopub.execute_input":"2023-12-18T06:07:10.837513Z","iopub.status.idle":"2023-12-18T06:07:11.474738Z","shell.execute_reply.started":"2023-12-18T06:07:10.837481Z","shell.execute_reply":"2023-12-18T06:07:11.473876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-12-18T06:00:28.543133Z","iopub.status.idle":"2023-12-18T06:00:28.543464Z","shell.execute_reply.started":"2023-12-18T06:00:28.543301Z","shell.execute_reply":"2023-12-18T06:00:28.543317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Example usage of KMeans\n# kmeans = KMeans(k=3)\n# kmeans.train(images_train)\n\n# # Access the cluster assignments and centroids\n# cluster_assignments = kmeans.clusters\n# centroids = kmeans.centroids","metadata":{"execution":{"iopub.status.busy":"2023-12-18T06:00:28.544555Z","iopub.status.idle":"2023-12-18T06:00:28.544915Z","shell.execute_reply.started":"2023-12-18T06:00:28.544719Z","shell.execute_reply":"2023-12-18T06:00:28.544735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import matplotlib.pyplot as plt\n\n# # Assuming you have 2D data, change indexing accordingly if you have more dimensions\n# x_values = images_train[:, 0]\n# y_values = images_train[:, 1]\n\n# # Scatter plot of data points\n# plt.scatter(x_values, y_values, c=cluster_assignments, cmap='viridis', alpha=0.5, edgecolors='w')\n\n# # Plot centroids\n# plt.scatter(kmeans.centroids[:, 0], kmeans.centroids[:, 1], c='red', marker='X', s=200, label='Centroids')\n\n# plt.title('KMeans Clustering')\n# plt.xlabel('Feature 1')\n# plt.ylabel('Feature 2')\n# plt.legend()\n# plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-12-18T06:00:28.546036Z","iopub.status.idle":"2023-12-18T06:00:28.546363Z","shell.execute_reply.started":"2023-12-18T06:00:28.546199Z","shell.execute_reply":"2023-12-18T06:00:28.546215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(cluster_assignments)\n# print(centroids)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T06:00:28.547733Z","iopub.status.idle":"2023-12-18T06:00:28.548091Z","shell.execute_reply.started":"2023-12-18T06:00:28.547923Z","shell.execute_reply":"2023-12-18T06:00:28.547939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# logistic ","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport math\nfrom scipy.special import expit\nimport matplotlib.pyplot as plt\n\nimages_train = images_train.tolist()\nimages_test = images_test.tolist()\nfor x in images_train:\n    x.insert(0, 1)\n\n# for x in Val_set:\n#     x.insert(0,1)\n\nfor x in images_test:\n    x.insert(0,1)\n\n\ntheta = np.random.rand(1,len(images_train[0])-1)\n# print(theta[0][:4])\ntheta = np. array(theta[0])\nprint(theta)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-18T06:18:01.434643Z","iopub.execute_input":"2023-12-18T06:18:01.435456Z","iopub.status.idle":"2023-12-18T06:18:01.500767Z","shell.execute_reply.started":"2023-12-18T06:18:01.43542Z","shell.execute_reply":"2023-12-18T06:18:01.499618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for i in range(len(images_train)):\n#     print(images_train[i])","metadata":{"execution":{"iopub.status.busy":"2023-12-18T06:18:04.55544Z","iopub.execute_input":"2023-12-18T06:18:04.555785Z","iopub.status.idle":"2023-12-18T06:18:04.559672Z","shell.execute_reply.started":"2023-12-18T06:18:04.555758Z","shell.execute_reply":"2023-12-18T06:18:04.55877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(images_train[0])\n# images_train[0]","metadata":{"execution":{"iopub.status.busy":"2023-12-18T06:18:05.662261Z","iopub.execute_input":"2023-12-18T06:18:05.662614Z","iopub.status.idle":"2023-12-18T06:18:05.6687Z","shell.execute_reply.started":"2023-12-18T06:18:05.662587Z","shell.execute_reply":"2023-12-18T06:18:05.667768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"size = len(images_train[0])-1\n\nlr = 0.0001\n\nprint(lr)\n\ntrain_loss = []\n\n\n\nfor i in range(1000):\n    TJ = 0\n    for j in images_train:\n        z = np.dot(np. array(j[:size]), theta)\n        h = expit(z)\n        J = -j[size]* np.log(h) -(1-j[size])*np.log(1-h)\n        TJ = TJ + J\n        dv = np. array(j[:size])*(h-j[size])\n        theta = np.subtract(theta ,dv * lr)\n    TJ = TJ/len(images_train)\n    train_loss.append(TJ)\n\n\n\ncorrect = 0\nfor t in images_test:\n    z = np.dot(np.array(t[:size]),theta)\n    h = expit(z)\n    if h>=0.5:\n        h = 1\n    else:\n        h = 0\n    if t[size] == h:\n        correct = correct+1\naccuracy = (correct/len(images_test))*100\nprint(accuracy)\n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-12-18T06:18:06.696995Z","iopub.execute_input":"2023-12-18T06:18:06.697328Z","iopub.status.idle":"2023-12-18T06:23:55.263397Z","shell.execute_reply.started":"2023-12-18T06:18:06.697304Z","shell.execute_reply":"2023-12-18T06:23:55.262384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nepoch=np.array(range(0,10000))\nplt.plot(epoch,np.array(train_loss))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T06:23:55.265041Z","iopub.execute_input":"2023-12-18T06:23:55.265335Z","iopub.status.idle":"2023-12-18T06:23:56.616909Z","shell.execute_reply.started":"2023-12-18T06:23:55.26531Z","shell.execute_reply":"2023-12-18T06:23:56.615658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def calculate_accuracy(predicted_values, true_values):\n#     # Ensure the lengths of predicted_values and true_values are the same\n#     if len(predicted_values) != len(true_values):\n#         raise ValueError(\"Lengths of predicted_values and true_values must be the same.\")\n\n#     # Count the number of correct predictions\n#     correct_predictions = np.sum(predicted_values == true_values)\n\n#     # Calculate accuracy\n#     accuracy = correct_predictions / len(true_values) * 100.0\n\n#     return accuracy\n\n# # Example usage:\n# # Assuming predicted_values and true_values are NumPy arrays or lists\n","metadata":{"execution":{"iopub.status.busy":"2023-12-18T06:00:28.558319Z","iopub.status.idle":"2023-12-18T06:00:28.558669Z","shell.execute_reply.started":"2023-12-18T06:00:28.558502Z","shell.execute_reply":"2023-12-18T06:00:28.558518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import numpy as np\n# from sklearn.preprocessing import LabelEncoder\n\n# class LogisticRegression:\n#     def __init__(self, epochs, lr):\n#         self.epochs = epochs\n#         self.lr = lr\n#         self.w = None\n#         self.b = None\n\n#     def sigmoid(self, z):\n#         return 1 / (1 + np.exp(-z))\n\n#     def train(self, X, y):\n#         m, n = X.shape\n#         self.w = np.zeros(n)\n#         self.b = 0\n\n#         # Use LabelEncoder to convert string labels to numeric values\n#         label_encoder = LabelEncoder()\n#         y_encoded = label_encoder.fit_transform(y)\n\n#         for epoch in range(self.epochs):\n#             for i in range(m):\n#                 x = X[i, :]\n#                 z = np.dot(x, self.w) + self.b\n#                 y_hat = self.sigmoid(z)\n\n#                 grad_w = (y_hat - float(y_encoded[i])) * x\n#                 grad_b = y_hat - float(y_encoded[i])\n\n#                 self.w -= self.lr * grad_w\n#                 self.b -= self.lr * grad_b\n\n#     def predict(self, X):\n#         z = np.dot(X, self.w) + self.b\n#         y_pred = self.sigmoid(z)\n\n#         # Assuming binary classification, round the predictions to 0 or 1\n#         return np.round(y_pred)\n\n# # Example usage\n# lr = LogisticRegression(epochs=100, lr=0.01)\n# lr.train(images_train, labels_train)\n# y_pred_lr = lr.predict(images_test)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-18T06:00:28.560655Z","iopub.status.idle":"2023-12-18T06:00:28.561033Z","shell.execute_reply.started":"2023-12-18T06:00:28.560821Z","shell.execute_reply":"2023-12-18T06:00:28.560855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Train and test the Logistic Regression algorithm\n# lr = LogisticRegression(epochs=100, lr=0.01)\n# lr.train(images_train, labels_train)\n# y_pred_lr = lr.predict(images_test)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T06:00:28.563794Z","iopub.status.idle":"2023-12-18T06:00:28.564175Z","shell.execute_reply.started":"2023-12-18T06:00:28.564007Z","shell.execute_reply":"2023-12-18T06:00:28.564024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # print(y_pred_lr[0])\n# # print(\"Logistic Regression accuracy:\", np.mean(y_pred_lr == labels_test))\n# accuracy = calculate_accuracy(y_pred_lr, labels_test)\n\n# print(f\"Accuracy: {accuracy:.2f}%\")\n","metadata":{"execution":{"iopub.status.busy":"2023-12-18T06:00:28.565061Z","iopub.status.idle":"2023-12-18T06:00:28.565399Z","shell.execute_reply.started":"2023-12-18T06:00:28.565236Z","shell.execute_reply":"2023-12-18T06:00:28.565252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# KNN Class\nclass KNN:\n    def __init__(self, k):\n        self.k = k\n\n    def train(self, X_train, y_train):\n        self.X_train = X_train\n        self.y_train = y_train\n\n    def predict(self, X_test):\n        y_pred = []\n        for x in X_test:\n            distances = np.linalg.norm(self.X_train - x, axis=1)\n            knn_indices = np.argsort(distances)[:self.k]\n            knn_labels = self.y_train[knn_indices]\n            y_pred.append(np.argmax(np.bincount(knn_labels)))\n\n        return np.array(y_pred)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T06:46:13.375441Z","iopub.execute_input":"2023-12-18T06:46:13.375857Z","iopub.status.idle":"2023-12-18T06:46:13.383045Z","shell.execute_reply.started":"2023-12-18T06:46:13.375808Z","shell.execute_reply":"2023-12-18T06:46:13.382028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train and test the KNN algorithm\nknn = KNN(k=5)\nknn.train(images_train, labels_train)\ny_pred_knn = knn.predict(images_test)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T06:46:15.238767Z","iopub.execute_input":"2023-12-18T06:46:15.239162Z","iopub.status.idle":"2023-12-18T06:46:15.296386Z","shell.execute_reply.started":"2023-12-18T06:46:15.239133Z","shell.execute_reply":"2023-12-18T06:46:15.295283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(\"KNN accuracy:\", np.mean(y_pred_knn == labels_test))\naccuracy = calculate_accuracy(y_pred_knn, labels_test)\n\nprint(f\"Accuracy: {accuracy:.2f}%\")","metadata":{"execution":{"iopub.status.busy":"2023-12-18T06:46:16.871612Z","iopub.execute_input":"2023-12-18T06:46:16.871989Z","iopub.status.idle":"2023-12-18T06:46:16.89942Z","shell.execute_reply.started":"2023-12-18T06:46:16.871958Z","shell.execute_reply":"2023-12-18T06:46:16.898127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}