{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Digit recognizer based on Machine Learning"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom sklearn.model_selection import train_test_split\nfrom sklearn import svm\nfrom matplotlib import pyplot as plt\nfrom sklearn.ensemble import RandomForestClassifier","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"#load data\ndata = pd.read_csv('../input/digit-recognizer/train.csv')\ndata.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#split data to labels and features\nlabels = data.iloc[:30000,0]\ntrain = data.iloc[:30000,1:]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#SVM\nx_train,x_test,y_train,y_test = train_test_split(train,labels,random_state=4,test_size=0.2)\nmodel = svm.SVC(C=1,kernel=\"linear\",degree=3,gamma=\"auto\")\nmodel.fit(x_train,y_train)\nprint(\"SVM accuracy :\",model.score(x_test,y_test)*100,\"%\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Random Forest\nmodel_ = RandomForestClassifier(n_estimators=100)\nmodel_.fit(x_train,y_train)\nprint(\"Random Forest accuracy :\",model_.score(x_test,y_test)*100,\"%\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#testing data\ntest_data = pd.read_csv('../input/digit-recognizer/test.csv')\nimage_ = test_data.iloc[4327,:].values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#reshape image test\nimage = image_.reshape(28,28)\nimage.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#show image\nplt.imshow(image)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#SVM prediction\nprint(\"predicted number is :\",model.predict(image.reshape(1,-1))[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Random Forest prediction\nprint(\"predicted number is :\",model_.predict(image.reshape(1,-1))[0])","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}