{"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":"import numpy as np \nimport pandas as pd\nimport os\nprint(os.listdir(\"../input\"))\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-05T22:09:30.061928Z","iopub.execute_input":"2021-06-05T22:09:30.064318Z","iopub.status.idle":"2021-06-05T22:09:30.069301Z","shell.execute_reply.started":"2021-06-05T22:09:30.064285Z","shell.execute_reply":"2021-06-05T22:09:30.068621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os, sys\nimport matplotlib.pyplot as plt\nimport cv2\nfrom sklearn.model_selection import train_test_split\nfrom PIL import Image\nimport warnings\nwarnings.filterwarnings(\"ignore\")\nIMG_SIZE = 512","metadata":{"execution":{"iopub.status.busy":"2021-06-05T22:09:30.073397Z","iopub.execute_input":"2021-06-05T22:09:30.073725Z","iopub.status.idle":"2021-06-05T22:09:30.087147Z","shell.execute_reply.started":"2021-06-05T22:09:30.073697Z","shell.execute_reply":"2021-06-05T22:09:30.086181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train=pd.read_csv(\"../input/aptos2019-blindness-detection/train.csv\")\ndf_test=pd.read_csv(\"../input/aptos2019-blindness-detection/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-06-05T22:09:30.088619Z","iopub.execute_input":"2021-06-05T22:09:30.088884Z","iopub.status.idle":"2021-06-05T22:09:30.112775Z","shell.execute_reply.started":"2021-06-05T22:09:30.088857Z","shell.execute_reply":"2021-06-05T22:09:30.111829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-05T22:09:30.114013Z","iopub.execute_input":"2021-06-05T22:09:30.114507Z","iopub.status.idle":"2021-06-05T22:09:30.121593Z","shell.execute_reply.started":"2021-06-05T22:09:30.114447Z","shell.execute_reply":"2021-06-05T22:09:30.120870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=df_train[\"id_code\"]\ny=df_train[\"diagnosis\"]\nlabels=[\"Train\",\"Test\"]\nsizes=[len(df_train),len(df_test)]\n\nplt.pie(sizes,labels=labels,autopct='%1.1f%%')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-05T22:09:30.122715Z","iopub.execute_input":"2021-06-05T22:09:30.122929Z","iopub.status.idle":"2021-06-05T22:09:30.199835Z","shell.execute_reply.started":"2021-06-05T22:09:30.122907Z","shell.execute_reply":"2021-06-05T22:09:30.198838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.15,random_state=42)\nprint(\"x_train shape: \", x_train.shape)\nprint(\"x_test shape: \", x_test.shape)\nprint(\"y_train shape: \", y_train.shape)\nprint(\"y_test shape: \", y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2021-06-05T22:09:30.201254Z","iopub.execute_input":"2021-06-05T22:09:30.201508Z","iopub.status.idle":"2021-06-05T22:09:30.210743Z","shell.execute_reply.started":"2021-06-05T22:09:30.201485Z","shell.execute_reply":"2021-06-05T22:09:30.209951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(y_train,label=\"y_train\")\nplt.hist(y_test,label=\"y_test\")\nplt.title(\"Retinopathy Type and number\")\nplt.xlabel(\"0:No DR, 1:Mild, 2:Moderate, 3:Severe, 4:Proliferative DR\")\nplt.ylabel(\"Number\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-05T22:09:30.212113Z","iopub.execute_input":"2021-06-05T22:09:30.212421Z","iopub.status.idle":"2021-06-05T22:09:30.408752Z","shell.execute_reply.started":"2021-06-05T22:09:30.212395Z","shell.execute_reply":"2021-06-05T22:09:30.408113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(25, 16))\n# display 10 images from each class\nfor i in sorted(y_train.unique()):\n    for j, (idx, row) in enumerate(df_train.loc[df_train['diagnosis'] == i].sample(5, random_state=42).iterrows()):\n        ax = fig.add_subplot(5, 5, i * 5 + j + 1, xticks=[], yticks=[])\n        path=f\"../input/aptos2019-blindness-detection/train_images/{row['id_code']}.png\"\n        image = cv2.imread(path)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n        image = cv2.resize(image, (IMG_SIZE, IMG_SIZE))\n        plt.imshow(image)\n        ax.set_title('Label: %d-%d-%s' % (i, idx, row['id_code']) )","metadata":{"execution":{"iopub.status.busy":"2021-06-05T22:09:30.409699Z","iopub.execute_input":"2021-06-05T22:09:30.410061Z","iopub.status.idle":"2021-06-05T22:09:34.876420Z","shell.execute_reply.started":"2021-06-05T22:09:30.410033Z","shell.execute_reply":"2021-06-05T22:09:34.875411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig=plt.figure(figsize=(25,16))\nfor i in sorted(y_train.unique()):\n    for j ,(idx,row) in enumerate(df_train.loc[df_train[\"diagnosis\"]==i].sample(5,random_state=42).iterrows()):\n        ax = fig.add_subplot(5, 5, i*5+j+1, xticks=[], yticks=[])\n        path=f\"../input/aptos2019-blindness-detection/train_images/{row['id_code']}.png\"\n        image = cv2.imread(path)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n        image = cv2.resize(image, (IMG_SIZE, IMG_SIZE))\n\n        plt.imshow(image)\n        ax.set_title('Label: %d-%d-%s' % (i, idx, row['id_code']) )","metadata":{"execution":{"iopub.status.busy":"2021-06-05T22:09:34.878335Z","iopub.execute_input":"2021-06-05T22:09:34.878634Z","iopub.status.idle":"2021-06-05T22:09:39.423401Z","shell.execute_reply.started":"2021-06-05T22:09:34.878604Z","shell.execute_reply":"2021-06-05T22:09:39.422565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig=plt.figure(figsize=(25,16))\nfor i in sorted(y_train.unique()):\n    for j ,(idx,row) in enumerate(df_train.loc[df_train[\"diagnosis\"]==i].sample(5,random_state=42).iterrows()):\n        ax = fig.add_subplot(5, 5, i*5+j+1, xticks=[], yticks=[])\n        path=f\"../input/aptos2019-blindness-detection/train_images/{row['id_code']}.png\"\n        image = cv2.imread(path)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n        image = cv2.resize(image, (IMG_SIZE, IMG_SIZE))\n\n        plt.imshow(image,cmap=\"gray\")\n        ax.set_title('Label: %d-%d-%s' % (i, idx, row['id_code']) )","metadata":{"execution":{"iopub.status.busy":"2021-06-05T22:09:39.424896Z","iopub.execute_input":"2021-06-05T22:09:39.425158Z","iopub.status.idle":"2021-06-05T22:09:43.509560Z","shell.execute_reply.started":"2021-06-05T22:09:39.425130Z","shell.execute_reply":"2021-06-05T22:09:43.508571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dpi=80\n\npath=f\"../input/aptos2019-blindness-detection/train_images/838c87c63422.png\"\nimage = cv2.imread(path)\nimage=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)\nheight,width=image.shape\nprint(height,width)\nSCALE=2\nfigsize=(width/float(dpi))/SCALE,(height/float(dpi))/SCALE\nimage = cv2.resize(image, (IMG_SIZE, IMG_SIZE))\nfig=plt.figure(figsize=figsize)\nplt.imshow(image,cmap=\"gray\")","metadata":{"execution":{"iopub.status.busy":"2021-06-05T22:09:43.510538Z","iopub.execute_input":"2021-06-05T22:09:43.510737Z","iopub.status.idle":"2021-06-05T22:09:43.868953Z","shell.execute_reply.started":"2021-06-05T22:09:43.510716Z","shell.execute_reply":"2021-06-05T22:09:43.868099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig=plt.figure(figsize=(25,16))\nfor i in sorted(y_train.unique()):\n    for j,(idx,row) in enumerate(df_train.loc[df_train[\"diagnosis\"]==i].sample(5,random_state=42).iterrows()):\n        \n        ax = fig.add_subplot(5, 5, i*5+j+1, xticks=[], yticks=[])\n        path=f\"../input/aptos2019-blindness-detection/train_images/{row['id_code']}.png\"\n        image = cv2.imread(path)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n\n        image = cv2.resize(image, (IMG_SIZE, IMG_SIZE))\n        image=cv2.addWeighted ( image,4, cv2.GaussianBlur( image , (0,0) , IMG_SIZE/10) ,-4 ,128) \n\n        plt.imshow(image, cmap='gray')\n        ax.set_title('Label: %d-%d-%s' % (i, idx, row['id_code']) )","metadata":{"execution":{"iopub.status.busy":"2021-06-05T22:09:43.870039Z","iopub.execute_input":"2021-06-05T22:09:43.870488Z","iopub.status.idle":"2021-06-05T22:09:53.183703Z","shell.execute_reply.started":"2021-06-05T22:09:43.870431Z","shell.execute_reply":"2021-06-05T22:09:53.182683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def crop_image_from_gray(img,tol=7):\n    if img.ndim ==2:\n        mask = img>tol\n        return img[np.ix_(mask.any(1),mask.any(0))]\n    elif img.ndim==3:\n        gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)\n        mask = gray_img>tol\n        \n        check_shape = img[:,:,0][np.ix_(mask.any(1),mask.any(0))].shape[0]\n        if (check_shape == 0): \n            return img \n        else:\n            img1=img[:,:,0][np.ix_(mask.any(1),mask.any(0))]\n            img2=img[:,:,1][np.ix_(mask.any(1),mask.any(0))]\n            img3=img[:,:,2][np.ix_(mask.any(1),mask.any(0))]\n   \n            img = np.stack([img1,img2,img3],axis=-1)\n    \n        return img","metadata":{"execution":{"iopub.status.busy":"2021-06-05T22:09:53.184715Z","iopub.execute_input":"2021-06-05T22:09:53.184937Z","iopub.status.idle":"2021-06-05T22:09:53.192300Z","shell.execute_reply.started":"2021-06-05T22:09:53.184915Z","shell.execute_reply":"2021-06-05T22:09:53.191232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_ben_color(path, sigmaX=10):\n    image = cv2.imread(path)\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    image = crop_image_from_gray(image)\n    image = cv2.resize(image, (IMG_SIZE, IMG_SIZE))\n    image=cv2.addWeighted ( image,4, cv2.GaussianBlur( image , (0,0) , sigmaX) ,-4 ,128)\n        \n    return image","metadata":{"execution":{"iopub.status.busy":"2021-06-05T22:09:53.193366Z","iopub.execute_input":"2021-06-05T22:09:53.193617Z","iopub.status.idle":"2021-06-05T22:09:53.209400Z","shell.execute_reply.started":"2021-06-05T22:09:53.193595Z","shell.execute_reply":"2021-06-05T22:09:53.208278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"NUM_SAMP=7\nfig = plt.figure(figsize=(25, 16))\nfor i in sorted(y_train.unique()):\n    for j, (idx, row) in enumerate(df_train.loc[df_train['diagnosis'] == i].sample(NUM_SAMP, random_state=42).iterrows()):\n        ax = fig.add_subplot(5, NUM_SAMP, i* NUM_SAMP + j + 1, xticks=[], yticks=[])\n        path=f\"../input/aptos2019-blindness-detection/train_images/{row['id_code']}.png\"\n        image = load_ben_color(path,sigmaX=30)\n\n        plt.imshow(image)\n        ax.set_title('%d-%d-%s' % (i, idx, row['id_code']) )","metadata":{"execution":{"iopub.status.busy":"2021-06-05T22:09:53.210390Z","iopub.execute_input":"2021-06-05T22:09:53.210727Z","iopub.status.idle":"2021-06-05T22:10:06.736802Z","shell.execute_reply.started":"2021-06-05T22:09:53.210698Z","shell.execute_reply":"2021-06-05T22:10:06.735657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_ben_color2(path, sigmaX=10):\n    image = cv2.imread(path)\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n    image = crop_image_from_gray(image)\n    image = cv2.resize(image, (IMG_SIZE, IMG_SIZE))\n  \n        \n    return image","metadata":{"execution":{"iopub.status.busy":"2021-06-05T22:10:06.737870Z","iopub.execute_input":"2021-06-05T22:10:06.738085Z","iopub.status.idle":"2021-06-05T22:10:06.742781Z","shell.execute_reply.started":"2021-06-05T22:10:06.738063Z","shell.execute_reply":"2021-06-05T22:10:06.741859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dpi = 80\npath=f\"../input/aptos2019-blindness-detection/train_images/838c87c63422.png\" \nimage = load_ben_color(path,sigmaX=10)\n\nheight, width = IMG_SIZE, IMG_SIZE\nprint(height, width)\n\nSCALE=1\nfigsize = (width / float(dpi))/SCALE, (height / float(dpi))/SCALE\n\nfig = plt.figure(figsize=figsize)\nplt.imshow(image, cmap='gray')\n\ndpi = 80 \n\npath=f\"../input/aptos2019-blindness-detection/train_images/838c87c63422.png\" \nimage = load_ben_color2(path,sigmaX=10)\n\nheight, width = IMG_SIZE, IMG_SIZE\nprint(height, width)\n\nSCALE=1\nfigsize = (width / float(dpi))/SCALE, (height / float(dpi))/SCALE\n\nfig = plt.figure(figsize=figsize)\nplt.imshow(image, cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2021-06-05T22:10:06.743781Z","iopub.execute_input":"2021-06-05T22:10:06.744041Z","iopub.status.idle":"2021-06-05T22:10:07.472545Z","shell.execute_reply.started":"2021-06-05T22:10:06.744016Z","shell.execute_reply":"2021-06-05T22:10:07.471697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dpi = 80 \n\npath=f\"../input/aptos2019-blindness-detection/train_images/78937523f7a8.png\" \nimage = load_ben_color(path,sigmaX=10)\n\nheight, width = IMG_SIZE, IMG_SIZE\nprint(height, width)\n\nSCALE=1\nfigsize = (width / float(dpi))/SCALE, (height / float(dpi))/SCALE\n\nfig = plt.figure(figsize=figsize)\nplt.imshow(image, cmap='gray')\ndpi = 80 \n\n\npath=f\"../input/aptos2019-blindness-detection/train_images/838c87c63422.png\" \nimage = load_ben_color(path,sigmaX=10)\n\nheight, width = IMG_SIZE, IMG_SIZE\nprint(height, width)\n\nSCALE=1\nfigsize = (width / float(dpi))/SCALE, (height / float(dpi))/SCALE\n\nfig = plt.figure(figsize=figsize)\nplt.imshow(image, cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2021-06-05T22:10:07.474766Z","iopub.execute_input":"2021-06-05T22:10:07.474988Z","iopub.status.idle":"2021-06-05T22:10:08.142156Z","shell.execute_reply.started":"2021-06-05T22:10:07.474966Z","shell.execute_reply":"2021-06-05T22:10:08.141373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.read_csv(\"../input/aptos2019-blindness-detection/train.csv\")\n\n","metadata":{"execution":{"iopub.status.busy":"2021-06-05T22:10:08.143848Z","iopub.execute_input":"2021-06-05T22:10:08.144105Z","iopub.status.idle":"2021-06-05T22:10:08.153759Z","shell.execute_reply.started":"2021-06-05T22:10:08.144080Z","shell.execute_reply":"2021-06-05T22:10:08.152849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-05T22:10:08.154764Z","iopub.execute_input":"2021-06-05T22:10:08.154999Z","iopub.status.idle":"2021-06-05T22:10:08.169495Z","shell.execute_reply.started":"2021-06-05T22:10:08.154973Z","shell.execute_reply":"2021-06-05T22:10:08.168546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"patient=train[train[\"diagnosis\"]==4]\nhealt=train[train[\"diagnosis\"]==0]\ntrain_df=pd.concat([patient,healt])","metadata":{"execution":{"iopub.status.busy":"2021-06-05T22:10:08.170889Z","iopub.execute_input":"2021-06-05T22:10:08.171128Z","iopub.status.idle":"2021-06-05T22:10:08.184473Z","shell.execute_reply.started":"2021-06-05T22:10:08.171104Z","shell.execute_reply":"2021-06-05T22:10:08.183611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[\"diagnosis\"]=[1 if i==4 else 0 for i in train_df.diagnosis]","metadata":{"execution":{"iopub.status.busy":"2021-06-05T22:10:08.185585Z","iopub.execute_input":"2021-06-05T22:10:08.185798Z","iopub.status.idle":"2021-06-05T22:10:08.197410Z","shell.execute_reply.started":"2021-06-05T22:10:08.185777Z","shell.execute_reply":"2021-06-05T22:10:08.196791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape","metadata":{"execution":{"iopub.status.busy":"2021-06-05T22:10:08.198405Z","iopub.execute_input":"2021-06-05T22:10:08.198855Z","iopub.status.idle":"2021-06-05T22:10:08.211093Z","shell.execute_reply.started":"2021-06-05T22:10:08.198829Z","shell.execute_reply":"2021-06-05T22:10:08.210352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=train_df.drop(columns=[\"diagnosis\"])\ny=train_df.diagnosis","metadata":{"execution":{"iopub.status.busy":"2021-06-05T22:10:08.212104Z","iopub.execute_input":"2021-06-05T22:10:08.212391Z","iopub.status.idle":"2021-06-05T22:10:08.223979Z","shell.execute_reply.started":"2021-06-05T22:10:08.212360Z","shell.execute_reply":"2021-06-05T22:10:08.223030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.30,random_state=42)","metadata":{"execution":{"iopub.status.busy":"2021-06-05T22:10:08.225112Z","iopub.execute_input":"2021-06-05T22:10:08.225390Z","iopub.status.idle":"2021-06-05T22:10:08.237547Z","shell.execute_reply.started":"2021-06-05T22:10:08.225359Z","shell.execute_reply":"2021-06-05T22:10:08.236575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dn_x = [ Image.open('../input/aptos2019-blindness-detection/train_images/'+i+'.png') for i in x_train.id_code[:5]]","metadata":{"execution":{"iopub.status.busy":"2021-06-05T22:10:08.238477Z","iopub.execute_input":"2021-06-05T22:10:08.238685Z","iopub.status.idle":"2021-06-05T22:10:08.256358Z","shell.execute_reply.started":"2021-06-05T22:10:08.238663Z","shell.execute_reply":"2021-06-05T22:10:08.255378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dn_x","metadata":{"execution":{"iopub.status.busy":"2021-06-05T22:10:08.257407Z","iopub.execute_input":"2021-06-05T22:10:08.257651Z","iopub.status.idle":"2021-06-05T22:10:08.276310Z","shell.execute_reply.started":"2021-06-05T22:10:08.257629Z","shell.execute_reply":"2021-06-05T22:10:08.275798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in dn_x:\n    \n    plt.figure(figsize=(5,3))\n    i = cv2.resize(np.asarray(i),(64,64))\n    i= cv2.cvtColor(i,cv2.COLOR_BGR2GRAY)\n    plt.imshow(i,cmap=\"gray\")\n    plt.axis(\"off\")\n    plt.show","metadata":{"execution":{"iopub.status.busy":"2021-06-05T22:10:08.277089Z","iopub.execute_input":"2021-06-05T22:10:08.277395Z","iopub.status.idle":"2021-06-05T22:10:08.870472Z","shell.execute_reply.started":"2021-06-05T22:10:08.277361Z","shell.execute_reply":"2021-06-05T22:10:08.869991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train=x_train_flatten.T\ny_train=y_train.T\nx_test=x_test_flatten.T\ny_test=y_test.T\nprint(\"x_train: \",x_train.shape)\nprint(\"x_test: \",x_test.shape)\nprint(\"y_train: \",y_train.shape)\nprint(\"y_test: \",y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2021-06-05T22:10:08.871281Z","iopub.execute_input":"2021-06-05T22:10:08.871598Z","iopub.status.idle":"2021-06-05T22:10:08.876973Z","shell.execute_reply.started":"2021-06-05T22:10:08.871564Z","shell.execute_reply":"2021-06-05T22:10:08.876512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train=np.array(y_train)\ny_test=np.array(y_test)","metadata":{"execution":{"iopub.status.busy":"2021-06-05T22:10:08.877786Z","iopub.execute_input":"2021-06-05T22:10:08.878091Z","iopub.status.idle":"2021-06-05T22:10:08.890918Z","shell.execute_reply.started":"2021-06-05T22:10:08.878060Z","shell.execute_reply":"2021-06-05T22:10:08.889981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train=y_train.reshape(-1,1)\ny_test=y_test.reshape(-1,1)\nprint(\"y_train: \",y_train.shape)\nprint(\"y_test: \",y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2021-06-05T22:10:08.891983Z","iopub.execute_input":"2021-06-05T22:10:08.892271Z","iopub.status.idle":"2021-06-05T22:10:08.903960Z","shell.execute_reply.started":"2021-06-05T22:10:08.892241Z","shell.execute_reply":"2021-06-05T22:10:08.903191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def initialize_weights_and_bias(dimension):\n    w=np.full((dimension,1),0.01)\n    b=0.0\n    return w,b","metadata":{"execution":{"iopub.status.busy":"2021-06-05T22:10:08.904948Z","iopub.execute_input":"2021-06-05T22:10:08.905255Z","iopub.status.idle":"2021-06-05T22:10:08.916420Z","shell.execute_reply.started":"2021-06-05T22:10:08.905225Z","shell.execute_reply":"2021-06-05T22:10:08.915947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def sigmoid(z):\n\n    y_head=1/(1+np.exp(-z))\n    return y_head","metadata":{"execution":{"iopub.status.busy":"2021-06-05T22:10:08.917365Z","iopub.execute_input":"2021-06-05T22:10:08.917690Z","iopub.status.idle":"2021-06-05T22:10:08.928073Z","shell.execute_reply.started":"2021-06-05T22:10:08.917658Z","shell.execute_reply":"2021-06-05T22:10:08.927379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def forward_propagation(w,b,x_train,y_train):\n    z=np.dot(w.T,x_train)+b\n    y_head=sigmoid(z)\n    loss=-y_train*np.log(y_head)-(1-y_train)*np.log(1-y_head)\n    cost=(np.sum(loss))/x_train.shape[1]\n    return cost","metadata":{"execution":{"iopub.status.busy":"2021-06-05T22:10:08.928833Z","iopub.execute_input":"2021-06-05T22:10:08.929223Z","iopub.status.idle":"2021-06-05T22:10:08.946508Z","shell.execute_reply.started":"2021-06-05T22:10:08.929199Z","shell.execute_reply":"2021-06-05T22:10:08.945760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def forward_backward_propagation(w,b,x_train,y_train):\n   \n    z=np.dot(w.T,x_train)+b\n    y_head=sigmoid(z)\n    loss=-y_train*np.log(y_head)-(1-y_train)*np.log(1-y_head)\n    cost=(np.sum(loss))/x_train.shape[1]\n    derivative_weight = (np.dot(x_train,((y_head-y_train).T)))/x_train.shape[1]\n    derivative_bias=np.sum(y_head-y_train)/x_train.shape[1]\n    gradients={\"derivative_weight\":derivative_weight,\"derivative_bias\":derivative_bias}\n    return cost,gradients","metadata":{"execution":{"iopub.status.busy":"2021-06-05T22:10:08.947483Z","iopub.execute_input":"2021-06-05T22:10:08.947804Z","iopub.status.idle":"2021-06-05T22:10:08.959149Z","shell.execute_reply.started":"2021-06-05T22:10:08.947763Z","shell.execute_reply":"2021-06-05T22:10:08.958530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def update(w,b,x_train,y_train,learning_rate,number_of_iteration):\n    cost_list=[]\n    cost_list2=[]\n    index=[]\n    for i in range(number_of_iteration):\n        cost,gradients=forward_backward_propagation(w,b,x_train,y_train)\n        cost_list.append(cost)\n        w=w-learning_rate*gradients[\"derivative_weight\"]\n        b=b-learning_rate*gradients[\"derivative_bias\"]\n    parameters ={\"weight\":w,\"bias\":b}\n    \n    return parameters,gradients,cost_list","metadata":{"execution":{"iopub.status.busy":"2021-06-05T22:10:08.960028Z","iopub.execute_input":"2021-06-05T22:10:08.960380Z","iopub.status.idle":"2021-06-05T22:10:08.972555Z","shell.execute_reply.started":"2021-06-05T22:10:08.960304Z","shell.execute_reply":"2021-06-05T22:10:08.971680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict(w,b,x_test):\n    z=sigmoid(np.dot(w.T,x_test)+b)\n    Y_prediction=np.zeros((1,x_test.shape[1]))\n    for i in range(z.shape[1]):\n        if z[0,i]<=0.5:\n            Y_prediction[0,i]=0\n        else:\n            Y_prediction[0,i]=1\n    return Y_prediction","metadata":{"execution":{"iopub.status.busy":"2021-06-05T22:10:08.973442Z","iopub.execute_input":"2021-06-05T22:10:08.973753Z","iopub.status.idle":"2021-06-05T22:10:08.984182Z","shell.execute_reply.started":"2021-06-05T22:10:08.973731Z","shell.execute_reply":"2021-06-05T22:10:08.983637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def logistic_regression(x_train,y_train,x_test,y_test,learning_rate,num_iterations):\n    dimension=x_train.shape[0]\n    w,b=initialize_weights_and_bias(dimension)\n    parameters,gradients,cost_list=update(w,b,x_train,y_train,learning_rate,num_iterations)\n    y_prediction_test=predict(parameters[\"weight\"],parameters[\"bias\"],x_test)\n    y_prediction_train=predict(parameters[\"weight\"],parameters[\"bias\"],x_train)\n    print(\"train accuracy: {} %\".format(100 - np.mean(np.abs(y_prediction_train - y_train)) * 100))\n    print(\"test accuracy: {} %\".format(100 - np.mean(np.abs(y_prediction_test - y_test)) * 100))\nlogistic_regression(x_train, y_train, x_test, y_test,learning_rate = 0.01, num_iterations = 50)","metadata":{"execution":{"iopub.status.busy":"2021-06-05T22:10:08.984931Z","iopub.execute_input":"2021-06-05T22:10:08.985281Z","iopub.status.idle":"2021-06-05T22:10:41.383784Z","shell.execute_reply.started":"2021-06-05T22:10:08.985196Z","shell.execute_reply":"2021-06-05T22:10:41.383173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import linear_model\nlogreg = linear_model.LogisticRegression(random_state = 42,max_iter= 150)\nprint(\"test accuracy: {} \".format(logreg.fit(x_train.T, y_train).score(x_test.T, y_test)))","metadata":{"execution":{"iopub.status.busy":"2021-06-05T22:10:41.384791Z","iopub.execute_input":"2021-06-05T22:10:41.385155Z","iopub.status.idle":"2021-06-05T22:10:41.968628Z","shell.execute_reply.started":"2021-06-05T22:10:41.385120Z","shell.execute_reply":"2021-06-05T22:10:41.968015Z"},"trusted":true},"execution_count":null,"outputs":[]}]}