{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[],"dockerImageVersionId":28755,"isInternetEnabled":false,"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\n# Use the kagglehub client library to attach Kaggle resources like competitions, datasets, and models to your session\n# Learn more about kagglehub: https://github.com/Kaggle/kagglehub/blob/main/README.md\n\nimport kagglehub\n# kagglehub.dataset_download('<owner>/<dataset-slug>')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-06-28T13:20:49.347371Z","iopub.execute_input":"2026-06-28T13:20:49.347747Z","iopub.status.idle":"2026-06-28T13:20:50.198529Z","shell.execute_reply.started":"2026-06-28T13:20:49.347687Z","shell.execute_reply":"2026-06-28T13:20:50.197453Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df=pd.read_csv(\"/kaggle/input/competitions/digit-recognizer/train.csv\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-06-28T13:20:50.200383Z","iopub.execute_input":"2026-06-28T13:20:50.200900Z","iopub.status.idle":"2026-06-28T13:20:52.659302Z","shell.execute_reply.started":"2026-06-28T13:20:50.200869Z","shell.execute_reply":"2026-06-28T13:20:52.658210Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(df.shape)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-06-28T13:20:52.660534Z","iopub.execute_input":"2026-06-28T13:20:52.660917Z","iopub.status.idle":"2026-06-28T13:20:52.666268Z","shell.execute_reply.started":"2026-06-28T13:20:52.660868Z","shell.execute_reply":"2026-06-28T13:20:52.664731Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-06-28T13:20:52.667528Z","iopub.execute_input":"2026-06-28T13:20:52.668678Z","iopub.status.idle":"2026-06-28T13:20:52.698420Z","shell.execute_reply.started":"2026-06-28T13:20:52.668634Z","shell.execute_reply":"2026-06-28T13:20:52.697464Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x=df.iloc[:,1:]\ny=df.iloc[:,0]\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-06-28T13:20:52.702626Z","iopub.execute_input":"2026-06-28T13:20:52.703215Z","iopub.status.idle":"2026-06-28T13:20:52.710116Z","shell.execute_reply.started":"2026-06-28T13:20:52.703170Z","shell.execute_reply":"2026-06-28T13:20:52.709030Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(x.shape)\nprint(y.shape)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-06-28T13:20:52.711847Z","iopub.execute_input":"2026-06-28T13:20:52.712509Z","iopub.status.idle":"2026-06-28T13:20:52.729564Z","shell.execute_reply.started":"2026-06-28T13:20:52.712466Z","shell.execute_reply":"2026-06-28T13:20:52.728555Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# validation split from train set for hyperparameter tuning","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx_temp,x_test,y_temp,y_test=train_test_split(x,y,test_size=0.15,random_state=42)\n\nx_train,x_val,y_train,y_val=train_test_split(x_temp,y_temp,test_size=0.2,random_state=42)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-06-28T13:20:52.730966Z","iopub.execute_input":"2026-06-28T13:20:52.731325Z","iopub.status.idle":"2026-06-28T13:20:53.951988Z","shell.execute_reply.started":"2026-06-28T13:20:52.731287Z","shell.execute_reply":"2026-06-28T13:20:53.951017Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.tree import DecisionTreeClassifier\ndt=DecisionTreeClassifier()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-06-28T13:20:53.953167Z","iopub.execute_input":"2026-06-28T13:20:53.953678Z","iopub.status.idle":"2026-06-28T13:20:54.034663Z","shell.execute_reply.started":"2026-06-28T13:20:53.953635Z","shell.execute_reply":"2026-06-28T13:20:54.033500Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import GridSearchCV\nparams={\n    \"criterion\":[\"entropy\",\"gini\"],\n    \"max_depth\":list(range(5,15))\n}\n\ngridSearch=GridSearchCV(dt,params,cv=10,n_jobs=-1)\ngridSearch.fit(x_val,y_val)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-06-28T13:20:54.035958Z","iopub.execute_input":"2026-06-28T13:20:54.036494Z","iopub.status.idle":"2026-06-28T13:22:57.199145Z","shell.execute_reply.started":"2026-06-28T13:20:54.036464Z","shell.execute_reply":"2026-06-28T13:22:57.198124Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"gridSearch.best_estimator_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-06-28T13:22:57.200325Z","iopub.execute_input":"2026-06-28T13:22:57.200649Z","iopub.status.idle":"2026-06-28T13:22:57.207504Z","shell.execute_reply.started":"2026-06-28T13:22:57.200612Z","shell.execute_reply":"2026-06-28T13:22:57.206675Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(gridSearch.best_score_)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-06-28T13:24:47.288825Z","iopub.status.idle":"2026-06-28T13:24:47.289177Z","shell.execute_reply.started":"2026-06-28T13:24:47.289025Z","shell.execute_reply":"2026-06-28T13:24:47.289046Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dt=DecisionTreeClassifier(criterion=\"entropy\",max_depth=14)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-06-28T13:22:57.208687Z","iopub.execute_input":"2026-06-28T13:22:57.209393Z","iopub.status.idle":"2026-06-28T13:22:57.224106Z","shell.execute_reply.started":"2026-06-28T13:22:57.209361Z","shell.execute_reply":"2026-06-28T13:22:57.223024Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dt.fit(x_train,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-06-28T13:22:57.225234Z","iopub.execute_input":"2026-06-28T13:22:57.226232Z","iopub.status.idle":"2026-06-28T13:23:06.252649Z","shell.execute_reply.started":"2026-06-28T13:22:57.226200Z","shell.execute_reply":"2026-06-28T13:23:06.251565Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred=dt.predict(x_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-06-28T13:23:06.253869Z","iopub.execute_input":"2026-06-28T13:23:06.254217Z","iopub.status.idle":"2026-06-28T13:23:06.276259Z","shell.execute_reply.started":"2026-06-28T13:23:06.254190Z","shell.execute_reply":"2026-06-28T13:23:06.275062Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\nprint(accuracy_score(y_pred,y_test))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-06-28T13:23:06.279360Z","iopub.execute_input":"2026-06-28T13:23:06.280009Z","iopub.status.idle":"2026-06-28T13:23:06.286791Z","shell.execute_reply.started":"2026-06-28T13:23:06.279977Z","shell.execute_reply":"2026-06-28T13:23:06.285657Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.tree import plot_tree\nplot_tree(dt)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-06-28T13:23:06.287949Z","iopub.execute_input":"2026-06-28T13:23:06.288239Z","iopub.status.idle":"2026-06-28T13:24:47.236655Z","shell.execute_reply.started":"2026-06-28T13:23:06.288213Z","shell.execute_reply":"2026-06-28T13:24:47.235769Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"raw","source":"","metadata":{}},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"raw","source":"print(y1_pred)","metadata":{}}]}