{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":13836,"databundleVersionId":1718836,"sourceType":"competition"}],"dockerImageVersionId":30732,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"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\nimport numpy as np # linear algebra\nimport 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\nimport os\nfor 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\n\nfrom IPython.display import clear_output\nclear_output()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-06-15T14:39:57.032575Z","iopub.execute_input":"2024-06-15T14:39:57.033658Z","iopub.status.idle":"2024-06-15T14:40:19.187656Z","shell.execute_reply.started":"2024-06-15T14:39:57.033607Z","shell.execute_reply":"2024-06-15T14:40:19.186124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom PIL import Image","metadata":{"execution":{"iopub.status.busy":"2024-06-15T14:40:19.189802Z","iopub.execute_input":"2024-06-15T14:40:19.190383Z","iopub.status.idle":"2024-06-15T14:40:19.195828Z","shell.execute_reply.started":"2024-06-15T14:40:19.190343Z","shell.execute_reply":"2024-06-15T14:40:19.194695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train  = pd.read_csv('/kaggle/input/cassava-leaf-disease-classification/train.csv')","metadata":{"execution":{"iopub.status.busy":"2024-06-15T14:40:19.197377Z","iopub.execute_input":"2024-06-15T14:40:19.197827Z","iopub.status.idle":"2024-06-15T14:40:19.248518Z","shell.execute_reply.started":"2024-06-15T14:40:19.197788Z","shell.execute_reply":"2024-06-15T14:40:19.247421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2024-06-15T14:40:19.251929Z","iopub.execute_input":"2024-06-15T14:40:19.252567Z","iopub.status.idle":"2024-06-15T14:40:19.277709Z","shell.execute_reply.started":"2024-06-15T14:40:19.252522Z","shell.execute_reply":"2024-06-15T14:40:19.276507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-15T14:40:19.279423Z","iopub.execute_input":"2024-06-15T14:40:19.279881Z","iopub.status.idle":"2024-06-15T14:40:19.290237Z","shell.execute_reply.started":"2024-06-15T14:40:19.279837Z","shell.execute_reply":"2024-06-15T14:40:19.289066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport csv\ndef list_images_index(folder_path , img_code):\n    files = os.listdir(folder_path)\n    \n#     img_extensions = ['.jpg', '.jpeg', '.png']\n    images = [file for file in files ]\n    \n    for index , image in enumerate(images):\n        if(image == img_code):\n            return index\n        \n    ","metadata":{"execution":{"iopub.status.busy":"2024-06-15T14:40:19.291989Z","iopub.execute_input":"2024-06-15T14:40:19.292742Z","iopub.status.idle":"2024-06-15T14:40:19.300407Z","shell.execute_reply.started":"2024-06-15T14:40:19.292691Z","shell.execute_reply":"2024-06-15T14:40:19.299112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '/kaggle/input/cassava-leaf-disease-classification/train_images/'\ncode = '2405559933.jpg'\nlist_images_index(path , code)","metadata":{"execution":{"iopub.status.busy":"2024-06-15T14:40:19.302285Z","iopub.execute_input":"2024-06-15T14:40:19.302704Z","iopub.status.idle":"2024-06-15T14:40:19.328912Z","shell.execute_reply.started":"2024-06-15T14:40:19.302666Z","shell.execute_reply":"2024-06-15T14:40:19.327547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"code = '2405559933.jpg'\nimage = Image.open('/kaggle/input/cassava-leaf-disease-classification/train_images/2405559933.jpg')\nimg_arr = np.array(image)\nplt.imshow(image)\nindex = list_images_index(path , code)\nprint('Category of disease is :',train.iloc[index , 1])","metadata":{"execution":{"iopub.status.busy":"2024-06-15T14:40:19.330451Z","iopub.execute_input":"2024-06-15T14:40:19.330818Z","iopub.status.idle":"2024-06-15T14:40:19.933183Z","shell.execute_reply.started":"2024-06-15T14:40:19.330787Z","shell.execute_reply":"2024-06-15T14:40:19.931992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### CLASSIFICATION WITH LOGISTIC REGRESSION","metadata":{"execution":{"iopub.status.busy":"2024-06-15T12:45:05.601770Z","iopub.execute_input":"2024-06-15T12:45:05.602134Z","iopub.status.idle":"2024-06-15T12:45:05.610021Z","shell.execute_reply.started":"2024-06-15T12:45:05.602106Z","shell.execute_reply":"2024-06-15T12:45:05.608903Z"}}},{"cell_type":"code","source":"# def images_to_array( folder ,part):\n#     images  = []\n#     count = 0\n#     for filename in os.listdir(folder):\n#         if part = 'a':\n            \n#         img = Image.open(os.path.join(folder, filename))\n#         img_arr = np.array(img).flatten()\n#         images.append(img)\n#     return images","metadata":{"execution":{"iopub.status.busy":"2024-06-15T14:22:47.711032Z","iopub.execute_input":"2024-06-15T14:22:47.711813Z","iopub.status.idle":"2024-06-15T14:22:47.716642Z","shell.execute_reply.started":"2024-06-15T14:22:47.711776Z","shell.execute_reply":"2024-06-15T14:22:47.715399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# path = '/kaggle/input/cassava-leaf-disease-classification/train_images/'\n# img_arr1 = images_to_array(path , 'a')\n# img_arr2 = images_to_array(path , 'b')\n# img_arr3 = images_to_array(path , 'c')\n# img_arr3 = images_to array(path , 'd')","metadata":{"execution":{"iopub.status.busy":"2024-06-15T14:22:48.311018Z","iopub.execute_input":"2024-06-15T14:22:48.311871Z","iopub.status.idle":"2024-06-15T14:22:48.316432Z","shell.execute_reply.started":"2024-06-15T14:22:48.311836Z","shell.execute_reply":"2024-06-15T14:22:48.315128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df = pd.DataFrame(img_arr)","metadata":{"execution":{"iopub.status.busy":"2024-06-15T14:22:48.905113Z","iopub.execute_input":"2024-06-15T14:22:48.905594Z","iopub.status.idle":"2024-06-15T14:22:48.910797Z","shell.execute_reply.started":"2024-06-15T14:22:48.905561Z","shell.execute_reply":"2024-06-15T14:22:48.909477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = []","metadata":{"execution":{"iopub.status.busy":"2024-06-15T14:40:19.934763Z","iopub.execute_input":"2024-06-15T14:40:19.935150Z","iopub.status.idle":"2024-06-15T14:40:19.940338Z","shell.execute_reply.started":"2024-06-15T14:40:19.935117Z","shell.execute_reply":"2024-06-15T14:40:19.939133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '/kaggle/input/cassava-leaf-disease-classification/train_images/'\nfor filename in os.listdir(path):\n    images.append(filename)\n   ","metadata":{"execution":{"iopub.status.busy":"2024-06-15T14:40:19.943484Z","iopub.execute_input":"2024-06-15T14:40:19.943955Z","iopub.status.idle":"2024-06-15T14:40:19.964802Z","shell.execute_reply.started":"2024-06-15T14:40:19.943919Z","shell.execute_reply":"2024-06-15T14:40:19.963432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame(images)","metadata":{"execution":{"iopub.status.busy":"2024-06-15T14:40:19.966362Z","iopub.execute_input":"2024-06-15T14:40:19.966709Z","iopub.status.idle":"2024-06-15T14:40:19.973082Z","shell.execute_reply.started":"2024-06-15T14:40:19.966681Z","shell.execute_reply":"2024-06-15T14:40:19.971918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_arr_list = []","metadata":{"execution":{"iopub.status.busy":"2024-06-15T14:40:19.974488Z","iopub.execute_input":"2024-06-15T14:40:19.974976Z","iopub.status.idle":"2024-06-15T14:40:19.983578Z","shell.execute_reply.started":"2024-06-15T14:40:19.974925Z","shell.execute_reply":"2024-06-15T14:40:19.981807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '/kaggle/input/cassava-leaf-disease-classification/train_images/'\ncount = 0\nfor img in images:\n    if(count <= 1000):\n        pathhh  = os.path.join(path , img)\n        im = Image.open(pathhh)\n        img_arr = np.array(im)\n        img_arr_list.append(img_arr)\n        \n        count +=1\n        \n\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2024-06-15T14:40:19.985210Z","iopub.execute_input":"2024-06-15T14:40:19.985601Z","iopub.status.idle":"2024-06-15T14:40:31.033662Z","shell.execute_reply.started":"2024-06-15T14:40:19.985564Z","shell.execute_reply":"2024-06-15T14:40:31.032375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_flatten1 = []","metadata":{"execution":{"iopub.status.busy":"2024-06-15T14:40:31.035291Z","iopub.execute_input":"2024-06-15T14:40:31.035688Z","iopub.status.idle":"2024-06-15T14:40:31.041060Z","shell.execute_reply.started":"2024-06-15T14:40:31.035649Z","shell.execute_reply":"2024-06-15T14:40:31.040069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count = 0\nfor i in img_arr_list:\n    k = i.flatten()\n    count += 1\n    img_flatten1.append(k)\n    \n        \n        \n    ","metadata":{"execution":{"iopub.status.busy":"2024-06-15T14:40:31.043687Z","iopub.execute_input":"2024-06-15T14:40:31.044084Z","iopub.status.idle":"2024-06-15T14:40:32.486232Z","shell.execute_reply.started":"2024-06-15T14:40:31.044055Z","shell.execute_reply":"2024-06-15T14:40:32.485154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# rescalling \nimg_flatten1 = np.array(img_flatten1)\nimg_flatten1 = (img_flatten1 / (1./255)).astype(np.uint8)","metadata":{"execution":{"iopub.status.busy":"2024-06-15T14:44:02.646681Z","iopub.execute_input":"2024-06-15T14:44:02.647694Z","iopub.status.idle":"2024-06-15T14:44:10.315851Z","shell.execute_reply.started":"2024-06-15T14:44:02.647640Z","shell.execute_reply":"2024-06-15T14:44:10.314364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nxtrain , xtest , ytrain , ytest = train_test_split(img_flatten1 , train.iloc[:1001,1].to_numpy() , test_size = 0.2 , random_state= 0)","metadata":{"execution":{"iopub.status.busy":"2024-06-15T14:44:20.225778Z","iopub.execute_input":"2024-06-15T14:44:20.227040Z","iopub.status.idle":"2024-06-15T14:44:22.177620Z","shell.execute_reply.started":"2024-06-15T14:44:20.226929Z","shell.execute_reply":"2024-06-15T14:44:22.176529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression","metadata":{"execution":{"iopub.status.busy":"2024-06-15T14:44:22.179575Z","iopub.execute_input":"2024-06-15T14:44:22.179920Z","iopub.status.idle":"2024-06-15T14:44:22.302680Z","shell.execute_reply.started":"2024-06-15T14:44:22.179890Z","shell.execute_reply":"2024-06-15T14:44:22.301500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr = LogisticRegression(multi_class = 'multinomial' , solver = 'lbfgs' , n_jobs = -1)","metadata":{"execution":{"iopub.status.busy":"2024-06-15T14:44:27.944771Z","iopub.execute_input":"2024-06-15T14:44:27.945845Z","iopub.status.idle":"2024-06-15T14:44:27.951871Z","shell.execute_reply.started":"2024-06-15T14:44:27.945801Z","shell.execute_reply":"2024-06-15T14:44:27.950266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr.fit(xtrain , ytrain)","metadata":{"execution":{"iopub.status.busy":"2024-06-15T14:44:29.883564Z","iopub.execute_input":"2024-06-15T14:44:29.884533Z","iopub.status.idle":"2024-06-15T14:55:45.366988Z","shell.execute_reply.started":"2024-06-15T14:44:29.884485Z","shell.execute_reply":"2024-06-15T14:55:45.365155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_preds = lr.predict(xtest)","metadata":{"execution":{"iopub.status.busy":"2024-06-15T14:59:05.729585Z","iopub.execute_input":"2024-06-15T14:59:05.730168Z","iopub.status.idle":"2024-06-15T14:59:06.838590Z","shell.execute_reply.started":"2024-06-15T14:59:05.730116Z","shell.execute_reply":"2024-06-15T14:59:06.837164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\naccuracy_score(ytest , y_preds)","metadata":{"execution":{"iopub.status.busy":"2024-06-15T14:59:07.824548Z","iopub.execute_input":"2024-06-15T14:59:07.825061Z","iopub.status.idle":"2024-06-15T14:59:07.836243Z","shell.execute_reply.started":"2024-06-15T14:59:07.825019Z","shell.execute_reply":"2024-06-15T14:59:07.834928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}