{"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 \nfrom sklearn.ensemble import RandomForestClassifier# data processing, CSV file I/O (e.g. pd.read_csv)\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\nimport cv2\nfrom sklearn.model_selection import RandomizedSearchCV\nfrom sklearn.metrics import accuracy_score\n\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\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-18T14:46:17.875663Z","iopub.execute_input":"2023-04-18T14:46:17.876521Z","iopub.status.idle":"2023-04-18T14:46:18.875704Z","shell.execute_reply.started":"2023-04-18T14:46:17.876473Z","shell.execute_reply":"2023-04-18T14:46:18.874468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install scikit-learn-intelex\n","metadata":{"execution":{"iopub.status.busy":"2023-04-18T14:46:18.878226Z","iopub.execute_input":"2023-04-18T14:46:18.879038Z","iopub.status.idle":"2023-04-18T14:46:30.491515Z","shell.execute_reply.started":"2023-04-18T14:46:18.878994Z","shell.execute_reply":"2023-04-18T14:46:30.490041Z"},"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-18T14:46:30.493219Z","iopub.execute_input":"2023-04-18T14:46:30.493983Z","iopub.status.idle":"2023-04-18T14:46:35.288242Z","shell.execute_reply.started":"2023-04-18T14:46:30.493934Z","shell.execute_reply":"2023-04-18T14:46:35.287078Z"},"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-18T14:46:35.290951Z","iopub.execute_input":"2023-04-18T14:46:35.291296Z","iopub.status.idle":"2023-04-18T14:46:35.400764Z","shell.execute_reply.started":"2023-04-18T14:46:35.291260Z","shell.execute_reply":"2023-04-18T14:46:35.399562Z"},"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-18T14:46:35.402419Z","iopub.execute_input":"2023-04-18T14:46:35.403145Z","iopub.status.idle":"2023-04-18T14:46:35.781388Z","shell.execute_reply.started":"2023-04-18T14:46:35.403104Z","shell.execute_reply":"2023-04-18T14:46:35.780367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import RandomizedSearchCV\n# Number of trees in random forest\nn_estimators = [int(x) for x in np.linspace(start = 200, stop = 2000, num = 10)]\n# Number of features to consider at every split\nmax_features = ['auto', 'sqrt','log2']\n# Maximum number of levels in tree\nmax_depth = [int(x) for x in np.linspace(10, 1000,10)]\n# Minimum number of samples required to split a node\nmin_samples_split = [2, 5, 10,14]\n# Minimum number of samples required at each leaf node\nmin_samples_leaf = [1, 2, 4,6,8]\n# Create the random grid\npar = {'n_estimators': n_estimators,\n               'max_features': max_features,\n               'max_depth': max_depth,\n               'min_samples_split': min_samples_split,\n               'min_samples_leaf': min_samples_leaf,\n              'criterion':['entropy','gini']}\nprint(par)","metadata":{"execution":{"iopub.status.busy":"2023-04-18T14:46:35.783095Z","iopub.execute_input":"2023-04-18T14:46:35.783526Z","iopub.status.idle":"2023-04-18T14:46:35.792526Z","shell.execute_reply.started":"2023-04-18T14:46:35.783485Z","shell.execute_reply":"2023-04-18T14:46:35.791392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rfc = RandomForestClassifier()\nrfc2=RandomizedSearchCV(estimator=rfc,param_distributions=par,n_iter=100,cv=3,verbose=2,\n                               random_state=100,n_jobs=-1)\nrfc2.fit(xtrain, ytrain)\n","metadata":{"execution":{"iopub.status.busy":"2023-04-18T14:46:35.794386Z","iopub.execute_input":"2023-04-18T14:46:35.795043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred=rfc2.predict(xval)\nrfc2.score(xval,yval)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(yval,pred)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file = '/kaggle/input/imgageee/9.png'\ntest_image = cv2.imread(file, cv2.IMREAD_GRAYSCALE)\n\nplt.imshow(test_image, cmap='gray')\n\n\nimg_resized = cv2.resize(test_image, (28, 28), interpolation=cv2.INTER_LINEAR)\nimg_resized = cv2.bitwise_not(img_resized)\n\nplt.imshow(img_resized, cmap='gray')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_resized","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_resized = np.array(img_resized)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_resized = img_resized.reshape(1,784)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_resized","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction2 = rfc2.predict(img_resized)\n\nprint('Predicted digit:', prediction2)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}