{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n%matplotlib inline\nfrom xgboost import XGBClassifier","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-01-09T13:57:03.783975Z","iopub.execute_input":"2023-01-09T13:57:03.78455Z","iopub.status.idle":"2023-01-09T13:57:04.376625Z","shell.execute_reply.started":"2023-01-09T13:57:03.784508Z","shell.execute_reply":"2023-01-09T13:57:04.375752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2023-01-09T13:57:06.353964Z","iopub.execute_input":"2023-01-09T13:57:06.35435Z","iopub.status.idle":"2023-01-09T13:57:06.361877Z","shell.execute_reply.started":"2023-01-09T13:57:06.354323Z","shell.execute_reply":"2023-01-09T13:57:06.360893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#load dataset\ntrain = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')\ntest = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')\nsample = pd.read_csv('/kaggle/input/Kannada-MNIST/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-01-09T10:35:00.12649Z","iopub.execute_input":"2023-01-09T10:35:00.127437Z","iopub.status.idle":"2023-01-09T10:35:05.980734Z","shell.execute_reply.started":"2023-01-09T10:35:00.12739Z","shell.execute_reply":"2023-01-09T10:35:05.979473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2023-01-09T10:35:07.335299Z","iopub.execute_input":"2023-01-09T10:35:07.335706Z","iopub.status.idle":"2023-01-09T10:35:07.371021Z","shell.execute_reply.started":"2023-01-09T10:35:07.335669Z","shell.execute_reply":"2023-01-09T10:35:07.36985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2023-01-09T10:35:09.00032Z","iopub.execute_input":"2023-01-09T10:35:09.001338Z","iopub.status.idle":"2023-01-09T10:35:09.026816Z","shell.execute_reply.started":"2023-01-09T10:35:09.001279Z","shell.execute_reply":"2023-01-09T10:35:09.025225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample","metadata":{"execution":{"iopub.status.busy":"2023-01-09T10:35:10.38183Z","iopub.execute_input":"2023-01-09T10:35:10.382254Z","iopub.status.idle":"2023-01-09T10:35:10.394244Z","shell.execute_reply.started":"2023-01-09T10:35:10.382212Z","shell.execute_reply":"2023-01-09T10:35:10.393217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2023-01-09T10:35:12.192648Z","iopub.execute_input":"2023-01-09T10:35:12.193568Z","iopub.status.idle":"2023-01-09T10:35:12.248797Z","shell.execute_reply.started":"2023-01-09T10:35:12.193388Z","shell.execute_reply":"2023-01-09T10:35:12.24758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.info()","metadata":{"execution":{"iopub.status.busy":"2023-01-09T10:35:13.600489Z","iopub.execute_input":"2023-01-09T10:35:13.600917Z","iopub.status.idle":"2023-01-09T10:35:13.652414Z","shell.execute_reply.started":"2023-01-09T10:35:13.60088Z","shell.execute_reply":"2023-01-09T10:35:13.651193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Check null\ntrain.isnull().sum().sum(), test.isnull().sum().sum()","metadata":{"execution":{"iopub.status.busy":"2023-01-09T10:35:14.991365Z","iopub.execute_input":"2023-01-09T10:35:14.992617Z","iopub.status.idle":"2023-01-09T10:35:15.094608Z","shell.execute_reply.started":"2023-01-09T10:35:14.992561Z","shell.execute_reply":"2023-01-09T10:35:15.093449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Model\nclf=XGBClassifier()","metadata":{"execution":{"iopub.status.busy":"2023-01-09T10:35:20.070853Z","iopub.execute_input":"2023-01-09T10:35:20.071237Z","iopub.status.idle":"2023-01-09T10:35:20.075637Z","shell.execute_reply.started":"2023-01-09T10:35:20.071204Z","shell.execute_reply":"2023-01-09T10:35:20.07452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Set training file lengths\ndt = train.values\ndt = train.astype('float')\ntrain_size = int(len(dt) * 0.5)","metadata":{"execution":{"iopub.status.busy":"2023-01-09T12:07:54.779467Z","iopub.execute_input":"2023-01-09T12:07:54.779881Z","iopub.status.idle":"2023-01-09T12:07:54.914993Z","shell.execute_reply.started":"2023-01-09T12:07:54.779845Z","shell.execute_reply":"2023-01-09T12:07:54.91377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Training dataset lengths\nxtrain=train.iloc[0:train_size,1:]\ntrain_label=train.iloc[0:train_size,0]","metadata":{"execution":{"iopub.status.busy":"2023-01-09T12:07:59.287046Z","iopub.execute_input":"2023-01-09T12:07:59.287459Z","iopub.status.idle":"2023-01-09T12:07:59.295623Z","shell.execute_reply.started":"2023-01-09T12:07:59.287425Z","shell.execute_reply":"2023-01-09T12:07:59.292979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Fit model\nclf.fit(xtrain,train_label)","metadata":{"execution":{"iopub.status.busy":"2023-01-09T12:08:00.347291Z","iopub.execute_input":"2023-01-09T12:08:00.347736Z","iopub.status.idle":"2023-01-09T12:15:06.831434Z","shell.execute_reply.started":"2023-01-09T12:08:00.347692Z","shell.execute_reply":"2023-01-09T12:15:06.830179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Testing data lengths\nxtest=train.iloc[train_size:,1:]\nactual_label=train.iloc[train_size,0]","metadata":{"execution":{"iopub.status.busy":"2023-01-09T10:51:11.437528Z","iopub.execute_input":"2023-01-09T10:51:11.437991Z","iopub.status.idle":"2023-01-09T10:51:11.445025Z","shell.execute_reply.started":"2023-01-09T10:51:11.437956Z","shell.execute_reply":"2023-01-09T10:51:11.443737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plot image\n#d=xtest.iloc[8]\n#d=d.values.reshape(28,28)\n#plt.imshow(255-d,cmap='gray')\n#print(clf.predict([xtest[8]]))\n#plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-09T11:05:34.847655Z","iopub.execute_input":"2023-01-09T11:05:34.848057Z","iopub.status.idle":"2023-01-09T11:05:34.854445Z","shell.execute_reply.started":"2023-01-09T11:05:34.848025Z","shell.execute_reply":"2023-01-09T11:05:34.852981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p=clf.predict(xtest)","metadata":{"execution":{"iopub.status.busy":"2023-01-09T10:53:09.934607Z","iopub.execute_input":"2023-01-09T10:53:09.935073Z","iopub.status.idle":"2023-01-09T10:53:10.53141Z","shell.execute_reply.started":"2023-01-09T10:53:09.935036Z","shell.execute_reply":"2023-01-09T10:53:10.530395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p","metadata":{"execution":{"iopub.status.busy":"2023-01-09T10:53:19.363527Z","iopub.execute_input":"2023-01-09T10:53:19.364002Z","iopub.status.idle":"2023-01-09T10:53:19.374293Z","shell.execute_reply.started":"2023-01-09T10:53:19.363965Z","shell.execute_reply":"2023-01-09T10:53:19.372351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-09T10:53:30.206204Z","iopub.execute_input":"2023-01-09T10:53:30.206858Z","iopub.status.idle":"2023-01-09T10:53:30.213696Z","shell.execute_reply.started":"2023-01-09T10:53:30.206823Z","shell.execute_reply":"2023-01-09T10:53:30.212351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count=0\nfor i in range(0,train_size):\n    count+=1 if p[i]==actual_label else 0\nprint(\"Accuracy= \", (count/train_size)*100)","metadata":{"execution":{"iopub.status.busy":"2023-01-09T10:53:39.746273Z","iopub.execute_input":"2023-01-09T10:53:39.746714Z","iopub.status.idle":"2023-01-09T10:53:39.763388Z","shell.execute_reply.started":"2023-01-09T10:53:39.746681Z","shell.execute_reply":"2023-01-09T10:53:39.76167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Set test file lengths\ntt = test.values\ntt = test.astype('int64')\ntest_size = int(len(tt))","metadata":{"execution":{"iopub.status.busy":"2023-01-09T11:59:13.397957Z","iopub.execute_input":"2023-01-09T11:59:13.398375Z","iopub.status.idle":"2023-01-09T11:59:13.411094Z","shell.execute_reply.started":"2023-01-09T11:59:13.398342Z","shell.execute_reply":"2023-01-09T11:59:13.409203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Testing data lengths\nxtest1=test.iloc[test_size:,0:]","metadata":{"execution":{"iopub.status.busy":"2023-01-09T11:59:50.916452Z","iopub.execute_input":"2023-01-09T11:59:50.916845Z","iopub.status.idle":"2023-01-09T11:59:50.923135Z","shell.execute_reply.started":"2023-01-09T11:59:50.916814Z","shell.execute_reply":"2023-01-09T11:59:50.92153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Predict on test set\np1=clf.predict(test)\np1","metadata":{"execution":{"iopub.status.busy":"2023-01-09T13:34:20.015031Z","iopub.status.idle":"2023-01-09T13:34:20.015366Z","shell.execute_reply.started":"2023-01-09T13:34:20.015202Z","shell.execute_reply":"2023-01-09T13:34:20.015216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p1.shape","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({\n        \"ImageId\": test.index+1,\n        \"Label\": p1\n    })\nsubmission.to_csv('submission.csv', index=False)\nsubmission","metadata":{},"execution_count":null,"outputs":[]}]}