{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"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 # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\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# 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-09-02T18:54:56.641013Z","iopub.execute_input":"2023-09-02T18:54:56.641445Z","iopub.status.idle":"2023-09-02T18:54:57.060484Z","shell.execute_reply.started":"2023-09-02T18:54:56.641411Z","shell.execute_reply":"2023-09-02T18:54:57.059229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Including all the necessary files","metadata":{}},{"cell_type":"code","source":"df_train=pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ndf_test=pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\ndf_samp=pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-09-02T18:54:58.076054Z","iopub.execute_input":"2023-09-02T18:54:58.076781Z","iopub.status.idle":"2023-09-02T18:55:04.816071Z","shell.execute_reply.started":"2023-09-02T18:54:58.076745Z","shell.execute_reply":"2023-09-02T18:55:04.814848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-02T18:55:04.818276Z","iopub.execute_input":"2023-09-02T18:55:04.818623Z","iopub.status.idle":"2023-09-02T18:55:04.853481Z","shell.execute_reply.started":"2023-09-02T18:55:04.818593Z","shell.execute_reply":"2023-09-02T18:55:04.852184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-02T18:55:04.857498Z","iopub.execute_input":"2023-09-02T18:55:04.857891Z","iopub.status.idle":"2023-09-02T18:55:04.878276Z","shell.execute_reply.started":"2023-09-02T18:55:04.857857Z","shell.execute_reply":"2023-09-02T18:55:04.877063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_samp.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-02T18:55:04.880790Z","iopub.execute_input":"2023-09-02T18:55:04.881118Z","iopub.status.idle":"2023-09-02T18:55:04.894296Z","shell.execute_reply.started":"2023-09-02T18:55:04.881090Z","shell.execute_reply":"2023-09-02T18:55:04.893048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train=df_train.loc[:,:]\nx_test=df_test.loc[:,:]\nx_train=np.array(x_train)\nx_test=np.array(x_test)","metadata":{"execution":{"iopub.status.busy":"2023-09-02T18:55:04.895716Z","iopub.execute_input":"2023-09-02T18:55:04.896101Z","iopub.status.idle":"2023-09-02T18:55:04.903692Z","shell.execute_reply.started":"2023-09-02T18:55:04.896070Z","shell.execute_reply":"2023-09-02T18:55:04.902834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Creating a training set that will train the model ","metadata":{}},{"cell_type":"code","source":"y_train=np.array(x_train[:,0])\nx_train=np.delete(x_train,0,axis=1)\nm,n=x_train.shape\ny_train=y_train.reshape(-1,1)","metadata":{"execution":{"iopub.status.busy":"2023-09-02T18:55:05.084010Z","iopub.execute_input":"2023-09-02T18:55:05.084451Z","iopub.status.idle":"2023-09-02T18:55:05.195463Z","shell.execute_reply.started":"2023-09-02T18:55:05.084410Z","shell.execute_reply":"2023-09-02T18:55:05.194517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#creating a new 30000,10 array from y_test(for multi class classification)\ny_Test=np.zeros((m,10))\n\n#this function takes the value and create a 30000 x 10 array like 0 0 1 0....0 0 \ndef change(g):\n  for i in range (m):\n    p=int(g[i])\n    y_Test[i,p]=1\n  return y_Test\n\ny_Test=change(y_train)\nw=np.random.uniform(2,8,(n,10))\nb=np.ones((m,10))\n","metadata":{"execution":{"iopub.status.busy":"2023-09-02T18:55:05.197158Z","iopub.execute_input":"2023-09-02T18:55:05.197600Z","iopub.status.idle":"2023-09-02T18:55:05.242397Z","shell.execute_reply.started":"2023-09-02T18:55:05.197562Z","shell.execute_reply":"2023-09-02T18:55:05.241225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#for reducing overflow\nx_train=(x_train)/255\nx_test=(x_test)/255","metadata":{"execution":{"iopub.status.busy":"2023-09-02T18:55:05.244164Z","iopub.execute_input":"2023-09-02T18:55:05.244589Z","iopub.status.idle":"2023-09-02T18:55:05.428958Z","shell.execute_reply.started":"2023-09-02T18:55:05.244548Z","shell.execute_reply":"2023-09-02T18:55:05.427954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## All the functions are build from scratch","metadata":{}},{"cell_type":"code","source":"#sigmoid function\ndef sigmoid(x,w,b):\n  z=np.dot(x,w) + b \n  return 1/(1+np.exp(-z))\n","metadata":{"execution":{"iopub.status.busy":"2023-09-02T18:55:05.433526Z","iopub.execute_input":"2023-09-02T18:55:05.433909Z","iopub.status.idle":"2023-09-02T18:55:05.439841Z","shell.execute_reply.started":"2023-09-02T18:55:05.433877Z","shell.execute_reply":"2023-09-02T18:55:05.438589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#This return an array (prediction)\ndef prediction(s):\n  return np.argmax(s , axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-09-02T18:55:05.441208Z","iopub.execute_input":"2023-09-02T18:55:05.441696Z","iopub.status.idle":"2023-09-02T18:55:05.453245Z","shell.execute_reply.started":"2023-09-02T18:55:05.441654Z","shell.execute_reply":"2023-09-02T18:55:05.451700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#This calculates dJ/dw and dJ/db and returns that \ndef gradient(x,y,w,b):\n    m,n=x.shape\n    \n    dj_dw=np.zeros((n,10))\n \n    f=sigmoid(x,w,b)\n    dj_db=(f-y)/m \n    dj_dw=np.dot(x.T,f-y)/m \n    return dj_dw, dj_db","metadata":{"execution":{"iopub.status.busy":"2023-09-02T18:55:05.454758Z","iopub.execute_input":"2023-09-02T18:55:05.455229Z","iopub.status.idle":"2023-09-02T18:55:05.470679Z","shell.execute_reply.started":"2023-09-02T18:55:05.455187Z","shell.execute_reply":"2023-09-02T18:55:05.469331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 5 steps will be followed and after each steps value of alpha will increase and iteration will decrease","metadata":{}},{"cell_type":"code","source":"def gradient_descent(x,w,b,y,alpha):\n  for i in range(iterations):\n    dj_dw_f, dj_db_f= gradient(x,y,w,b)\n    w= w - alpha*dj_dw_f\n    b= b - alpha*dj_db_f\n    if i%50==0:\n      print(\"iterations is:\", i)\n    #plt.scatter(i,h,marker='X',c='b')\n    #plt.title(\"GRAPH FOR COST VS ITERATIONS\")\n    #plt.xlabel(\"ITERATIONS\")\n    #plt.ylabel(\"COST\")\n  return w,b\niterations=1000\nalpha=0.4\nfor i in range(5):\n    w,b= gradient_descent(x_train,w,b,y_Test,alpha)\n    alpha+=0.2\n    iterations-=200\n    print(\"Step : \",i+1,\" done successfully\",\"\\n\")\n","metadata":{"execution":{"iopub.status.busy":"2023-09-02T18:55:05.473035Z","iopub.execute_input":"2023-09-02T18:55:05.474258Z","iopub.status.idle":"2023-09-02T19:03:03.144571Z","shell.execute_reply.started":"2023-09-02T18:55:05.474211Z","shell.execute_reply":"2023-09-02T19:03:03.143005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Calculating accuracy after 3000 iterations on training set in 5 steps","metadata":{}},{"cell_type":"code","source":"f=sigmoid(x_train,w,b)\ny_predicted=prediction(f)\ny_predicted=y_predicted.reshape(m,1)\nc=0\nfor j in range(m):\n  if y_train[j]==y_predicted[j]:\n    c+=1\nprint(\"Accuracy : \",100*c/m)","metadata":{"execution":{"iopub.status.busy":"2023-09-02T19:03:03.151960Z","iopub.execute_input":"2023-09-02T19:03:03.152876Z","iopub.status.idle":"2023-09-02T19:03:03.437042Z","shell.execute_reply.started":"2023-09-02T19:03:03.152798Z","shell.execute_reply":"2023-09-02T19:03:03.435881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Accuracy is shown here ✔️","metadata":{}},{"cell_type":"markdown","source":"## Confusion Matrix is below","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\n\n# Assuming you have two sets of labels\npredicted_labels = np.array(y_predicted)\ntrue_labels = np.array(y_train)\n\n# Calculate confusion matrix\ncm = confusion_matrix(y_train, y_predicted)\n\n# Plot confusion matrix\nplt.imshow(cm, interpolation='nearest', cmap=plt.cm.Reds)\nplt.title('Confusion Matrix')\nplt.colorbar()\ntick_marks = np.arange(len(np.unique(true_labels)))\nplt.xticks(tick_marks, np.unique(true_labels))\nplt.yticks(tick_marks, np.unique(true_labels))\nplt.xlabel('Predicted Label')\nplt.ylabel('True Label')\n\n# Show plot\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-02T19:03:03.439042Z","iopub.execute_input":"2023-09-02T19:03:03.439889Z","iopub.status.idle":"2023-09-02T19:03:04.593817Z","shell.execute_reply.started":"2023-09-02T19:03:03.439843Z","shell.execute_reply":"2023-09-02T19:03:04.592599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ***NOW VERIFYING ITH THE TEST SET***","metadata":{}},{"cell_type":"code","source":"f=sigmoid(x_test,w,b[:28000,:])\nY_PRE=prediction(f)\nY_PRE=Y_PRE.reshape(-1,1)\ndef test(index,x,y):\n    print(\"PREDICTED NUMBER BY THE MODEL : \",int(Y_PRE[index]),\"\\n\")\n    print(\"Actual image ---> \")\n    plt.imshow(x[index].reshape(28,28))\n    plt.show()\n    print(\"\\n\",\"\\n\")\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2023-09-02T19:03:04.595593Z","iopub.execute_input":"2023-09-02T19:03:04.596430Z","iopub.status.idle":"2023-09-02T19:03:04.704178Z","shell.execute_reply.started":"2023-09-02T19:03:04.596383Z","shell.execute_reply":"2023-09-02T19:03:04.702299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Showing out put for our test dataset  ","metadata":{}},{"cell_type":"code","source":"for j in range(10):\n    test(j,x_test,Y_PRE)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-09-02T19:03:04.706328Z","iopub.execute_input":"2023-09-02T19:03:04.707095Z","iopub.status.idle":"2023-09-02T19:03:07.173828Z","shell.execute_reply.started":"2023-09-02T19:03:04.707049Z","shell.execute_reply":"2023-09-02T19:03:07.172841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_samp['Label']=Y_PRE","metadata":{"execution":{"iopub.status.busy":"2023-09-02T19:03:27.470434Z","iopub.execute_input":"2023-09-02T19:03:27.470898Z","iopub.status.idle":"2023-09-02T19:03:27.477683Z","shell.execute_reply.started":"2023-09-02T19:03:27.470859Z","shell.execute_reply":"2023-09-02T19:03:27.476181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_samp.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-02T19:03:40.849897Z","iopub.execute_input":"2023-09-02T19:03:40.850624Z","iopub.status.idle":"2023-09-02T19:03:40.864315Z","shell.execute_reply.started":"2023-09-02T19:03:40.850579Z","shell.execute_reply":"2023-09-02T19:03:40.863038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_samp.to_csv('/kaggle/working/df_samp.csv',index=False)\n","metadata":{"execution":{"iopub.status.busy":"2023-09-02T19:05:24.211225Z","iopub.execute_input":"2023-09-02T19:05:24.211963Z","iopub.status.idle":"2023-09-02T19:05:24.289505Z","shell.execute_reply.started":"2023-09-02T19:05:24.211924Z","shell.execute_reply":"2023-09-02T19:05:24.288591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# IF U LOVED MY WORK DO LIKE AND GIVE YOUR VIEWS REGARDING THIS PROJECT 😊","metadata":{}},{"cell_type":"markdown","source":"## THANK YOU AND ALL OF YOUR IDEAS ARE ALWAYS OPEN IN MY COMMENT SECTION.. ❤️","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"}}