{"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":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30635,"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-01-19T13:41:35.590937Z","iopub.execute_input":"2024-01-19T13:41:35.591493Z","iopub.status.idle":"2024-01-19T13:41:35.599269Z","shell.execute_reply.started":"2024-01-19T13:41:35.591455Z","shell.execute_reply":"2024-01-19T13:41:35.598025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"execution":{"iopub.status.busy":"2024-01-19T13:41:35.601969Z","iopub.execute_input":"2024-01-19T13:41:35.603193Z","iopub.status.idle":"2024-01-19T13:41:38.500856Z","shell.execute_reply.started":"2024-01-19T13:41:35.603109Z","shell.execute_reply":"2024-01-19T13:41:38.499708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.sample(5)","metadata":{"execution":{"iopub.status.busy":"2024-01-19T13:41:38.502298Z","iopub.execute_input":"2024-01-19T13:41:38.503661Z","iopub.status.idle":"2024-01-19T13:41:38.525859Z","shell.execute_reply.started":"2024-01-19T13:41:38.503606Z","shell.execute_reply":"2024-01-19T13:41:38.524348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Spliting Data into train and Testsets","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_test,y_train,y_test=train_test_split(df.iloc[:,1:],df.iloc[:,0],test_size=0.2,random_state=2)","metadata":{"execution":{"iopub.status.busy":"2024-01-19T13:41:38.527249Z","iopub.execute_input":"2024-01-19T13:41:38.527582Z","iopub.status.idle":"2024-01-19T13:41:38.995869Z","shell.execute_reply.started":"2024-01-19T13:41:38.527553Z","shell.execute_reply":"2024-01-19T13:41:38.994783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nfrom sklearn.tree import DecisionTreeClassifier","metadata":{"execution":{"iopub.status.busy":"2024-01-19T13:43:07.347615Z","iopub.execute_input":"2024-01-19T13:43:07.348023Z","iopub.status.idle":"2024-01-19T13:43:07.352734Z","shell.execute_reply.started":"2024-01-19T13:43:07.347992Z","shell.execute_reply":"2024-01-19T13:43:07.351436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf1=LogisticRegression()\nclf2=DecisionTreeClassifier()\n","metadata":{"execution":{"iopub.status.busy":"2024-01-19T13:43:49.498782Z","iopub.execute_input":"2024-01-19T13:43:49.499264Z","iopub.status.idle":"2024-01-19T13:43:49.504188Z","shell.execute_reply.started":"2024-01-19T13:43:49.499222Z","shell.execute_reply":"2024-01-19T13:43:49.503255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf1.fit(X_train,y_train)\n","metadata":{"execution":{"iopub.status.busy":"2024-01-19T13:45:12.691145Z","iopub.execute_input":"2024-01-19T13:45:12.691603Z","iopub.status.idle":"2024-01-19T13:45:28.065593Z","shell.execute_reply.started":"2024-01-19T13:45:12.691569Z","shell.execute_reply":"2024-01-19T13:45:28.064347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf2.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2024-01-19T13:45:28.068356Z","iopub.execute_input":"2024-01-19T13:45:28.069403Z","iopub.status.idle":"2024-01-19T13:45:39.280263Z","shell.execute_reply.started":"2024-01-19T13:45:28.069344Z","shell.execute_reply":"2024-01-19T13:45:39.279025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred1=clf1.predict(X_test)\ny_pred2=clf2.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-01-19T13:46:22.105160Z","iopub.execute_input":"2024-01-19T13:46:22.105615Z","iopub.status.idle":"2024-01-19T13:46:22.231650Z","shell.execute_reply.started":"2024-01-19T13:46:22.105564Z","shell.execute_reply":"2024-01-19T13:46:22.230281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Accuracy as a classification matrics**","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score, confusion_matrix\nprint(\"Accuracy of LR\",accuracy_score(y_test,y_pred1))\nprint(\"Accuracy of DT\",accuracy_score(y_test,y_pred2))","metadata":{"execution":{"iopub.status.busy":"2024-01-19T13:48:16.459667Z","iopub.execute_input":"2024-01-19T13:48:16.460060Z","iopub.status.idle":"2024-01-19T13:48:16.470335Z","shell.execute_reply.started":"2024-01-19T13:48:16.460028Z","shell.execute_reply":"2024-01-19T13:48:16.469141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Confusion Matrix AS A Clssification Matrics**","metadata":{}},{"cell_type":"code","source":"print(\"LR confusion matrix\")\npd.DataFrame(confusion_matrix(y_test,y_pred1),columns=list(range(0,10)))","metadata":{"execution":{"iopub.status.busy":"2024-01-19T13:50:20.771814Z","iopub.execute_input":"2024-01-19T13:50:20.772293Z","iopub.status.idle":"2024-01-19T13:50:20.795240Z","shell.execute_reply.started":"2024-01-19T13:50:20.772256Z","shell.execute_reply":"2024-01-19T13:50:20.794390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"DT confusion matrix\")\npd.DataFrame(confusion_matrix(y_test,y_pred2),columns=list(range(0,10)))","metadata":{"execution":{"iopub.status.busy":"2024-01-19T13:52:03.450469Z","iopub.execute_input":"2024-01-19T13:52:03.451147Z","iopub.status.idle":"2024-01-19T13:52:03.473283Z","shell.execute_reply.started":"2024-01-19T13:52:03.451112Z","shell.execute_reply":"2024-01-19T13:52:03.471941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Precision,Recall and F1_score as a classification matrics**","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import precision_score, recall_score, f1_score","metadata":{"execution":{"iopub.status.busy":"2024-01-19T13:57:45.620456Z","iopub.execute_input":"2024-01-19T13:57:45.620908Z","iopub.status.idle":"2024-01-19T13:57:45.626878Z","shell.execute_reply.started":"2024-01-19T13:57:45.620873Z","shell.execute_reply":"2024-01-19T13:57:45.625711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(precision_score(y_test,y_pred1,average=None))\nprint(precision_score(y_test,y_pred1,average='macro'))\nprint(precision_score(y_test,y_pred1,average='weighted'))","metadata":{"execution":{"iopub.status.busy":"2024-01-19T14:03:29.648374Z","iopub.execute_input":"2024-01-19T14:03:29.649210Z","iopub.status.idle":"2024-01-19T14:03:29.676603Z","shell.execute_reply.started":"2024-01-19T14:03:29.649154Z","shell.execute_reply":"2024-01-19T14:03:29.675295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* In the above snipet when we assigned the value of average as None it will give us the precision_score of all classes; \n* In case of macro it gives the average of precision_scores of all classes\n* In case of weigthed it gives the weigthed precision_score","metadata":{}},{"cell_type":"code","source":"recall_score(y_test,y_pred1,average='weighted')","metadata":{"execution":{"iopub.status.busy":"2024-01-19T13:59:57.534342Z","iopub.execute_input":"2024-01-19T13:59:57.535303Z","iopub.status.idle":"2024-01-19T13:59:57.550796Z","shell.execute_reply.started":"2024-01-19T13:59:57.535253Z","shell.execute_reply":"2024-01-19T13:59:57.549651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"f1 score is a harmonic mean of both precision and recall score and its value is always close to the smaller value among the two.","metadata":{}},{"cell_type":"code","source":"f1_score(y_test,y_pred1,average='weighted')","metadata":{"execution":{"iopub.status.busy":"2024-01-19T13:59:25.384272Z","iopub.execute_input":"2024-01-19T13:59:25.385407Z","iopub.status.idle":"2024-01-19T13:59:25.399933Z","shell.execute_reply.started":"2024-01-19T13:59:25.385349Z","shell.execute_reply":"2024-01-19T13:59:25.398641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report\nprint(classification_report(y_test,y_pred1))","metadata":{"execution":{"iopub.status.busy":"2024-01-19T14:07:50.058528Z","iopub.execute_input":"2024-01-19T14:07:50.059669Z","iopub.status.idle":"2024-01-19T14:07:50.092562Z","shell.execute_reply.started":"2024-01-19T14:07:50.059618Z","shell.execute_reply":"2024-01-19T14:07:50.091446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"classification report class will give all the info in one go here support represents the no. of entries in that perticular class","metadata":{}}]}