{"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":"# 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)\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.model_selection import cross_val_score, train_test_split\nimport cv2\nimport matplotlib.pyplot as plt\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":"2023-04-05T10:00:05.599424Z","iopub.execute_input":"2023-04-05T10:00:05.599912Z","iopub.status.idle":"2023-04-05T10:00:05.615818Z","shell.execute_reply.started":"2023-04-05T10:00:05.599877Z","shell.execute_reply":"2023-04-05T10:00:05.614436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest_data = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-04-05T10:00:05.618403Z","iopub.execute_input":"2023-04-05T10:00:05.619209Z","iopub.status.idle":"2023-04-05T10:00:09.988613Z","shell.execute_reply.started":"2023-04-05T10:00:05.619157Z","shell.execute_reply":"2023-04-05T10:00:09.987269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train_data.to_numpy()\nytrain = train[:, 0]\nxtrain = train[:, 1:]\nxtrain = xtrain / 255","metadata":{"execution":{"iopub.status.busy":"2023-04-05T10:00:09.990876Z","iopub.execute_input":"2023-04-05T10:00:09.991357Z","iopub.status.idle":"2023-04-05T10:00:10.592561Z","shell.execute_reply.started":"2023-04-05T10:00:09.991309Z","shell.execute_reply":"2023-04-05T10:00:10.591345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xtrain, xval, ytrain, yval = train_test_split(\n    xtrain, ytrain, test_size=0.2, random_state=42,   \n)","metadata":{"execution":{"iopub.status.busy":"2023-04-05T10:00:10.595079Z","iopub.execute_input":"2023-04-05T10:00:10.595481Z","iopub.status.idle":"2023-04-05T10:00:11.744854Z","shell.execute_reply.started":"2023-04-05T10:00:10.595444Z","shell.execute_reply":"2023-04-05T10:00:11.743476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mymodel=RandomForestClassifier(n_estimators=100,random_state=0)\nmymodel.fit(xtrain,ytrain)\npred=mymodel.predict(xval)","metadata":{"execution":{"iopub.status.busy":"2023-04-05T10:01:35.432740Z","iopub.execute_input":"2023-04-05T10:01:35.433682Z","iopub.status.idle":"2023-04-05T10:02:03.431166Z","shell.execute_reply.started":"2023-04-05T10:01:35.433643Z","shell.execute_reply":"2023-04-05T10:02:03.429767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mymodel.score(xval,yval)","metadata":{"execution":{"iopub.status.busy":"2023-04-05T10:00:38.405370Z","iopub.execute_input":"2023-04-05T10:00:38.405795Z","iopub.status.idle":"2023-04-05T10:00:38.798732Z","shell.execute_reply.started":"2023-04-05T10:00:38.405751Z","shell.execute_reply":"2023-04-05T10:00:38.797770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ypred=mymodel.predict(test_data)","metadata":{"execution":{"iopub.status.busy":"2023-04-05T10:00:38.800016Z","iopub.execute_input":"2023-04-05T10:00:38.800543Z","iopub.status.idle":"2023-04-05T10:00:40.063578Z","shell.execute_reply.started":"2023-04-05T10:00:38.800510Z","shell.execute_reply":"2023-04-05T10:00:40.062316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.shape","metadata":{"execution":{"iopub.status.busy":"2023-04-05T10:00:40.065178Z","iopub.execute_input":"2023-04-05T10:00:40.065882Z","iopub.status.idle":"2023-04-05T10:00:40.074581Z","shell.execute_reply.started":"2023-04-05T10:00:40.065835Z","shell.execute_reply":"2023-04-05T10:00:40.073257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ypred\nypred = pd.Series(ypred,name=\"Label\")","metadata":{"execution":{"iopub.status.busy":"2023-04-05T10:00:40.079126Z","iopub.execute_input":"2023-04-05T10:00:40.079564Z","iopub.status.idle":"2023-04-05T10:00:40.088389Z","shell.execute_reply.started":"2023-04-05T10:00:40.079518Z","shell.execute_reply":"2023-04-05T10:00:40.086886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),ypred],axis = 1)\n\nsubmission.to_csv(\"mysubmission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2023-04-05T10:00:40.090046Z","iopub.execute_input":"2023-04-05T10:00:40.090446Z","iopub.status.idle":"2023-04-05T10:00:40.132372Z","shell.execute_reply.started":"2023-04-05T10:00:40.090411Z","shell.execute_reply":"2023-04-05T10:00:40.131328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import Image\nimport os\n!ls ../input/imgageee\n","metadata":{"execution":{"iopub.status.busy":"2023-04-05T10:00:40.133872Z","iopub.execute_input":"2023-04-05T10:00:40.134224Z","iopub.status.idle":"2023-04-05T10:00:41.245671Z","shell.execute_reply.started":"2023-04-05T10:00:40.134186Z","shell.execute_reply":"2023-04-05T10:00:41.243907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mypic = Image('/kaggle/input/imgageee/num8.png')","metadata":{"execution":{"iopub.status.busy":"2023-04-05T10:00:41.248938Z","iopub.execute_input":"2023-04-05T10:00:41.249475Z","iopub.status.idle":"2023-04-05T10:00:41.258630Z","shell.execute_reply.started":"2023-04-05T10:00:41.249429Z","shell.execute_reply":"2023-04-05T10:00:41.256998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nfrom numpy import asarray\nmerapic=Image.open('/kaggle/input/imgageee/num8.png')\nnumpydata = asarray(merapic)\n# data\nprint(numpydata)","metadata":{"execution":{"iopub.status.busy":"2023-04-05T10:00:41.260857Z","iopub.execute_input":"2023-04-05T10:00:41.261349Z","iopub.status.idle":"2023-04-05T10:00:41.272877Z","shell.execute_reply.started":"2023-04-05T10:00:41.261308Z","shell.execute_reply":"2023-04-05T10:00:41.271596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"numpydata.shape","metadata":{"execution":{"iopub.status.busy":"2023-04-05T10:00:41.275835Z","iopub.execute_input":"2023-04-05T10:00:41.276580Z","iopub.status.idle":"2023-04-05T10:00:41.289655Z","shell.execute_reply.started":"2023-04-05T10:00:41.276540Z","shell.execute_reply":"2023-04-05T10:00:41.288269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nsamples, nx, ny = numpydata.shape\nd2_numpydata = numpydata.reshape((4,nsamples*nsamples))","metadata":{"execution":{"iopub.status.busy":"2023-04-05T10:00:41.292336Z","iopub.execute_input":"2023-04-05T10:00:41.292709Z","iopub.status.idle":"2023-04-05T10:00:41.299128Z","shell.execute_reply.started":"2023-04-05T10:00:41.292676Z","shell.execute_reply":"2023-04-05T10:00:41.298286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"d2_numpydata.shape","metadata":{"execution":{"iopub.status.busy":"2023-04-05T10:00:41.300441Z","iopub.execute_input":"2023-04-05T10:00:41.300977Z","iopub.status.idle":"2023-04-05T10:00:41.314070Z","shell.execute_reply.started":"2023-04-05T10:00:41.300944Z","shell.execute_reply":"2023-04-05T10:00:41.312801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resized_img=cv2.resize(d2_numpydata, (28,28))","metadata":{"execution":{"iopub.status.busy":"2023-04-05T10:00:41.315395Z","iopub.execute_input":"2023-04-05T10:00:41.316034Z","iopub.status.idle":"2023-04-05T10:00:41.324886Z","shell.execute_reply.started":"2023-04-05T10:00:41.316001Z","shell.execute_reply":"2023-04-05T10:00:41.323797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"d2_numpydata.shape","metadata":{"execution":{"iopub.status.busy":"2023-04-05T10:00:41.326490Z","iopub.execute_input":"2023-04-05T10:00:41.327161Z","iopub.status.idle":"2023-04-05T10:00:41.338860Z","shell.execute_reply.started":"2023-04-05T10:00:41.327126Z","shell.execute_reply":"2023-04-05T10:00:41.337525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resized_img.shape","metadata":{"execution":{"iopub.status.busy":"2023-04-05T10:00:41.341676Z","iopub.execute_input":"2023-04-05T10:00:41.342171Z","iopub.status.idle":"2023-04-05T10:00:41.351135Z","shell.execute_reply.started":"2023-04-05T10:00:41.342124Z","shell.execute_reply":"2023-04-05T10:00:41.349777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"numpydata2 = asarray(resized_img)","metadata":{"execution":{"iopub.status.busy":"2023-04-05T10:00:41.352569Z","iopub.execute_input":"2023-04-05T10:00:41.352919Z","iopub.status.idle":"2023-04-05T10:00:41.362066Z","shell.execute_reply.started":"2023-04-05T10:00:41.352887Z","shell.execute_reply":"2023-04-05T10:00:41.360968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgpred=mymodel.predict(d2_numpydata)\nimgpred= pd.Series(imgpred,name=\"Answer\")\n","metadata":{"execution":{"iopub.status.busy":"2023-04-05T10:00:41.363724Z","iopub.execute_input":"2023-04-05T10:00:41.364501Z","iopub.status.idle":"2023-04-05T10:00:41.390727Z","shell.execute_reply.started":"2023-04-05T10:00:41.364462Z","shell.execute_reply":"2023-04-05T10:00:41.389300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgpred","metadata":{"execution":{"iopub.status.busy":"2023-04-05T10:00:41.392012Z","iopub.execute_input":"2023-04-05T10:00:41.392625Z","iopub.status.idle":"2023-04-05T10:00:41.401217Z","shell.execute_reply.started":"2023-04-05T10:00:41.392589Z","shell.execute_reply":"2023-04-05T10:00:41.400082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.open('/kaggle/input/imgageee/num8100.png').convert('L')\nimg = img.resize((28, 28))\nimg = np.array(img)\nimg = img.reshape(1, -1) / 255.0","metadata":{"execution":{"iopub.status.busy":"2023-04-05T10:02:14.460108Z","iopub.execute_input":"2023-04-05T10:02:14.460623Z","iopub.status.idle":"2023-04-05T10:02:14.468334Z","shell.execute_reply.started":"2023-04-05T10:02:14.460580Z","shell.execute_reply":"2023-04-05T10:02:14.466869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgpred2=mymodel.predict(img)\nimgpred2= pd.Series(imgpred2,name=\"Answer\")\n","metadata":{"execution":{"iopub.status.busy":"2023-04-05T10:02:15.938699Z","iopub.execute_input":"2023-04-05T10:02:15.940061Z","iopub.status.idle":"2023-04-05T10:02:15.962370Z","shell.execute_reply.started":"2023-04-05T10:02:15.940007Z","shell.execute_reply":"2023-04-05T10:02:15.960642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Predicted digit:', imgpred2[0])","metadata":{"execution":{"iopub.status.busy":"2023-04-05T10:02:18.590484Z","iopub.execute_input":"2023-04-05T10:02:18.591007Z","iopub.status.idle":"2023-04-05T10:02:18.599411Z","shell.execute_reply.started":"2023-04-05T10:02:18.590963Z","shell.execute_reply":"2023-04-05T10:02:18.597354Z"},"trusted":true},"execution_count":null,"outputs":[]}]}