{"cells":[{"metadata":{},"cell_type":"markdown","source":">     APTOS 2019 Blindness Detection\nDetecting diabetic retinopathy to stop blindness\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"#import necessary packages\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.metrics import classification_report,confusion_matrix \nfrom sklearn.metrics import cohen_kappa_score\nfrom imblearn.over_sampling import SMOTE\nimport numpy as np\nimport pandas as pd\nfrom tqdm import tqdm_notebook as tqdm\n\n#Ensures consistency across runs\nfrom numpy.random import seed\nseed(1)\n\n#Imports to view data\nimport cv2\nfrom glob import glob\nimport matplotlib.pyplot as plt\nfrom numpy import floor\nimport random\n\n#others\nimport os\nprint(os.listdir('../input/'))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Read datasets"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df=pd.read_csv('../input/train.csv')\ntest_df=pd.read_csv('../input/test.csv')\nprint('Size of train dataset',train_df.shape)\nprint('Size of test dataset',test_df.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"display(train_df.head(6))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_labels=train_df['diagnosis']\ndisplay(train_labels.head())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"display(test_df.head(6))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Target classes\ntarget_classes=['No DR','Mild','Moderate','Sever','Proliferative DR']","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Taregt classes\n\n    0 - No DR\n\n    1 - Mild\n\n    2 - Moderate\n\n    3 - Severe\n\n    4 - Proliferative DR\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"#Histo plot for trian data\nfrom sklearn.utils import shuffle\nid_code=train_df['id_code']\ndiagnosis=train_df['diagnosis']\nid_code,diagnosis=shuffle(id_code,diagnosis,random_state=42)\nplt.figure(figsize=(15,8))\nplt.xlabel('diagnosis')\nplt.ylabel('count')\ndiagnosis.hist()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Read & resize the images"},{"metadata":{"trusted":true},"cell_type":"code","source":"def read_image(path,id_code,size):\n    img_path=os.path.join(path,id_code+'.png')\n    image=cv2.imread(img_path)\n    image=cv2.resize(image,(size,size))\n    #Normalizing pixel data (0-255)\n    image=image.reshape(size,size,3).astype('float32')/255  \n    return image","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Build train images data as a numpy array\ntrain_images=[]\ntrain_images.append(train_df['id_code'].apply(lambda x:read_image\n                                              ('../input/train_images',x,128)))\ndf_train=np.array(train_images)\ndf_train=df_train.reshape(df_train.shape[1],128,128,3).astype('float32')\nprint(df_train.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Build test images data as a numpy array\ntest_df=pd.read_csv('../input/sample_submission.csv')\ntest_images=[]\ntest_images.append(test_df['id_code'].apply(lambda x:read_image\n                                              ('../input/test_images',x,128)))\nX_test=np.array(test_images)\nX_test=X_test.reshape(X_test.shape[1],128,128,3).astype('float32')\nprint(X_test.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#convert 3d array to 1d array\ndf_train_2d = len(df_train)\ndf_train= df_train.reshape(df_train_2d,-1)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"> SMOTE over sampling method for handling imbalanced data"},{"metadata":{"trusted":true},"cell_type":"code","source":"#Before sampling\nprint('Before sampling...')\nprint('Size of the train dataset:',len(df_train))\nprint(\"Before sampling,counts of label '0':{}\".format(sum(train_labels==0)))\nprint(\"Before sampling,counts of label '1':{}\".format(sum(train_labels==1)))\nprint(\"Before sampling,counts of label '2':{}\".format(sum(train_labels==2)))\nprint(\"Before sampling,counts of label '3':{}\".format(sum(train_labels==3)))\nprint(\"Before sampling,counts of label '4':{}\".format(sum(train_labels==4)))\n#Apply SMOTE technique\nsm=SMOTE(random_state=42,k_neighbors=3)\nX_train_res,Y_train_res=sm.fit_sample(df_train,train_labels.ravel())\n\n#After sampling\nprint('After sampling...')\nprint('Size of the train dataset:',len(X_train_res))\nprint(\"Before sampling,counts of label '0':{}\".format(sum(Y_train_res==0)))\nprint(\"Before sampling,counts of label '1':{}\".format(sum(Y_train_res==1)))\nprint(\"Before sampling,counts of label '2':{}\".format(sum(Y_train_res==2)))\nprint(\"Before sampling,counts of label '3':{}\".format(sum(Y_train_res==3)))\nprint(\"Before sampling,counts of label '4':{}\".format(sum(Y_train_res==4)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train,X_valid,Y_train,Y_valid=train_test_split(X_train_res,Y_train_res,\n                                                 test_size=0.1,\n                                                 random_state=42)\nprint(len(X_train),len(X_valid))\nprint(len(Y_train),len(Y_valid))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#convert 3d array to 1d array\nX_test_2d = len(X_test)\nX_test= X_test.reshape(X_test_2d,-1)\nprint(X_train.shape,X_valid.shape,X_test.shape)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Train & evaluate KNN Classfier on pixel intensities"},{"metadata":{"trusted":true},"cell_type":"code","source":"print('Evaluating the KNN Classifier...')\nmodel=KNeighborsClassifier(n_neighbors=3,n_jobs=-1)\nmodel.fit(X_train,Y_train)\nY_pred=model.predict(X_valid)\nclassification_report=classification_report(Y_valid,Y_pred,target_names=target_classes)\ndisplay(classification_report)\nconfusion_matrix=confusion_matrix(Y_valid,Y_pred)\ndisplay(confusion_matrix)\nkappa_score=cohen_kappa_score(Y_valid,Y_pred,weights='quadratic')\nprint('Quadratic Kappa Score:')\ndisplay(kappa_score)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#prediction on unseen data (test data)\nKNN=pd.read_csv('../input/sample_submission.csv')\nY_predict=model.predict(X_test)\nKNN['diagnosis']=Y_predict\nKNN.to_csv('submission.csv',index=False)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}