{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30761,"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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-08-30T17:01:28.689464Z","iopub.execute_input":"2024-08-30T17:01:28.689914Z","iopub.status.idle":"2024-08-30T17:01:28.698360Z","shell.execute_reply.started":"2024-08-30T17:01:28.689868Z","shell.execute_reply":"2024-08-30T17:01:28.697073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2024-08-30T17:01:28.700253Z","iopub.execute_input":"2024-08-30T17:01:28.700624Z","iopub.status.idle":"2024-08-30T17:01:32.571459Z","shell.execute_reply.started":"2024-08-30T17:01:28.700575Z","shell.execute_reply":"2024-08-30T17:01:32.570333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.metrics import accuracy_score, confusion_matrix, precision_score, recall_score, ConfusionMatrixDisplay, classification_report\nfrom sklearn.model_selection import RandomizedSearchCV, train_test_split","metadata":{"execution":{"iopub.status.busy":"2024-08-30T17:01:32.572912Z","iopub.execute_input":"2024-08-30T17:01:32.573306Z","iopub.status.idle":"2024-08-30T17:01:32.578857Z","shell.execute_reply.started":"2024-08-30T17:01:32.573268Z","shell.execute_reply":"2024-08-30T17:01:32.577751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2024-08-30T17:01:32.581303Z","iopub.execute_input":"2024-08-30T17:01:32.581664Z","iopub.status.idle":"2024-08-30T17:01:32.603026Z","shell.execute_reply.started":"2024-08-30T17:01:32.581626Z","shell.execute_reply":"2024-08-30T17:01:32.601741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2024-08-30T17:01:32.604581Z","iopub.execute_input":"2024-08-30T17:01:32.604988Z","iopub.status.idle":"2024-08-30T17:01:32.627308Z","shell.execute_reply.started":"2024-08-30T17:01:32.604945Z","shell.execute_reply":"2024-08-30T17:01:32.625927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y=list(train['label'])","metadata":{"execution":{"iopub.status.busy":"2024-08-30T17:01:32.628692Z","iopub.execute_input":"2024-08-30T17:01:32.629064Z","iopub.status.idle":"2024-08-30T17:01:32.644666Z","shell.execute_reply.started":"2024-08-30T17:01:32.629020Z","shell.execute_reply":"2024-08-30T17:01:32.643434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y[:5])\n","metadata":{"execution":{"iopub.status.busy":"2024-08-30T17:01:32.646051Z","iopub.execute_input":"2024-08-30T17:01:32.646395Z","iopub.status.idle":"2024-08-30T17:01:32.658476Z","shell.execute_reply.started":"2024-08-30T17:01:32.646354Z","shell.execute_reply":"2024-08-30T17:01:32.657168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=train.drop('label',axis=1)\nX=X/255.0","metadata":{"execution":{"iopub.status.busy":"2024-08-30T17:01:32.660189Z","iopub.execute_input":"2024-08-30T17:01:32.660567Z","iopub.status.idle":"2024-08-30T17:01:32.832120Z","shell.execute_reply.started":"2024-08-30T17:01:32.660527Z","shell.execute_reply":"2024-08-30T17:01:32.831071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=RandomForestClassifier()","metadata":{"execution":{"iopub.status.busy":"2024-08-30T17:01:32.867929Z","iopub.execute_input":"2024-08-30T17:01:32.868548Z","iopub.status.idle":"2024-08-30T17:01:32.873507Z","shell.execute_reply.started":"2024-08-30T17:01:32.868495Z","shell.execute_reply":"2024-08-30T17:01:32.872357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-08-30T17:01:32.874729Z","iopub.execute_input":"2024-08-30T17:01:32.875115Z","iopub.status.idle":"2024-08-30T17:01:33.091691Z","shell.execute_reply.started":"2024-08-30T17:01:32.875079Z","shell.execute_reply":"2024-08-30T17:01:33.090442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2024-08-30T17:01:33.093013Z","iopub.execute_input":"2024-08-30T17:01:33.093350Z","iopub.status.idle":"2024-08-30T17:01:59.475830Z","shell.execute_reply.started":"2024-08-30T17:01:33.093316Z","shell.execute_reply":"2024-08-30T17:01:59.474666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=model.predict(X_val)","metadata":{"execution":{"iopub.status.busy":"2024-08-30T17:01:59.477074Z","iopub.execute_input":"2024-08-30T17:01:59.477390Z","iopub.status.idle":"2024-08-30T17:01:59.833861Z","shell.execute_reply.started":"2024-08-30T17:01:59.477358Z","shell.execute_reply":"2024-08-30T17:01:59.832627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy = accuracy_score(y_val, y_pred)","metadata":{"execution":{"iopub.status.busy":"2024-08-30T17:01:59.835114Z","iopub.execute_input":"2024-08-30T17:01:59.835435Z","iopub.status.idle":"2024-08-30T17:01:59.844082Z","shell.execute_reply.started":"2024-08-30T17:01:59.835402Z","shell.execute_reply":"2024-08-30T17:01:59.842952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(accuracy)","metadata":{"execution":{"iopub.status.busy":"2024-08-30T17:01:59.845526Z","iopub.execute_input":"2024-08-30T17:01:59.845884Z","iopub.status.idle":"2024-08-30T17:01:59.852729Z","shell.execute_reply.started":"2024-08-30T17:01:59.845838Z","shell.execute_reply":"2024-08-30T17:01:59.851744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Display a classification report\nprint(classification_report(y_val, y_pred))\n\n# Display the confusion matrix\nprint(confusion_matrix(y_val, y_pred))","metadata":{"execution":{"iopub.status.busy":"2024-08-30T17:01:59.854263Z","iopub.execute_input":"2024-08-30T17:01:59.854633Z","iopub.status.idle":"2024-08-30T17:01:59.897622Z","shell.execute_reply.started":"2024-08-30T17:01:59.854595Z","shell.execute_reply":"2024-08-30T17:01:59.896399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}