{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"},{"sourceId":3809092,"sourceType":"datasetVersion","datasetId":2270028},{"sourceId":8166529,"sourceType":"datasetVersion","datasetId":4832499},{"sourceId":8168182,"sourceType":"datasetVersion","datasetId":4833693},{"sourceId":8168358,"sourceType":"datasetVersion","datasetId":4833785}],"dockerImageVersionId":30673,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nfrom matplotlib import pyplot as plt\nfrom imblearn.over_sampling import SMOTE\nfrom sklearn.utils import shuffle\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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-04-19T20:33:38.935038Z","iopub.execute_input":"2024-04-19T20:33:38.935364Z","iopub.status.idle":"2024-04-19T20:33:38.952683Z","shell.execute_reply.started":"2024-04-19T20:33:38.935340Z","shell.execute_reply":"2024-04-19T20:33:38.951599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"migraineData4579 = pd.read_excel('/kaggle/input/bothtables/AddedTables (2).xlsx', sheet_name='total diary 4579')\nmigraineData1099 = pd.read_excel('/kaggle/input/bothtables/AddedTables (2).xlsx', sheet_name='headache diary 1099')\n#print(migraineData4579.shape)\nprint(migraineData1099.shape)\nnp1099 = np.array(migraineData1099)\nnp1099 = np1099[2:, 0:]\n\nY_1099 = np1099[0:1098, 35] # gets migraine data \nX_1099 = np1099[0:1098, 39:58]\n\nprint(Y_1099.shape, Y_1099[0])\nprint(X_1099.shape, X_1099[0])\nX_1099 = X_1099.astype(int)\nY_1099 = Y_1099.astype(int)\nsm = SMOTE(random_state=42)\n\nX_n1099, Y_n1099 = sm.fit_resample(X_1099, Y_1099)\n\n#X_new1099 = X_new1099.reshape(-1,1)\nY_n1099 = Y_n1099.reshape(-1,1)\n\nX_new1099, Y_new1099 = shuffle(X_n1099, Y_n1099, random_state=42)\n\nprint(X_new1099.shape, Y_new1099.shape)\n\n\nnp4579 = np.array(migraineData4579)\n#print(np4579[2, 38:57])\nY_4579 = np4579[2:4550, 26]\nX_4579 = np4579[2:4550, 38:57]\nprint(Y_4579.shape, Y_4579[0])\nprint(X_4579.shape, X_4579[0])\ndf_X_4579 = pd.DataFrame(X_4579)  # Convert to DataFrame for easier manipulation\ndf_X_4579 = df_X_4579.apply(pd.to_numeric, errors='coerce')\ndf_X_4579.fillna(df_X_4579.mean(), inplace=True)\n\nnan_in_X_4579 = df_X_4579.isnull().values.any()\nprint(\"NaN in X_4579:\", nan_in_X_4579)\nX_4579 = np.array(df_X_4579)\nprint(X_4579.dtype)\n\n#print(\"NaN in X_4579:\", np.any(np.isnan(X_4579)))\n\nX_4579 = X_4579.astype(int)\nY_4579 = Y_4579.astype(int)\nsm = SMOTE(random_state=42)\n\nX_n4579, Y_n4579 = sm.fit_resample(X_4579, Y_4579)\n\n#X_new1099 = X_new1099.reshape(-1,1)\nY_n4579 = Y_n4579.reshape(-1,1)\n\nX_new4579, Y_new4579 = shuffle(X_n4579, Y_n4579, random_state=42)\n\nprint(X_n4579.shape, Y_n4579.shape)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-19T20:33:38.962788Z","iopub.execute_input":"2024-04-19T20:33:38.963137Z","iopub.status.idle":"2024-04-19T20:33:43.059504Z","shell.execute_reply.started":"2024-04-19T20:33:38.963110Z","shell.execute_reply":"2024-04-19T20:33:43.057719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def relu(x):\n    return np.maximum(x,0) # relu activation\n            \ndef reluPrime(x):\n    return x > 0 # relu derivative\n        \ndef softMax(x):\n    A = np.exp(x) / sum(np.exp(x)) # softmax activation for output layer \n    return A \n        \ndef one_hot(y):\n    one_hot_array = np.zeros((10,1)) # one hot encoding for output layer loss calculation\n    class_label = y\n    one_hot_array[class_label] = 1.0\n            \n    return one_hot_array\n\ndef get_predic(A3):\n    return np.argmax(A3, 0) # get NN prediction\n    \ndef get_accur(predic, Y):\n    print(predic, Y)\n    #return np.sum(predic == Y) /  # compares predic vs y_train\n    \ndef init_params(sizes):\n    #print(\"IRAN\")\n    #inputLayer = X_Train[self.count]\n    W1 = np.random.rand(sizes[0],19)-0.5 #weights first hidden layer\n    B1 = np.random.rand(sizes[0],1)-0.5   #biases first hidden layer\n    W2 = np.random.rand(sizes[1],sizes[0])-0.5 #second layer]\n    B2 = np.random.rand(sizes[1],1)-0.5\n    W3 = np.random.rand(1,sizes[1])-0.5# output layer \n    B3 = np.random.rand(1,1)-0.5\n    #print(\"shapeW: \", W1.shape, \" \", W2.shape, \" \", W3.shape, \"B \", B1.shape, \" \", B2.shape, \" \", B3.shape)\n    return W1, B1, W2, B2, W3, B3\n\ndef forward_propagation( W1, B1, W2, B2, W3, B3, X):\n\n    Z1 = W1.dot(X)\n    B1 = B1.reshape(-1,1) # first layer \n    Z1 = Z1.reshape(-1,1) \n    A1 = relu(Z1)\n   \n    Z2 = W2.dot(A1)\n    Z2 = Z2.reshape(-1,1)\n    B2 = B2.reshape(-1,1) # second layer\n    Z2 = Z2 + B2\n    A2 = relu(Z2)\n\n    Z3 = W3.dot(A2) \n    Z3 = Z3.reshape(-1,1) # third layer(output)\n    B3 = B3.reshape(-1,1)\n    Z3 = Z3 + B3\n    #A3 = np.argmax(Z3,0)\n    A3 = Z3\n    if A3[0] > 0.5:\n        A3[0] = 1\n    else:\n        A3[0] = 0\n    #print(\"z3:\", Z3)\n    return Z1, A1, Z2, A2, Z3, A3\n\ndef backward_propagation( Z1, A1, Z2, A2, Z3, A3, W1, W2, W3, X, Y, m):\n    \n    dZ3 = A3 - Y\n    dZ3 = dZ3.reshape(-1,1)\n    #print(dZ3.shape)\n    dW3 = dZ3.dot(A2.T)  # output layer \n    dB3 = np.sum(dZ3)\n\n    dZ2 = W3.T.dot(dZ3) * reluPrime(Z2)  # 2nd hidden layer\n    dW2 = dZ2.dot(A1.T) \n    dB2 = np.sum(dZ2)\n\n    dZ1 = W2.T.dot(dZ2) * reluPrime(Z1) # first hidden layer\n    dW1 = dZ1.dot(X.T)\n    dB1 = np.sum(dZ1)\n \n    return dW1, dB1, dW2, dB2, dW3, dB3\n\ndef update_weights_and_biases( W1, B1, W2, B2, W3, B3, dW1, dB1, dW2, dB2, dW3, dB3, alpha):\n    W1 = W1 - alpha*dW1\n    B1 = B1 - alpha*dB1\n    \n    W2 = W2 - alpha*dW2\n    B2 = B2 - alpha*dB2\n    \n    W3 = W3 - alpha*dW3\n    B3 = B3 - alpha*dB3\n    return W1, B1, W2, B2, W3, B3\n\n\ndef gradient_descent(alpha, iterations, sizes):\n    W1, B1, W2, B2, W3, B3 = init_params(sizes)\n    correct = 0\n    totalCorrect = 0\n    for x in range (0, iterations):\n        \n        #Y=Y_new4579[x]\n        #X = X_new4579[x]\n        \n        Y=Y_new1099[x]\n        X = X_new1099[x]\n        \n       \n        X = np.array(X)\n        X = X.reshape(-1,1)\n        #print(type(X), X.shape)\n        #print(X)\n        #Y = Y_4579[x]\n        #Y.reshape(-1,1)\n        Z1, A1, Z2, A2, Z3, A3 = forward_propagation(W1, B1, W2, B2, W3, B3, X)\n        dW1, dB1, dW2, dB2, dW3, dB3 = backward_propagation(Z1, A1, Z2, A2, Z3, A3, W1, W2, W3, X, Y, x+1)\n        W1, B1, W2, B2, W3, B3 = update_weights_and_biases(W1, B1, W2, B2, W3, B3, dW1, dB1, dW2, dB2, dW3, dB3, alpha)\n        print(\"pred:\", A3, \" \", Y)\n        #Z1, A1, Z2, A2, Z3, A3 = forward_propagation(W1, B1, W2, B2, W3, B3, X)\n        #dW1, dB1, dW2, dB2, dW3, dB3 = backward_propagation(Z1, A1, Z2, A2, Z3, A3, W1, W2, W3, X, Y, x+1)\n        #W1, B1, W2, B2, W3, B3 = update_weights_and_biases(W1, B1, W2, B2, W3, B3, dW1, dB1, dW2, dB2, dW3, dB3, alpha)\n        \n        if x % 50 == 0 and x != 0:\n            print(\"Accuracy: \", (correct/50))\n            correct = 0\n            \n            \n        if(A3 == Y ):\n            correct+=1\n            totalCorrect+=1\n            \n    print(\"total%: \", totalCorrect/iterations)\n    return W1, B1, W2, B2, W3, B3","metadata":{"execution":{"iopub.status.busy":"2024-04-19T20:33:43.061126Z","iopub.execute_input":"2024-04-19T20:33:43.061406Z","iopub.status.idle":"2024-04-19T20:33:43.087730Z","shell.execute_reply.started":"2024-04-19T20:33:43.061382Z","shell.execute_reply":"2024-04-19T20:33:43.086965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"W1, B1, W2, B2, W3, B3 = gradient_descent(0.03, 1526, [100,100])\n\ndef make_predic(W1, B1, W2, B2, W3, B3, X):\n    _, _, _, _, _, A3 = forward_propagation(W1, B1, W2, B2, W3, B3, X)\n    prediction = A3\n    return prediction\n\n \n\ntotal = 4550\ncorrect = 0\nfor x in range (total):\n    \n    #Y = Y_n1099[x]\n    #X = X_n1099[x]\n    \n    Y=Y_new4579[x]\n    X=X_new4579[x]\n    \n    A3 = make_predic(W1, B1, W2, B2, W3, B3, X)\n    #print(A3, Y)\n    if A3 == Y:\n        correct+=1\n\nprint(\"Total:\", correct/total)\n    ","metadata":{"execution":{"iopub.status.busy":"2024-04-19T20:33:43.089002Z","iopub.execute_input":"2024-04-19T20:33:43.089585Z","iopub.status.idle":"2024-04-19T20:33:43.684976Z","shell.execute_reply.started":"2024-04-19T20:33:43.089555Z","shell.execute_reply":"2024-04-19T20:33:43.684132Z"},"trusted":true},"execution_count":null,"outputs":[]}]}