{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"#Import necessary packages\nimport pandas as pd\nimport numpy as np\nimport sklearn\nimport matplotlib.pyplot as plt\nimport math\nimport random as rand\n\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.model_selection import train_test_split,GridSearchCV\n\nimport lightgbm as lgb","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"filepath=\"../input/\"\ntrain_path = filepath+\"train.csv\"\ntraining = pd.read_csv(train_path)\ntraining.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fff2003550d2452e9b76d72b2e9d8d68a6a13d75"},"cell_type":"code","source":"#Based on this, the picture size will be 28x28\nsize=int(math.sqrt(training.shape[1]-1))\nsize","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8f92c9302236be76526c64b59d3cc033dc89abe1"},"cell_type":"code","source":"training.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"769094a5e84ab624b5a51b61227f1a1155f32b27"},"cell_type":"code","source":"#Pull a random integer between 0 and number of rows in data set to test\nrand_num =rand.randint(0,training.shape[0])\n\n#Create array based on pulling row corresponding to random int\nnumber=np.array(training.loc[rand_num][1:],dtype='uint8')\n\ncolor_cutoff = 180\nnumber[number < color_cutoff] = 0\nnumber[number >= color_cutoff] = 254\n\n\n#Create 2-D array based on image size\nnumber=number.reshape((size,size))\n\nprint(\"Labeled value is\",str(training.loc[rand_num][0]))\nplt.imshow(number,cmap='Greys')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d850136b4281968c64146e438bb44269f13b9686"},"cell_type":"code","source":"#drop_list=training.columns.to_series()['pixel0':'pixel27']+training.columns.to_series()['pixel755':'pixel783']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"931b7910b5bf998ad28d74d05928407a24590716"},"cell_type":"code","source":"#Labels are fairly evenly distributed\nplt.hist(training['label'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d4916269451a81842efa128cbedb707540d80a32"},"cell_type":"code","source":"training_labels=training['label']\ntraining_without_labels=training.drop(labels='label',axis=1)\n#added\ndrop_list=training.columns.to_series()['pixel0':'pixel27']+training.columns.to_series()['pixel755':'pixel783']\ntraining=training.drop(columns=drop_list.index,axis=1)\n\ntrain_data, test_data, train_labels, test_labels = train_test_split(training_without_labels, training_labels, test_size=0.7)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0e9b3f5cebe93b946893d28e393f3f063bea6f14"},"cell_type":"code","source":"d_train = lgb.Dataset(train_data, label=train_labels)\nparams = {}\n#params['learning_rate'] = 0.01\nparams['boosting_type'] = 'gbdt'\nparams['objective'] = 'multiclass'\nparams['metric'] = 'multi_logloss'\nparams['num_class'] = 10\nparams['num_iterations'] = 500\n#params['max_bin']=50\n#params['num_leaves'] = 10\n\n#params['min_data'] = 50\n#params['max_depth'] = 10\nclf = lgb.train(params, d_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"77a02b818eb386542d4b2aa42d776d0943955467"},"cell_type":"code","source":"#Prediction\npredict=clf.predict(test_data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"24b34c7b70c178938200052d3ec0d6bf667e75e9"},"cell_type":"code","source":"predictions = []\n\nfor x in predict:\n    predictions.append(np.argmax(x))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2fb71c1df450779b751bfe90b5932ac0f23ee521"},"cell_type":"code","source":"#Accuracy\nfrom sklearn.metrics import accuracy_score\naccuracy=accuracy_score(test_labels,predictions)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"63a16fae657ade8d10af007fe96bdae8dceaa62b"},"cell_type":"code","source":"accuracy","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"efd9c9935508480f511f44f366dba06bdd406f37"},"cell_type":"code","source":"check_results = pd.DataFrame({'actual':test_labels,'predict':predictions})\ncheck_results['combo'] = check_results['actual'].astype('str')+\" \"+check_results['predict'].astype('str')\ncheck_results.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0abdfac41553256930a888cdf644cc99ded49b67"},"cell_type":"code","source":"check_results[check_results.actual != check_results.predict]['combo'].value_counts().sort_values(ascending=False).head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ddd7ab486b81f47b4257c65bf35d3a323fbfb5e4"},"cell_type":"code","source":"final_train = lgb.Dataset(training_without_labels, label=training_labels)\n\nfinal_clf = lgb.train(params, final_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d224b7729954b3385cdd523c5706e2c6240ec11b"},"cell_type":"code","source":"test_path = filepath+\"test.csv\"\ntesting = pd.read_csv(test_path)\n#testing=testing.drop(columns=drop_list.index,axis=1)\n\nsolutions=final_clf.predict(testing)\n\nsolution_labels = []\n\nfor x in solutions:\n    solution_labels.append(np.argmax(x))\n    \nsubmission=pd.DataFrame(solution_labels,columns=['Label'])\nsubmission.index.name = \"ImageID\"\nsubmission.index += 1\n\nsubmission.to_csv('submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"07caada368c604891a784e38f7b3199a15eb518e"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}