{"id":18835352,"url":"https://github.com/ashwanikumar04/artificial-neural-network","last_synced_at":"2026-01-26T20:30:13.564Z","repository":{"id":88929055,"uuid":"93264902","full_name":"ashwanikumar04/artificial-neural-network","owner":"ashwanikumar04","description":"A simple artifical neural network","archived":false,"fork":false,"pushed_at":"2018-06-19T03:34:30.000Z","size":275,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-12-30T07:44:10.463Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ashwanikumar04.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-06-03T17:53:24.000Z","updated_at":"2023-06-17T19:29:54.000Z","dependencies_parsed_at":"2023-03-13T18:17:40.074Z","dependency_job_id":null,"html_url":"https://github.com/ashwanikumar04/artificial-neural-network","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ashwanikumar04%2Fartificial-neural-network","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ashwanikumar04%2Fartificial-neural-network/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ashwanikumar04%2Fartificial-neural-network/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ashwanikumar04%2Fartificial-neural-network/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ashwanikumar04","download_url":"https://codeload.github.com/ashwanikumar04/artificial-neural-network/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239770023,"owners_count":19693983,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-08T02:15:56.279Z","updated_at":"2026-01-26T20:30:13.497Z","avatar_url":"https://github.com/ashwanikumar04.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"This is based on the content provided [here](https://www.udemy.com/deeplearning/learn/v4/overview) and simplified with comments for personal understanding.\nThis is a simple ANN which predicts if a customer will leave the bank or not.\n\n## Data loading\nWe will use Pandas to load the data in csv\n\n\n```python\nimport pandas as pd\ndataset = pd.read_csv(\"data.csv\")\n\n#We are taking only the independent variables which impact the dependent variable (i.e. if the customer leaves the banks)\nX = dataset.iloc[:,3:13].values\n\ny = dataset.iloc[:,13].values\n```\n\n## Encoding\nNow, we need to encode categorical data, as in mathematical equations we need to use only numerical values.\nWe encode all the independent variables which are non numeric.\nIn our current case, we need to encode ```Geography``` and ```Gender```\n\n\n```python\n# Encoding categorical values\nfrom sklearn.preprocessing import LabelEncoder, OneHotEncoder\nlabelencoder_X_1 = LabelEncoder()\nX[:,1]= labelencoder_X_1.fit_transform(X[:,1])\nlabelencoder_X_2 = LabelEncoder()\nX[:,2]= labelencoder_X_2.fit_transform(X[:,2])\n\n```\n\nWe need to include dummy variables to avoid un-necessary preference to any encoded value as all the variables are of same importance.\n\n\n```python\nonehotencoder = OneHotEncoder(categorical_features=[1])\nX = onehotencoder.fit_transform(X).toarray()\n```\n\n### Avoid dummy variable trap\nWe need to avoid dummy variable trap. More info on dummy variable trap is [here](http://www.algosome.com/articles/dummy-variable-trap-regression.html)\n\n\n```python\n#Done to avoid Dummy variable trap. Reducing dummy variable by one\nX = X[:,1:]\n```\n\n## Data split\nSplitting the data in training and test set\n\n\n```python\n#Splitting the dataset into the training and test set\nfrom sklearn.model_selection import train_test_split\nX_train, X_test, y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=0)\n\n```\n\n## Feature Scaling\nFeature scaling is done to avoid dominance of one independent variable on others.\nWe need to fit and transform the training dataset.\nBut test dataset is only transformmed.\n\n\n```python\nfrom sklearn.preprocessing import StandardScaler\nsc=StandardScaler()\nX_train = sc.fit_transform(X_train)\nX_test = sc.transform(X_test)\n```\n\n## Artificial Neural Network\nNow we will create ANN with following approach\n- Randomly initialise the weights to small numbers close to 0 (but not 0)\n- Input the first observation of your dataset in the input layer, each feature in one input node.\n- Forward-Propagation from left to right, the neurons are activated in a way that the impact of each neuron's activation is limited by the weights. Propagate the activations until getting the predicted result y.\n- Compare the predicted result to the actual result. Measure the generated error.\n- Back-Progpagation: from right to left, the error is back-propagated so that the weights are updated accordingly.\n- Repeat Steps 1-5 till whole data set is exhausted.\n\n\n```python\n#ANN \nimport keras\nfrom keras.models import Sequential\nfrom keras.layers import Dense\nfrom keras.layers import Dropout\nclassifier = Sequential()\n\n#Adding the input layer\n#Number of nodes in hiddedn layer = average of number of nodes in input layer and number of nodes in output layer\n#It is recommended to use RELU for hidden layers and Sigmoid (for two categories)/Softmax (for more than two categories) for output layer\nclassifier.add(Dense(activation=\"relu\", input_dim=11, units=6, kernel_initializer=\"uniform\"))\nclassifier.add(Dropout(rate=0.1))\n#Adding second hidden layer \nclassifier.add(Dense(activation=\"relu\", units=6, kernel_initializer=\"uniform\"))\n#Adding Dropout to avoid overfitting\nclassifier.add(Dropout(rate=0.1))\n#Adding output layer \nclassifier.add(Dense(activation=\"sigmoid\", units=1, kernel_initializer=\"uniform\"))\n\n# Compiling the ANN\n# If dependent variable is binary then loss = binary_crossentropy else loss = categorical_crossentropy\nclassifier.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])\nclassifier.fit(X_train,y_train,batch_size=10,epochs=100)\ny_pred = classifier.predict(X_test)\ny_pred = (y_pred\u003e0.5)\n\n```\n\n    Epoch 1/100\n    8000/8000 [==============================] - 2s - loss: 0.4904 - acc: 0.7954     \n    Epoch 2/100\n    8000/8000 [==============================] - 2s - loss: 0.4357 - acc: 0.7960     \n    Epoch 3/100\n    8000/8000 [==============================] - 2s - loss: 0.4333 - acc: 0.7960     \n    Epoch 4/100\n    8000/8000 [==============================] - 2s - loss: 0.4330 - acc: 0.7960     \n    Epoch 5/100\n    8000/8000 [==============================] - 2s - loss: 0.4277 - acc: 0.7960     \n    .\n    .\n    . \n    Epoch 95/100\n    8000/8000 [==============================] - 2s - loss: 0.4204 - acc: 0.8295     \n    Epoch 96/100\n    8000/8000 [==============================] - 2s - loss: 0.4206 - acc: 0.8306     \n    Epoch 97/100\n    8000/8000 [==============================] - 2s - loss: 0.4219 - acc: 0.8325     \n    Epoch 98/100\n    8000/8000 [==============================] - 2s - loss: 0.4209 - acc: 0.8305     \n    Epoch 99/100\n    8000/8000 [==============================] - 2s - loss: 0.4194 - acc: 0.8304     \n    Epoch 100/100\n    8000/8000 [==============================] - 2s - loss: 0.4206 - acc: 0.8300     \n\n```python\nimport numpy as np\nnew_prediction = classifier.predict(sc.transform(np.array([[0.,0,600,1,40,3,60000,2,1,1,50000]])))\nnew_prediction=(new_prediction\u003e0.5)\nprint(\"The customer will leave\" if new_prediction else\"The customer will not leave\")\n```\n\n    The customer will not leave\n\n\nThis is a simple ANN to determine if a customer will leave the bank or not.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fashwanikumar04%2Fartificial-neural-network","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fashwanikumar04%2Fartificial-neural-network","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fashwanikumar04%2Fartificial-neural-network/lists"}