├── neural_network ├── GANs-PyTorch-Vanilla-LS-DC │ ├── README.md │ ├── gan-checks-tf.npz │ └── gan_outputs_pytorch.png ├── RNN │ └── Google_test.csv ├── 02-imdb-binary-classification.ipynb ├── qlearning.ipynb └── CNN-using keras.ipynb ├── assets └── images │ ├── clone.png │ ├── fork.png │ ├── url.JPG │ ├── git-status.png │ ├── compare-and-pull.png │ └── submit-pull-request.png ├── machine_learning ├── Piecewise │ └── peppers.tif ├── random_forest_regression │ ├── __pycache__ │ │ └── random_forest_regression.cpython-37.pyc │ ├── Position_Salaries.csv │ ├── random_forest_regression.py │ └── random_forest_regression.ipynb ├── random_forest_classification │ ├── __pycache__ │ │ └── random_forest_classification.cpython-37.pyc │ ├── random_forest_classification.py │ └── Social_Network_Ads.csv ├── Associative Mining │ ├── grocery_data.csv │ └── transformed_data.csv ├── Natural language processing │ ├── intents.json │ └── Movie_recommendation_Sentance_Embedding.ipynb ├── Basics of tensorflow │ └── Basic of TensorFlow.ipynb ├── Decision Tree regression with k-fold cross validation │ └── 2019.csv ├── dbscan │ └── dbscan.py ├── Linear_Regression │ └── linear_regression_using_pandas.ipynb ├── Naive Bayes Classification │ └── Customer_Behaviour.csv └── Decision tree │ └── Decision_Tree.ipynb ├── requirements.txt ├── .gitpod.yml ├── gradient_descent.py ├── LICENSE.md ├── README.md ├── numerical_methods ├── the_trapezium_method.ipynb ├── the_Simpson’s_method.ipynb ├── the_rectangular_method.ipynb ├── euler_method_for_the_Cauchy_problem.ipynb └── newton_forward_divided_difference_formula.ipynb ├── Automaton ├── ScrapNewsfromIndiaToday.ipynb ├── Netflix_Scrapper.ipynb └── Pushdown_Automata_Implementation.ipynb ├── .github └── workflows │ └── directory_workflow.yml ├── CODE_OF_CONDUCT.md ├── Contributing.md ├── DIRECTORY.md └── .gitignore /neural_network/GANs-PyTorch-Vanilla-LS-DC/README.md: -------------------------------------------------------------------------------- 1 | # GANs 2 | Using PyTorch 3 | -------------------------------------------------------------------------------- /assets/images/clone.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TheAlgorithms/Jupyter/HEAD/assets/images/clone.png -------------------------------------------------------------------------------- /assets/images/fork.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TheAlgorithms/Jupyter/HEAD/assets/images/fork.png -------------------------------------------------------------------------------- /assets/images/url.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TheAlgorithms/Jupyter/HEAD/assets/images/url.JPG -------------------------------------------------------------------------------- /assets/images/git-status.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TheAlgorithms/Jupyter/HEAD/assets/images/git-status.png -------------------------------------------------------------------------------- /assets/images/compare-and-pull.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TheAlgorithms/Jupyter/HEAD/assets/images/compare-and-pull.png -------------------------------------------------------------------------------- /assets/images/submit-pull-request.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TheAlgorithms/Jupyter/HEAD/assets/images/submit-pull-request.png -------------------------------------------------------------------------------- /machine_learning/Piecewise/peppers.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TheAlgorithms/Jupyter/HEAD/machine_learning/Piecewise/peppers.tif -------------------------------------------------------------------------------- /neural_network/GANs-PyTorch-Vanilla-LS-DC/gan-checks-tf.npz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TheAlgorithms/Jupyter/HEAD/neural_network/GANs-PyTorch-Vanilla-LS-DC/gan-checks-tf.npz -------------------------------------------------------------------------------- /neural_network/GANs-PyTorch-Vanilla-LS-DC/gan_outputs_pytorch.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TheAlgorithms/Jupyter/HEAD/neural_network/GANs-PyTorch-Vanilla-LS-DC/gan_outputs_pytorch.png -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | tensorflow 2 | opencv-python 3 | keras 4 | matplotlib 5 | mlxtend 6 | numpy 7 | pandas 8 | requests 9 | seaborn 10 | sklearn 11 | tabulate 12 | scikit-fuzzy 13 | pillow 14 | beautifulsoup4 15 | skia-python -------------------------------------------------------------------------------- /machine_learning/random_forest_regression/__pycache__/random_forest_regression.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TheAlgorithms/Jupyter/HEAD/machine_learning/random_forest_regression/__pycache__/random_forest_regression.cpython-37.pyc -------------------------------------------------------------------------------- /machine_learning/random_forest_classification/__pycache__/random_forest_classification.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TheAlgorithms/Jupyter/HEAD/machine_learning/random_forest_classification/__pycache__/random_forest_classification.cpython-37.pyc -------------------------------------------------------------------------------- /machine_learning/random_forest_regression/Position_Salaries.csv: -------------------------------------------------------------------------------- 1 | Position,Level,Salary 2 | Business Analyst,1,45000 3 | Junior Consultant,2,50000 4 | Senior Consultant,3,60000 5 | Manager,4,80000 6 | Country Manager,5,110000 7 | Region Manager,6,150000 8 | Partner,7,200000 9 | Senior Partner,8,300000 10 | C-level,9,500000 11 | CEO,10,1000000 -------------------------------------------------------------------------------- /.gitpod.yml: -------------------------------------------------------------------------------- 1 | tasks: 2 | - init: pip install -r ./requirements.txt 3 | 4 | vscode: 5 | extensions: 6 | - almenon.arepl 7 | - ms-toolsai.jupyter 8 | 9 | github: 10 | prebuilds: 11 | master: true 12 | pullRequests: true 13 | addCheck: true 14 | addBadge: true 15 | pullRequestsFromForks: true 16 | -------------------------------------------------------------------------------- /machine_learning/Associative Mining/grocery_data.csv: -------------------------------------------------------------------------------- 1 | products 2 | "MILK,BREAD,BISCUIT" 3 | "BREAD,MILK,BISCUIT,CORNFLAKES" 4 | "BREAD,TEA,BOURNVITA" 5 | "JAM,MAGGI,BREAD,MILK" 6 | "MAGGI,TEA,BISCUIT" 7 | "BREAD,TEA,BOURNVITA" 8 | "MAGGI,TEA,CORNFLAKES" 9 | "MAGGI,BREAD,TEA,BISCUIT" 10 | "JAM,MAGGI,BREAD,TEA" 11 | "BREAD,MILK" 12 | "COFFEE,COCK,BISCUIT,CORNFLAKES" 13 | "COFFEE,COCK,BISCUIT,CORNFLAKES" 14 | "COFFEE,SUGER,BOURNVITA" 15 | "BREAD,COFFEE,COCK" 16 | "BREAD,SUGER,BISCUIT" 17 | "COFFEE,SUGER,CORNFLAKES" 18 | "BREAD,SUGER,BOURNVITA" 19 | "BREAD,COFFEE,SUGER" 20 | "BREAD,COFFEE,SUGER" 21 | "TEA,MILK,COFFEE,CORNFLAKES" 22 | -------------------------------------------------------------------------------- /machine_learning/Associative Mining/transformed_data.csv: -------------------------------------------------------------------------------- 1 | BISCUIT,BOURNVITA,BREAD,COCK,COFFEE,CORNFLAKES,JAM,MAGGI,MILK,SUGER,TEA 2 | 1,0,1,0,0,0,0,0,1,0,0 3 | 1,0,1,0,0,1,0,0,1,0,0 4 | 0,1,1,0,0,0,0,0,0,0,1 5 | 0,0,1,0,0,0,1,1,1,0,0 6 | 1,0,0,0,0,0,0,1,0,0,1 7 | 0,1,1,0,0,0,0,0,0,0,1 8 | 0,0,0,0,0,1,0,1,0,0,1 9 | 1,0,1,0,0,0,0,1,0,0,1 10 | 0,0,1,0,0,0,1,1,0,0,1 11 | 0,0,1,0,0,0,0,0,1,0,0 12 | 1,0,0,1,1,1,0,0,0,0,0 13 | 1,0,0,1,1,1,0,0,0,0,0 14 | 0,1,0,0,1,0,0,0,0,1,0 15 | 0,0,1,1,1,0,0,0,0,0,0 16 | 1,0,1,0,0,0,0,0,0,1,0 17 | 0,0,0,0,1,1,0,0,0,1,0 18 | 0,1,1,0,0,0,0,0,0,1,0 19 | 0,0,1,0,1,0,0,0,0,1,0 20 | 0,0,1,0,1,0,0,0,0,1,0 21 | 0,0,0,0,1,1,0,0,1,0,1 22 | -------------------------------------------------------------------------------- /gradient_descent.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | def gradient_descent(x,y): 4 | m_curr = b_curr = 0 5 | iterations = 10000 6 | n = len(x) 7 | learning_rate = 0.08 8 | 9 | for i in range(iterations): 10 | y_predicted = m_curr * x + b_curr 11 | cost = (1/n) * sum([val**2 for val in (y-y_predicted)]) 12 | md = -(2/n)*sum(x*(y-y_predicted)) 13 | bd = -(2/n)*sum(y-y_predicted) 14 | m_curr = m_curr - learning_rate * md 15 | b_curr = b_curr - learning_rate * bd 16 | print ("m {}, b {}, cost {} iteration {}".format(m_curr,b_curr,cost, i)) 17 | 18 | x = np.array([1,2,3,4,5]) 19 | y = np.array([5,7,9,11,13]) 20 | 21 | gradient_descent(x,y) -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 The Algorithms 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /machine_learning/random_forest_regression/random_forest_regression.py: -------------------------------------------------------------------------------- 1 | # Random Forest Regression 2 | 3 | # Importing the libraries 4 | import os 5 | import numpy as np 6 | import matplotlib.pyplot as plt 7 | import pandas as pd 8 | 9 | # Importing the dataset 10 | script_dir = os.path.dirname(os.path.realpath(__file__)) 11 | dataset = pd.read_csv(os.path.join(script_dir, 'Position_Salaries.csv')) 12 | X = dataset.iloc[:, 1:2].values 13 | y = dataset.iloc[:, 2].values 14 | 15 | # Splitting the dataset into the Training set and Test set 16 | """from sklearn.cross_validation import train_test_split 17 | X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)""" 18 | 19 | # Feature Scaling 20 | """from sklearn.preprocessing import StandardScaler 21 | sc_X = StandardScaler() 22 | X_train = sc_X.fit_transform(X_train) 23 | X_test = sc_X.transform(X_test) 24 | sc_y = StandardScaler() 25 | y_train = sc_y.fit_transform(y_train)""" 26 | 27 | # Fitting Random Forest Regression to the dataset 28 | from sklearn.ensemble import RandomForestRegressor 29 | regressor = RandomForestRegressor(n_estimators = 10, random_state = 0) 30 | regressor.fit(X, y) 31 | 32 | # Predicting a new result 33 | y_pred = regressor.predict([[6.5]]) 34 | 35 | # Visualising the Random Forest Regression results (higher resolution) 36 | X_grid = np.arange(min(X), max(X), 0.01) 37 | X_grid = X_grid.reshape((len(X_grid), 1)) 38 | plt.scatter(X, y, color = 'red') 39 | plt.plot(X_grid, regressor.predict(X_grid), color = 'blue') 40 | plt.title('Truth or Bluff (Random Forest Regression)') 41 | plt.xlabel('Position level') 42 | plt.ylabel('Salary') 43 | plt.show() 44 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## The Algorithms - Jupyter 2 | 3 | ![Repo size](https://img.shields.io/github/repo-size/TheAlgorithms/Jupyter) 4 | [![Gitpod ready-to-code](https://img.shields.io/badge/Gitpod-ready--to--code-blue?logo=gitpod)](https://gitpod.io/#https://github.com/TheAlgorithms/Jupyter) 5 | ![Number of issues](https://img.shields.io/github/issues/TheAlgorithms/Jupyter?color=green) 6 | ![Number of PRs](https://img.shields.io/github/issues-pr/TheAlgorithms/Jupyter?color=green)
7 | ![Latest commit](https://img.shields.io/github/last-commit/TheAlgorithms/Jupyter) 8 | ![All contributors](https://img.shields.io/github/contributors/TheAlgorithms/Jupyter) 9 | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/TheAlgorithms/Jupyter/master) 10 | 11 | ## Getting started 12 | 13 | You can run and edit the algorithms or contribute to them using [Gitpod.io](https://www.gitpod.io/), a free online development environment, with a single click. 14 | 15 | [![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](http://gitpod.io/#https://github.com/TheAlgorithms/Jupyter) 16 | 17 | ## Contributing New Algorithms 18 | 19 | * Make your pull requests to be **specific** and **focused**. Instead of contributing "several algorithms" all at once contribute them all one by one separately (i.e. one pull request for "Logistic Regression", another one for "K-Means", and so on). 20 | 21 | * Every new algorithm must have: 22 | * **Source code** with comments and readable namings 23 | * **Math** being explained in README.md along with the code 24 | * **Jupyter demo notebook** with an example of how this new algorithm may be applied 25 | 26 | If you're adding new **datasets** they need to be saved in the `/data` folder. CSV files are preferable. The size of the file should not be greater than `30Mb`. 27 | 28 | ## Contributing 29 | 30 | Before removing any bug, or adding new algorithms please read our **[Contribution Guidelines](Contributing.md)** and our **[Code of Conduct](CODE_OF_CONDUCT.md)**. 31 | 32 | ## License 33 | 34 | All the code is licensed under the [MIT License](LICENSE.md) 35 | -------------------------------------------------------------------------------- /numerical_methods/the_trapezium_method.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "The trapezium rule is a way of estimating the area under a curve. We know that the area under a curve is given by integration, so the trapezium rule gives a method of estimating integrals." 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Let's check this method for the next function: $$f(x) = ({e^x / 2})*(cos(x)-sin(x))$$ with $\\varepsilon = 0.001$" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": {}, 21 | "outputs": [ 22 | { 23 | "name": "stdout", 24 | "output_type": "stream", 25 | "text": [ 26 | "Result: -22.12539445092147\n" 27 | ] 28 | } 29 | ], 30 | "source": [ 31 | "import math \n", 32 | "import numpy as np\n", 33 | "\n", 34 | "n = 4 \n", 35 | "a = 2.\n", 36 | "b = 3.\n", 37 | "def f(x):\n", 38 | " return (math.e**x / 2)*(math.cos(x)-math.sin(x))\n", 39 | "\n", 40 | "def trapezoid(a,b,n):\n", 41 | " z = (b-a)/n\n", 42 | " i=a\n", 43 | " s=0\n", 44 | " while (i+z) Iterator[str]: 24 | fs_exts = tuple(".ipynb .py".split()) 25 | for dirpath, dirnames, filenames in os.walk(top_dir): 26 | dirnames[:] = [d for d in dirnames if d[0] not in "._"] 27 | for filename in filenames: 28 | if os.path.splitext(filename)[1].lower() in fs_exts: 29 | yield os.path.join(dirpath, filename).lstrip("./") 30 | def md_prefix(i): 31 | return f"{i * ' '}*" if i else "\n##" 32 | def print_path(old_path: str, new_path: str) -> str: 33 | global g_output 34 | old_parts = old_path.split(os.sep) 35 | for i, new_part in enumerate(new_path.split(os.sep)): 36 | if i + 1 > len(old_parts) or old_parts[i] != new_part: 37 | if new_part: 38 | g_output.append(f"{md_prefix(i)} {new_part.replace('_', ' ').title()}") 39 | return new_path 40 | def build_directory_md(top_dir: str = ".") -> str: 41 | global g_output 42 | old_path = "" 43 | for filepath in sorted(good_filepaths(), key=str.lower): 44 | filepath, filename = os.path.split(filepath) 45 | if filepath != old_path: 46 | old_path = print_path(old_path, filepath) 47 | indent = (filepath.count(os.sep) + 1) if filepath else 0 48 | url = "/".join((URL_BASE, filepath, filename)).replace(" ", "%20") 49 | filename = os.path.splitext(filename.replace("_", " ").title())[0] 50 | g_output.append(f"{md_prefix(indent)} [{filename}]({url})") 51 | return "# List of all files\n" + "\n".join(g_output) 52 | with open("DIRECTORY.md", "w") as out_file: 53 | out_file.write(build_directory_md(".") + "\n") 54 | - name: Commit DIRECTORY.md 55 | run: | 56 | git commit -m "updating DIRECTORY.md" DIRECTORY.md || true 57 | git diff DIRECTORY.md 58 | git push --force origin HEAD:$GITHUB_REF || true 59 | -------------------------------------------------------------------------------- /machine_learning/random_forest_classification/random_forest_classification.py: -------------------------------------------------------------------------------- 1 | # Random Forest Classification 2 | 3 | # Importing the libraries 4 | import os 5 | import numpy as np 6 | import matplotlib.pyplot as plt 7 | import pandas as pd 8 | 9 | # Importing the dataset 10 | script_dir = os.path.dirname(os.path.realpath(__file__)) 11 | dataset = pd.read_csv(os.path.join(script_dir, 'Social_Network_Ads.csv')) 12 | X = dataset.iloc[:, [2, 3]].values 13 | y = dataset.iloc[:, 4].values 14 | 15 | # Splitting the dataset into the Training set and Test set 16 | from sklearn.model_selection import train_test_split 17 | X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0) 18 | 19 | # Feature Scaling 20 | from sklearn.preprocessing import StandardScaler 21 | sc = StandardScaler() 22 | X_train = sc.fit_transform(X_train) 23 | X_test = sc.transform(X_test) 24 | 25 | # Fitting Random Forest Classification to the Training set 26 | from sklearn.ensemble import RandomForestClassifier 27 | classifier = RandomForestClassifier(n_estimators = 10, criterion = 'entropy', random_state = 0) 28 | classifier.fit(X_train, y_train) 29 | 30 | # Predicting the Test set results 31 | y_pred = classifier.predict(X_test) 32 | 33 | # Making the Confusion Matrix 34 | from sklearn.metrics import confusion_matrix 35 | cm = confusion_matrix(y_test, y_pred) 36 | 37 | # Visualising the Training set results 38 | from matplotlib.colors import ListedColormap 39 | X_set, y_set = X_train, y_train 40 | X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01), 41 | np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01)) 42 | plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape), 43 | alpha = 0.75, cmap = ListedColormap(('red', 'green'))) 44 | plt.xlim(X1.min(), X1.max()) 45 | plt.ylim(X2.min(), X2.max()) 46 | for i, j in enumerate(np.unique(y_set)): 47 | plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1], 48 | c = ListedColormap(('red', 'green'))(i), label = j) 49 | plt.title('Random Forest Classification (Training set)') 50 | plt.xlabel('Age') 51 | plt.ylabel('Estimated Salary') 52 | plt.legend() 53 | plt.show() 54 | 55 | # Visualising the Test set results 56 | from matplotlib.colors import ListedColormap 57 | X_set, y_set = X_test, y_test 58 | X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01), 59 | np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01)) 60 | plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape), 61 | alpha = 0.75, cmap = ListedColormap(('red', 'green'))) 62 | plt.xlim(X1.min(), X1.max()) 63 | plt.ylim(X2.min(), X2.max()) 64 | for i, j in enumerate(np.unique(y_set)): 65 | plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1], 66 | c = ListedColormap(('red', 'green'))(i), label = j) 67 | plt.title('Random Forest Classification (Test set)') 68 | plt.xlabel('Age') 69 | plt.ylabel('Estimated Salary') 70 | plt.legend() 71 | plt.show() 72 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | ## Code of Conduct 2 | 3 | ### Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, gender identity and expression, level of experience, 9 | nationality, personal appearance, race, religion, or sexual identity and 10 | orientation. 11 | 12 | ### Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | - Using welcoming and inclusive language 18 | - Being respectful of differing viewpoints and experiences 19 | - Gracefully accepting constructive criticism 20 | - Focusing on what is best for the community 21 | - Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | - The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | - Trolling, insulting/derogatory comments, and personal or political attacks 28 | - Public or private harassment 29 | - Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | - Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ### Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ### Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ### Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at [cclauss](https://github.com/cclauss). All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | -------------------------------------------------------------------------------- /machine_learning/Natural language processing/intents.json: -------------------------------------------------------------------------------- 1 | { 2 | "intents": [{ 3 | "tag": "greeting", 4 | "patterns": ["Hi", "How are you", "Is anyone there?", "Hello", "Good day", "Whats up"], 5 | "responses": ["Hello!", "Good to see you again!", "Hi there, how can I help?"], 6 | "context_set": "" 7 | }, 8 | { 9 | "tag": "action and advanture", 10 | "patterns": [" Elements of action/adventure (car chases, shootouts, explosions) and thriller", "Combines action set-pieces with serious themes, character insight and/or emotional power", "I like action movies", "i like fighting movies", "action sequences, such as fighting, stunts, car chases or explosions", "Fighting is really awesome"], 11 | "responses": ["I also like action movies :)", "ohh nice", "yeah action movie is awesome"], 12 | "context_set": "" 13 | }, 14 | { 15 | "tag": "comedy", 16 | "patterns": ["These films are designed to make the audience laugh through amusement", "very first silent movies were comedies, as slapstick comedy often relies on visual depictions", "with many former stand-up comics transitioning to the film industry", "satirical comedy-drama & the plot is", "i like comedy movies", "comedy is really love to watch", "laughing is the best medicine", "i love everyday"], 17 | "responses": ["I also like comedy movies :)", "ohh nice", "yeah laughing is better for health"], 18 | "context_set": "" 19 | }, 20 | { 21 | "tag": "Horror", 22 | "patterns": ["A horror film is a film that seeks to elicit fear for entertainment purposes", "horror has existed as a film genre for more than a century", "i like Horror movie", "Horror movies are so thrilled", "an intense feeling of fear, shock, or disgust.", "I like intense and fear movies", "Horror films effectively center on the dark side of life", "i like dark movies", "Horror Films are unsettling films designed to frighten and panic,monster"], 23 | "responses": ["ohh i think you like science fiction kind of horror", "Ohhh i also dark movie", "Hmm horror movies are so thriilled"], 24 | "context_set": "" 25 | }, 26 | { 27 | "tag": "Romance", 28 | "patterns": ["romance movies are romantic love stories", "romance movie focus on passion, emotion, and the affectionate romantic involvement", "Romance movie strong and pure love and romance the main plot focus", "i love romantic and love movies", "romantic love, obsessive love, sentimental love, spiritual love, forbidden love/romance, platonic love, sexual and passionate love, sacrificial love, explosive and destructive love", "i want to watch Deep and true romantic love between two people", "i like "], 29 | "responses": ["Romantic movie are just awesome", "Yeahh i like true love movies", "Romance is everywhere :)", "I recommond don't watch with family :("], 30 | "context_set": "" 31 | }, 32 | { 33 | "tag": "Accept", 34 | "patterns": ["ohh really", "nice", "ohhh"], 35 | "responses": ["yess", "yeah", "yepp", "yeah that's true"], 36 | "context_set": "" 37 | } 38 | ] 39 | } -------------------------------------------------------------------------------- /numerical_methods/the_rectangular_method.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "The rectangle method (also called the midpoint rule) is the simplest method in Mathematics used to compute an approximation of a definite integral." 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Let's check this method for the next function: $$f(x) = ({e^x / 2})*(cos(x)-sin(x))$$ with $\\varepsilon = 0.001$" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "import math \n", 24 | "import numpy as np\n", 25 | "\n", 26 | "def integration(a,b,n):\n", 27 | " h = (b-a)/n\n", 28 | " r = f(a) + f(b)\n", 29 | " i = 1\n", 30 | " while i < n:\n", 31 | " x = a + i*h\n", 32 | " r = r + 4 * f(x)\n", 33 | " i = i + 1\n", 34 | " x = a + i * h\n", 35 | " r = r +2*f(x)\n", 36 | " i = i + 1\n", 37 | " r = r * h / 3\n", 38 | " print(\"Result: \", r) \n", 39 | "\n", 40 | "def rectangles(a,b,n):\n", 41 | " \n", 42 | " z = (b-a)/n\n", 43 | " i = a\n", 44 | " s1=0\n", 45 | " s2=0\n", 46 | " while i 13 | 14 | ## Fork this repository 15 | 16 | Fork this repository by clicking on the fork button on the top of this page. 17 | This will create a copy of this repository in your account. 18 | 19 | ## Clone the repository 20 | 21 | clone this repository 22 | 23 | Now clone the forked repository to your machine. Go to your GitHub account, open the forked repository, click on the code button and then click the _copy to clipboard_ icon. 24 | 25 | Open a terminal and run the following git command: 26 | 27 | ``` 28 | git clone "url you just copied" 29 | ``` 30 | 31 | where "url you just copied" (without the quotation marks) is the url to this repository (your fork of this project). See the previous steps to obtain the url. 32 | 33 | copy URL to clipboard 34 | 35 | For example: 36 | 37 | ``` 38 | git clone https://github.com/TheAlgorithms/Jupyter 39 | ``` 40 | 41 | where `this-is-you` is your GitHub username. Here you're copying the contents of the first-contributions repository on GitHub to your computer. 42 | 43 | ## Create a branch 44 | 45 | Change to the repository directory on your computer (if you are not already there): 46 | 47 | ``` 48 | cd Jupyter 49 | ``` 50 | 51 | Now create a branch using the `git checkout` command: 52 | 53 | ``` 54 | git checkout -b 55 | ``` 56 | 57 | For example: 58 | 59 | ``` 60 | git checkout -b add-alonzo-church 61 | ``` 62 | 63 | (The name of the branch does not need to have the word _add_ in it, but it's a reasonable thing to include because the purpose of this branch is to add your name to a list.) 64 | 65 | ## Make necessary changes and commit those changes 66 | 67 | Now open `Contributors.md` file in a text editor, add your name to it. Don't add it at the beginning or end of the file. Put it anywhere in between. Now, save the file. 68 | 69 | git status 70 | 71 | If you go to the project directory and execute the command `git status`, you'll see there are changes. 72 | 73 | Add those changes to the branch you just created using the `git add` command: 74 | 75 | ``` 76 | git add Contributors.md 77 | ``` 78 | 79 | Now commit those changes using the `git commit` command: 80 | 81 | ``` 82 | git commit -m "Add to Contributors list" 83 | ``` 84 | 85 | replacing `` with your name. 86 | 87 | ## Push changes to GitHub 88 | 89 | Push your changes using the command `git push`: 90 | 91 | ``` 92 | git push origin 93 | ``` 94 | 95 | replacing `` with the name of the branch you created earlier. 96 | 97 | ## Submit your changes for review 98 | 99 | If you go to your repository on GitHub, you'll see a `Compare & pull request` button. Click on that button. 100 | 101 | create a pull request 102 | 103 | Now submit the pull request. 104 | 105 | submit pull request 106 | 107 | Soon I'll be merging all your changes into the master branch of this project. You will get a notification email once the changes have been merged. 108 | 109 | ## Contributing new algorithms 110 | 111 | - Make your pull requests to be **specific** and **focused**. Instead of contributing "several algorithms" all at once contribute them all one by one separately (i.e. one pull request for "Logistic Regression", another one for "K-Means" and so on). 112 | 113 | - Every new algorithm must have: 114 | - **Source code** with comments and readable namings 115 | - **Math** being explained in README.md along with the code 116 | - **Jupyter demo notebook** with example of how this new algorithm may be applied 117 | 118 | If you're adding new **datasets** they need to be saved in the `/data` folder. CSV files are preferable. The size of the file should not be greater than `30Mb`. 119 | 120 | ## Where to go from here? 121 | 122 | Congrats! You just completed the standard _fork -> clone -> edit -> PR_ workflow that you'll encounter often as a contributor! 123 | 124 | ## Reference 125 | 126 | - [https://github.com/firstcontributions](https://github.com/firstcontributions) 127 | -------------------------------------------------------------------------------- /Automaton/Netflix_Scrapper.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Netflix Scrapper\n", 8 | "\n", 9 | "The purpose of the code is to get details of all the Categories on Netflix and then to gather information about Sub-Categories and movies under each Sub-Category." 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": null, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "from bs4 import BeautifulSoup\n", 19 | "import requests\n", 20 | "import pandas as pd\n", 21 | "import numpy as np" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": null, 27 | "metadata": {}, 28 | "outputs": [], 29 | "source": [ 30 | "def make_soup(url):\n", 31 | " return BeautifulSoup(requests.get(url).text, 'html.parser')" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": null, 37 | "metadata": {}, 38 | "outputs": [], 39 | "source": [ 40 | "def browseCategory(category, data):\n", 41 | " category_url = data[category-1][2]\n", 42 | " category = data[category-1][1]\n", 43 | " subCategory_details = []\n", 44 | " count = 1\n", 45 | " subCategories = []\n", 46 | " soup = make_soup(category_url)\n", 47 | " cards_list = soup.find_all('section',{'class':'nm-collections-row'})\n", 48 | " for card in cards_list:\n", 49 | " try:\n", 50 | " subCategory = card.find('h1').text\n", 51 | " movie_list = []\n", 52 | " movies = card.find_all('li')\n", 53 | " movie_count = 1\n", 54 | " for movie in movies:\n", 55 | " try:\n", 56 | " movie_title = movie.find('span',{'class':'nm-collections-title-name'}).text\n", 57 | " movie_link = movie.find('a').get('href')\n", 58 | " movie_list.append([movie_count, movie_title , movie_link])\n", 59 | " movie_count += 1\n", 60 | " except AttributeError:\n", 61 | " pass\n", 62 | " subCategories.append(subCategory)\n", 63 | " subCategory_details.append(movie_list)\n", 64 | " count += 1\n", 65 | " except AttributeError:\n", 66 | " pass\n", 67 | " return subCategories, subCategory_details, count-1" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": null, 73 | "metadata": {}, 74 | "outputs": [], 75 | "source": [ 76 | "def getCategories(base_url):\n", 77 | " category_soup = make_soup(base_url)\n", 78 | " categories = category_soup.find_all('section',{'class':'nm-collections-row'})\n", 79 | " result=[]\n", 80 | " count = 1\n", 81 | " for category in categories:\n", 82 | " try:\n", 83 | " Title = category.find('span', {'class':'nm-collections-row-name'}).text\n", 84 | " url = category.find('a').get('href')\n", 85 | " result.append([count, Title, url])\n", 86 | " count += 1\n", 87 | " except AttributeError:\n", 88 | " pass\n", 89 | " #print(result)\n", 90 | " return result" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": null, 96 | "metadata": {}, 97 | "outputs": [], 98 | "source": [ 99 | "def main():\n", 100 | " netflix_url = \"https://www.netflix.com/in/browse/genre/839338\"\n", 101 | " categories = getCategories(netflix_url)\n", 102 | " print(\"Please select one of the category\")\n", 103 | " df = pd.DataFrame(np.array(categories), columns=['Sr.No', 'Title', 'link'])\n", 104 | " print(df.to_string(index=False))\n", 105 | " choice = int(input('\\n\\n Please Enter your Choice: \\n'))\n", 106 | " subCategories, movieList, count = browseCategory(choice, categories)\n", 107 | " for i in range(0, count):\n", 108 | " print(subCategories[i],'\\n\\n')\n", 109 | " subCategory_df = pd.DataFrame(np.array(movieList[i]), columns=['Sr.No', 'Title', 'link'])\n", 110 | " print(subCategory_df.to_string(index=False))\n", 111 | " print(\"\\n\\n\\n\")\n", 112 | " \n", 113 | "if __name__ == '__main__':\n", 114 | " main()" 115 | ] 116 | } 117 | ], 118 | "metadata": { 119 | "kernelspec": { 120 | "display_name": "Python 3", 121 | "language": "python", 122 | "name": "python3" 123 | }, 124 | "language_info": { 125 | "codemirror_mode": { 126 | "name": "ipython", 127 | "version": 3 128 | }, 129 | "file_extension": ".py", 130 | "mimetype": "text/x-python", 131 | "name": "python", 132 | "nbconvert_exporter": "python", 133 | "pygments_lexer": "ipython3", 134 | "version": "3.7.4" 135 | } 136 | }, 137 | "nbformat": 4, 138 | "nbformat_minor": 2 139 | } 140 | -------------------------------------------------------------------------------- /Automaton/Pushdown_Automata_Implementation.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## A Simple Implementation of Push Down Automaton:" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## Defining the 6 tuples:\n", 15 | "> * Q-->finite set of sets\n", 16 | "> * Sigma-->finite set of input alphabet\n", 17 | "> * Gamma-->finite set of stack alphabet\n", 18 | "> * Delta-->transition relation\n", 19 | "> * Q0-->start state\n", 20 | "> * Z-->initial stack symbol\n", 21 | "> * F-->set of accepting states\n", 22 | "\n", 23 | "# Defining the states:\n", 24 | "\n", 25 | "* state 0: starting state\n", 26 | "\n", 27 | "* state 1:From state 0 whenever it sees a 1 or 2, it moves to state 1+pushes the element onto the stack\n", 28 | "* state 1:From state 1 whenever it sees a 1 or 2, it remains in state 1+pushes the element onto the stack\n", 29 | "\n", 30 | "* state 2:From state 1 whenever it sees a 0, it moves to state 2+pops from the stack\n", 31 | "* state 2:From state 1 whenever it sees a 0, it remains in state 2+pops from the stack\n", 32 | "\n", 33 | "* state 3:From state 0, if it sees a 0,it moves to state 3,the rejected state\n", 34 | "* state 3:From state 2, if it sees a 1 or 2 , it moves to state 3, the rejected state\n", 35 | "* state 3:If at the end, the stack is not empty, it moves to state 3,the rejected state\n" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 1, 41 | "metadata": {}, 42 | "outputs": [ 43 | { 44 | "name": "stdout", 45 | "output_type": "stream", 46 | "text": [ 47 | "Enter the String:123456\n", 48 | "The String is rejected\n" 49 | ] 50 | } 51 | ], 52 | "source": [ 53 | "#stack functions\n", 54 | "def push(a,list1):\n", 55 | " #pushing to the stack/adding to the top of the stack\n", 56 | " list1.append(a)\n", 57 | " return 1\n", 58 | "\n", 59 | "def pop(list1):\n", 60 | " #for poping from the stack/removing the top element of the stack\n", 61 | " index=len(list1)-1\n", 62 | " if (index>0):\n", 63 | " list1.pop(index)\n", 64 | " return 1\n", 65 | " else:\n", 66 | " return 0\n", 67 | "\n", 68 | "# Q={0,1,2,3}\n", 69 | "# Sigma={0,1,2}\n", 70 | "# Starting state={0}\n", 71 | "# Z=#\n", 72 | "# F={2}\n", 73 | "\n", 74 | "#setting the initial stack symbol\n", 75 | "stack=['#']\n", 76 | "#setting the starting state\n", 77 | "state=0\n", 78 | "\n", 79 | "#taking the input\n", 80 | "input_string=input('Enter the String:')\n", 81 | "\n", 82 | "#performing the operations\n", 83 | "l=len(input_string)\n", 84 | "i=0\n", 85 | "if l%2==0:\n", 86 | " while istate=1\")\n", 93 | " else :\n", 94 | " state=3\n", 95 | " #print(\">1.state=3\")\n", 96 | " break\n", 97 | " i+=1\n", 98 | " \n", 99 | " while i2.state=2\")\n", 107 | " pop(stack)\n", 108 | " else:\n", 109 | " state=3\n", 110 | " #print(\">2.state=3\")\n", 111 | " break\n", 112 | " i+=1\n", 113 | "else:\n", 114 | " state=3\n", 115 | " #print(\"3state=3\")\n", 116 | " \n", 117 | "if state==2 and len(stack)!=1:\n", 118 | " state=3\n", 119 | " \n", 120 | "#print(state)\n", 121 | "#print(len(stack))\n", 122 | "\n", 123 | "#checking the final state and displaying the result\n", 124 | "if(state==2):\n", 125 | " print(\"The String is accepted\")\n", 126 | "else:\n", 127 | " print(\"The String is rejected\")" 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": null, 133 | "metadata": {}, 134 | "outputs": [], 135 | "source": [] 136 | } 137 | ], 138 | "metadata": { 139 | "kernelspec": { 140 | "display_name": "Python 3", 141 | "language": "python", 142 | "name": "python3" 143 | }, 144 | "language_info": { 145 | "codemirror_mode": { 146 | "name": "ipython", 147 | "version": 3 148 | }, 149 | "file_extension": ".py", 150 | "mimetype": "text/x-python", 151 | "name": "python", 152 | "nbconvert_exporter": "python", 153 | "pygments_lexer": "ipython3", 154 | "version": "3.7.4" 155 | } 156 | }, 157 | "nbformat": 4, 158 | "nbformat_minor": 2 159 | } 160 | -------------------------------------------------------------------------------- /machine_learning/Basics of tensorflow/Basic of TensorFlow.ipynb: -------------------------------------------------------------------------------- 1 | {"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"Basic of TensorFlow.ipynb","version":"0.3.2","provenance":[],"collapsed_sections":[]},"kernelspec":{"name":"python3","display_name":"Python 3"}},"cells":[{"cell_type":"markdown","metadata":{"id":"PEIqWp_Ajco2","colab_type":"text"},"source":["#**TensorFlow**"]},{"cell_type":"code","metadata":{"id":"xiljd-w4r8D5","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":202},"outputId":"a29b5444-b2f4-4826-f6c0-9264da4f00c9","executionInfo":{"status":"ok","timestamp":1568463145418,"user_tz":-330,"elapsed":3395,"user":{"displayName":"HRITIK JAISWAL","photoUrl":"https://lh3.googleusercontent.com/a-/AAuE7mAIoT5asTvy-RaZPvDKRvz2bMxFBhU-1QvQZ2E4=s64","userId":"10596177819840519504"}}},"source":["!wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip\n"],"execution_count":27,"outputs":[{"output_type":"stream","text":["--2019-09-14 12:12:29-- https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip\n","Resolving bin.equinox.io (bin.equinox.io)... 52.204.136.9, 3.214.163.243, 52.54.237.49, ...\n","Connecting to bin.equinox.io (bin.equinox.io)|52.204.136.9|:443... connected.\n","HTTP request sent, awaiting response... 200 OK\n","Length: 13607069 (13M) [application/octet-stream]\n","Saving to: ‘ngrok-stable-linux-amd64.zip’\n","\n","\r ngrok-sta 0%[ ] 0 --.-KB/s \r ngrok-stab 64%[===========> ] 8.43M 41.9MB/s \rngrok-stable-linux- 100%[===================>] 12.98M 40.9MB/s in 0.3s \n","\n","2019-09-14 12:12:30 (40.9 MB/s) - ‘ngrok-stable-linux-amd64.zip’ saved [13607069/13607069]\n","\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"4mvo4BebsFsF","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":50},"outputId":"16dc0dd7-5b5e-498c-d899-a5fd29ade248","executionInfo":{"status":"ok","timestamp":1568463166696,"user_tz":-330,"elapsed":2990,"user":{"displayName":"HRITIK JAISWAL","photoUrl":"https://lh3.googleusercontent.com/a-/AAuE7mAIoT5asTvy-RaZPvDKRvz2bMxFBhU-1QvQZ2E4=s64","userId":"10596177819840519504"}}},"source":["!unzip ngrok-stable-linux-amd64.zip"],"execution_count":28,"outputs":[{"output_type":"stream","text":["Archive: ngrok-stable-linux-amd64.zip\n"," inflating: ngrok \n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"GX2b0F9csGx9","colab_type":"code","colab":{}},"source":["LOG_DIR = './log'\n","get_ipython().system_raw(\n","'tensorboard --logdir {} --host 0.0.0.0 --port 6006 &'\n",".format(LOG_DIR)\n",")"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"6qraPiqesGnF","colab_type":"code","colab":{}},"source":["get_ipython().system_raw('./ngrok http 6006 &')"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"08Nt7vuysGjV","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":34},"outputId":"c0b5b743-0b21-44b0-ffb4-e95420bc3367","executionInfo":{"status":"ok","timestamp":1568463203880,"user_tz":-330,"elapsed":2778,"user":{"displayName":"HRITIK JAISWAL","photoUrl":"https://lh3.googleusercontent.com/a-/AAuE7mAIoT5asTvy-RaZPvDKRvz2bMxFBhU-1QvQZ2E4=s64","userId":"10596177819840519504"}}},"source":["! curl -s http://localhost:4040/api/tunnels | python3 -c \\\n","\"import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'])\""],"execution_count":31,"outputs":[{"output_type":"stream","text":["https://f4e76656.ngrok.io\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"U1rIp6d6ipiM","colab_type":"code","colab":{}},"source":["import tensorflow as tf"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"xa3Rocvoivc0","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":67},"outputId":"84fbbbe8-0735-49ba-ef44-77eba67eae3b","executionInfo":{"status":"ok","timestamp":1568460837092,"user_tz":-330,"elapsed":909,"user":{"displayName":"HRITIK JAISWAL","photoUrl":"https://lh3.googleusercontent.com/a-/AAuE7mAIoT5asTvy-RaZPvDKRvz2bMxFBhU-1QvQZ2E4=s64","userId":"10596177819840519504"}}},"source":["a = tf.constant(10)\n","b = tf.constant(10)\n","c = tf.add(a,b)\n","print(a)\n","print(b)\n","with tf.Session() as sess:\n"," print(sess.run(c))"],"execution_count":6,"outputs":[{"output_type":"stream","text":["Tensor(\"Const_6:0\", shape=(), dtype=int32)\n","Tensor(\"Const_7:0\", shape=(), dtype=int32)\n","20\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"PEA9Vel3jG_G","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":67},"outputId":"0505cf2c-ea75-4b34-80fd-6c00a42123ac","executionInfo":{"status":"ok","timestamp":1568461402364,"user_tz":-330,"elapsed":895,"user":{"displayName":"HRITIK JAISWAL","photoUrl":"https://lh3.googleusercontent.com/a-/AAuE7mAIoT5asTvy-RaZPvDKRvz2bMxFBhU-1QvQZ2E4=s64","userId":"10596177819840519504"}}},"source":["\n","a=tf.constant([2,2],name='a')\n","b=tf.constant([[0,1],[2,3]],name='b')\n","x=tf.add(a,b,name='add')\n","y=tf.multiply(a,b,name='mul')\n","with tf.Session() as sess:\n"," x,y=sess.run([x,y])\n","print(x,y)"],"execution_count":7,"outputs":[{"output_type":"stream","text":["[[2 3]\n"," [4 5]] [[0 2]\n"," [4 6]]\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"hZtuBtLQla08","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":67},"outputId":"d239d562-6f88-4cd0-b44d-2b5facd723af","executionInfo":{"status":"ok","timestamp":1568461653049,"user_tz":-330,"elapsed":903,"user":{"displayName":"HRITIK JAISWAL","photoUrl":"https://lh3.googleusercontent.com/a-/AAuE7mAIoT5asTvy-RaZPvDKRvz2bMxFBhU-1QvQZ2E4=s64","userId":"10596177819840519504"}}},"source":["input_tensor = tf.constant([[0,1], [2,3] , [4,5]])\n","like_t = tf.ones_like(input_tensor)\n","my_mul = tf.multiply(like_t , input_tensor , name = \"mul\")\n","\n","with tf.Session() as sess:\n"," my_mul = sess.run(my_mul)\n"," \n","print(my_mul)"],"execution_count":10,"outputs":[{"output_type":"stream","text":["[[0 1]\n"," [2 3]\n"," [4 5]]\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"xKbFVIlzmPDu","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":168},"outputId":"b396f610-a01e-4cd0-fc4c-a784e680e7e1","executionInfo":{"status":"ok","timestamp":1568462757185,"user_tz":-330,"elapsed":877,"user":{"displayName":"HRITIK JAISWAL","photoUrl":"https://lh3.googleusercontent.com/a-/AAuE7mAIoT5asTvy-RaZPvDKRvz2bMxFBhU-1QvQZ2E4=s64","userId":"10596177819840519504"}}},"source":["import numpy as np\n","\n","m1 = [[1 ,2] ,[3 ,4]]\n","\n","m2 = np.array([[1 ,2],[3,4]] , dtype= np.float32)\n","\n","m3 = tf.constant([[1,2] , [3,4]])\n","\n","print(m1)\n","print(m2)\n","print(\"\\n m3 :\" ,m3)\n","\n","t1 = tf.convert_to_tensor(m1 , dtype = tf.float32)\n","t2 = tf.convert_to_tensor(m2 , dtype = tf.float32)\n","t3 = tf.convert_to_tensor(m3 , dtype = tf.int32)\n","\n","print(t1)\n","print(t2)\n","print(t3)\n"],"execution_count":26,"outputs":[{"output_type":"stream","text":["[[1, 2], [3, 4]]\n","[[1. 2.]\n"," [3. 4.]]\n","\n"," m3 :\n"," Tensor(\"Const_42:0\", shape=(2, 2), dtype=int32)\n","Tensor(\"Const_43:0\", shape=(2, 2), dtype=float32)\n","Tensor(\"Const_44:0\", shape=(2, 2), dtype=float32)\n","Tensor(\"Const_42:0\", shape=(2, 2), dtype=int32)\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"lX53MUN2owLu","colab_type":"code","colab":{}},"source":[""],"execution_count":0,"outputs":[]}]} -------------------------------------------------------------------------------- /neural_network/02-imdb-binary-classification.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stderr", 10 | "output_type": "stream", 11 | "text": [ 12 | "C:\\Users\\Hussnain\\Anaconda3\\envs\\tensorflow\\lib\\site-packages\\h5py\\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n", 13 | " from ._conv import register_converters as _register_converters\n", 14 | "Using TensorFlow backend.\n" 15 | ] 16 | } 17 | ], 18 | "source": [ 19 | "#Imports\n", 20 | "from keras.datasets import imdb\n", 21 | "\n", 22 | "from keras import models\n", 23 | "from keras import layers\n", 24 | "from keras import optimizers\n", 25 | "from keras import losses\n", 26 | "from keras import metrics,activations\n", 27 | "\n", 28 | "import matplotlib.pyplot as plt" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": null, 34 | "metadata": {}, 35 | "outputs": [ 36 | { 37 | "name": "stdout", 38 | "output_type": "stream", 39 | "text": [ 40 | "Downloading data from https://s3.amazonaws.com/text-datasets/imdb.npz\n", 41 | " 1048576/17464789 [>.............................] - ETA: 53:49" 42 | ] 43 | } 44 | ], 45 | "source": [ 46 | "#Downloading data from https://s3.amazonaws.com/text-datasets/imdb.npz\n", 47 | "\n", 48 | "(xtrain,ytrain), (xtest, ytest) = imdb.load_data(num_words=10000)" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": null, 54 | "metadata": {}, 55 | "outputs": [], 56 | "source": [ 57 | "#Exploring the dataset\n", 58 | "\n", 59 | "print('xtrain shape', xtrain.shape)\n", 60 | "print('ytrain shape', ytrain.shape)\n", 61 | "print()\n", 62 | "print('xtest shape', xtest.shape)\n", 63 | "print('ytest shape', ytest.shape)\n", 64 | "print()\n", 65 | "print('xtrain first review as dictionary index', xtrain[1])\n", 66 | "print()\n", 67 | "print()\n", 68 | "print('ytrain label', ytrain[0])" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": null, 74 | "metadata": {}, 75 | "outputs": [], 76 | "source": [ 77 | "#index to words mapping\n", 78 | "word_index = imdb.get_word_index()" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": null, 84 | "metadata": {}, 85 | "outputs": [], 86 | "source": [ 87 | "reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": null, 93 | "metadata": {}, 94 | "outputs": [], 95 | "source": [ 96 | "decode_review = ' '.join([reverse_word_index.get(i-3, reverse_word_index.get(i)) for i in xtrain[22]])\n", 97 | "decode_review" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": null, 103 | "metadata": {}, 104 | "outputs": [], 105 | "source": [ 106 | "import numpy as np\n", 107 | "\n", 108 | "def vectorize_sequences(sequences, dimension=10000):\n", 109 | " results = np.zeros((len(sequences), dimension))\n", 110 | " for i, sequence in enumerate(sequences):\n", 111 | " results[i, sequence] = 1. \n", 112 | " return results\n", 113 | "\n", 114 | "x_train = vectorize_sequences(xtrain)\n", 115 | "x_test = vectorize_sequences(xtest)" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": null, 121 | "metadata": {}, 122 | "outputs": [], 123 | "source": [ 124 | "ytrain = np.asarray(ytrain).astype('float32')\n", 125 | "ytest = np.asarray(ytest).astype('float32')" 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": null, 131 | "metadata": {}, 132 | "outputs": [], 133 | "source": [ 134 | "#model\n", 135 | "model = models.Sequential()\n", 136 | "model.add(layers.Dense(16, activation=activations.relu, input_shape=(10000,)))\n", 137 | "model.add(layers.Dense(16, activation=activations.relu))\n", 138 | "model.add(layers.Dense(1, activation=activations.sigmoid))" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": null, 144 | "metadata": {}, 145 | "outputs": [], 146 | "source": [ 147 | "model.compile(optimizer=optimizers.RMSprop(lr=0.0001), loss=losses.mse, metrics=['acc'])" 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": null, 153 | "metadata": {}, 154 | "outputs": [], 155 | "source": [ 156 | "x_val = x_train[:10000]\n", 157 | "y_val = ytrain[:10000]\n", 158 | "\n", 159 | "x_train_partial = x_train[10000:]\n", 160 | "y_train_partial = ytrain[10000:]" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": null, 166 | "metadata": {}, 167 | "outputs": [], 168 | "source": [ 169 | "history = model.fit(x_train_partial, y_train_partial, epochs=4, batch_size=512, validation_data=(x_val,y_val))\n", 170 | "history_dict = history.history\n", 171 | "history_dict.keys()\n", 172 | "print(history.history['acc'][-1])\n", 173 | "print(history.history['val_acc'][-1])" 174 | ] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "execution_count": null, 179 | "metadata": {}, 180 | "outputs": [], 181 | "source": [ 182 | "print(model.predict(x_train_partial[22:23]))" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": null, 188 | "metadata": {}, 189 | "outputs": [], 190 | "source": [ 191 | "loss = history_dict['loss']\n", 192 | "val_loss = history_dict['val_loss']\n", 193 | "epochs = range(0, len(loss)+1)\n", 194 | "epochs" 195 | ] 196 | }, 197 | { 198 | "cell_type": "code", 199 | "execution_count": null, 200 | "metadata": {}, 201 | "outputs": [], 202 | "source": [ 203 | "%matplotlib\n", 204 | "acc = history.history['acc']\n", 205 | "val_acc = history.history['val_acc']\n", 206 | "loss = history.history['loss']\n", 207 | "val_loss = history.history['val_loss']\n", 208 | "\n", 209 | "epochs = range(1, len(acc) + 1)\n", 210 | "\n", 211 | "# \"bo\" is for \"blue dot\"\n", 212 | "plt.plot(epochs, loss, 'ro', label='Training loss')\n", 213 | "# b is for \"solid blue line\"\n", 214 | "plt.plot(epochs, val_loss, 'b', label='Validation loss')\n", 215 | "plt.title('Training and validation loss')\n", 216 | "plt.xlabel('Epochs')\n", 217 | "plt.ylabel('Loss')\n", 218 | "plt.legend()\n", 219 | "\n", 220 | "plt.show()" 221 | ] 222 | }, 223 | { 224 | "cell_type": "code", 225 | "execution_count": null, 226 | "metadata": {}, 227 | "outputs": [], 228 | "source": [ 229 | "plt.clf() # clear figure# clear \n", 230 | "acc_values = history_dict['acc']\n", 231 | "val_acc_values = history_dict['val_acc']\n", 232 | "\n", 233 | "plt.plot(epochs, acc, 'bo', label='Training acc')\n", 234 | "plt.plot(epochs, val_acc, 'b', label='Validation acc')\n", 235 | "plt.title('Training and validation accuracy')\n", 236 | "plt.xlabel('Epochs')\n", 237 | "plt.ylabel('Loss')\n", 238 | "plt.legend()\n", 239 | "\n", 240 | "plt.show()" 241 | ] 242 | }, 243 | { 244 | "cell_type": "code", 245 | "execution_count": null, 246 | "metadata": {}, 247 | "outputs": [], 248 | "source": [] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "execution_count": null, 253 | "metadata": {}, 254 | "outputs": [], 255 | "source": [] 256 | } 257 | ], 258 | "metadata": { 259 | "kernelspec": { 260 | "display_name": "Python 3", 261 | "language": "python", 262 | "name": "python3" 263 | }, 264 | "language_info": { 265 | "codemirror_mode": { 266 | "name": "ipython", 267 | "version": 3 268 | }, 269 | "file_extension": ".py", 270 | "mimetype": "text/x-python", 271 | "name": "python", 272 | "nbconvert_exporter": "python", 273 | "pygments_lexer": "ipython3", 274 | "version": "3.6.5" 275 | } 276 | }, 277 | "nbformat": 4, 278 | "nbformat_minor": 2 279 | } 280 | -------------------------------------------------------------------------------- /machine_learning/Decision Tree regression with k-fold cross validation/2019.csv: -------------------------------------------------------------------------------- 1 | Overall rank,Country or region,Score,GDP per capita,Social support,Healthy life expectancy,Freedom to make life choices,Generosity,Perceptions of corruption 2 | 1,Finland,7.769,1.340,1.587,0.986,0.596,0.153,0.393 3 | 2,Denmark,7.600,1.383,1.573,0.996,0.592,0.252,0.410 4 | 3,Norway,7.554,1.488,1.582,1.028,0.603,0.271,0.341 5 | 4,Iceland,7.494,1.380,1.624,1.026,0.591,0.354,0.118 6 | 5,Netherlands,7.488,1.396,1.522,0.999,0.557,0.322,0.298 7 | 6,Switzerland,7.480,1.452,1.526,1.052,0.572,0.263,0.343 8 | 7,Sweden,7.343,1.387,1.487,1.009,0.574,0.267,0.373 9 | 8,New Zealand,7.307,1.303,1.557,1.026,0.585,0.330,0.380 10 | 9,Canada,7.278,1.365,1.505,1.039,0.584,0.285,0.308 11 | 10,Austria,7.246,1.376,1.475,1.016,0.532,0.244,0.226 12 | 11,Australia,7.228,1.372,1.548,1.036,0.557,0.332,0.290 13 | 12,Costa Rica,7.167,1.034,1.441,0.963,0.558,0.144,0.093 14 | 13,Israel,7.139,1.276,1.455,1.029,0.371,0.261,0.082 15 | 14,Luxembourg,7.090,1.609,1.479,1.012,0.526,0.194,0.316 16 | 15,United Kingdom,7.054,1.333,1.538,0.996,0.450,0.348,0.278 17 | 16,Ireland,7.021,1.499,1.553,0.999,0.516,0.298,0.310 18 | 17,Germany,6.985,1.373,1.454,0.987,0.495,0.261,0.265 19 | 18,Belgium,6.923,1.356,1.504,0.986,0.473,0.160,0.210 20 | 19,United States,6.892,1.433,1.457,0.874,0.454,0.280,0.128 21 | 20,Czech Republic,6.852,1.269,1.487,0.920,0.457,0.046,0.036 22 | 21,United Arab Emirates,6.825,1.503,1.310,0.825,0.598,0.262,0.182 23 | 22,Malta,6.726,1.300,1.520,0.999,0.564,0.375,0.151 24 | 23,Mexico,6.595,1.070,1.323,0.861,0.433,0.074,0.073 25 | 24,France,6.592,1.324,1.472,1.045,0.436,0.111,0.183 26 | 25,Taiwan,6.446,1.368,1.430,0.914,0.351,0.242,0.097 27 | 26,Chile,6.444,1.159,1.369,0.920,0.357,0.187,0.056 28 | 27,Guatemala,6.436,0.800,1.269,0.746,0.535,0.175,0.078 29 | 28,Saudi Arabia,6.375,1.403,1.357,0.795,0.439,0.080,0.132 30 | 29,Qatar,6.374,1.684,1.313,0.871,0.555,0.220,0.167 31 | 30,Spain,6.354,1.286,1.484,1.062,0.362,0.153,0.079 32 | 31,Panama,6.321,1.149,1.442,0.910,0.516,0.109,0.054 33 | 32,Brazil,6.300,1.004,1.439,0.802,0.390,0.099,0.086 34 | 33,Uruguay,6.293,1.124,1.465,0.891,0.523,0.127,0.150 35 | 34,Singapore,6.262,1.572,1.463,1.141,0.556,0.271,0.453 36 | 35,El Salvador,6.253,0.794,1.242,0.789,0.430,0.093,0.074 37 | 36,Italy,6.223,1.294,1.488,1.039,0.231,0.158,0.030 38 | 37,Bahrain,6.199,1.362,1.368,0.871,0.536,0.255,0.110 39 | 38,Slovakia,6.198,1.246,1.504,0.881,0.334,0.121,0.014 40 | 39,Trinidad & Tobago,6.192,1.231,1.477,0.713,0.489,0.185,0.016 41 | 40,Poland,6.182,1.206,1.438,0.884,0.483,0.117,0.050 42 | 41,Uzbekistan,6.174,0.745,1.529,0.756,0.631,0.322,0.240 43 | 42,Lithuania,6.149,1.238,1.515,0.818,0.291,0.043,0.042 44 | 43,Colombia,6.125,0.985,1.410,0.841,0.470,0.099,0.034 45 | 44,Slovenia,6.118,1.258,1.523,0.953,0.564,0.144,0.057 46 | 45,Nicaragua,6.105,0.694,1.325,0.835,0.435,0.200,0.127 47 | 46,Kosovo,6.100,0.882,1.232,0.758,0.489,0.262,0.006 48 | 47,Argentina,6.086,1.092,1.432,0.881,0.471,0.066,0.050 49 | 48,Romania,6.070,1.162,1.232,0.825,0.462,0.083,0.005 50 | 49,Cyprus,6.046,1.263,1.223,1.042,0.406,0.190,0.041 51 | 50,Ecuador,6.028,0.912,1.312,0.868,0.498,0.126,0.087 52 | 51,Kuwait,6.021,1.500,1.319,0.808,0.493,0.142,0.097 53 | 52,Thailand,6.008,1.050,1.409,0.828,0.557,0.359,0.028 54 | 53,Latvia,5.940,1.187,1.465,0.812,0.264,0.075,0.064 55 | 54,South Korea,5.895,1.301,1.219,1.036,0.159,0.175,0.056 56 | 55,Estonia,5.893,1.237,1.528,0.874,0.495,0.103,0.161 57 | 56,Jamaica,5.890,0.831,1.478,0.831,0.490,0.107,0.028 58 | 57,Mauritius,5.888,1.120,1.402,0.798,0.498,0.215,0.060 59 | 58,Japan,5.886,1.327,1.419,1.088,0.445,0.069,0.140 60 | 59,Honduras,5.860,0.642,1.236,0.828,0.507,0.246,0.078 61 | 60,Kazakhstan,5.809,1.173,1.508,0.729,0.410,0.146,0.096 62 | 61,Bolivia,5.779,0.776,1.209,0.706,0.511,0.137,0.064 63 | 62,Hungary,5.758,1.201,1.410,0.828,0.199,0.081,0.020 64 | 63,Paraguay,5.743,0.855,1.475,0.777,0.514,0.184,0.080 65 | 64,Northern Cyprus,5.718,1.263,1.252,1.042,0.417,0.191,0.162 66 | 65,Peru,5.697,0.960,1.274,0.854,0.455,0.083,0.027 67 | 66,Portugal,5.693,1.221,1.431,0.999,0.508,0.047,0.025 68 | 67,Pakistan,5.653,0.677,0.886,0.535,0.313,0.220,0.098 69 | 68,Russia,5.648,1.183,1.452,0.726,0.334,0.082,0.031 70 | 69,Philippines,5.631,0.807,1.293,0.657,0.558,0.117,0.107 71 | 70,Serbia,5.603,1.004,1.383,0.854,0.282,0.137,0.039 72 | 71,Moldova,5.529,0.685,1.328,0.739,0.245,0.181,0.000 73 | 72,Libya,5.525,1.044,1.303,0.673,0.416,0.133,0.152 74 | 73,Montenegro,5.523,1.051,1.361,0.871,0.197,0.142,0.080 75 | 74,Tajikistan,5.467,0.493,1.098,0.718,0.389,0.230,0.144 76 | 75,Croatia,5.432,1.155,1.266,0.914,0.296,0.119,0.022 77 | 76,Hong Kong,5.430,1.438,1.277,1.122,0.440,0.258,0.287 78 | 77,Dominican Republic,5.425,1.015,1.401,0.779,0.497,0.113,0.101 79 | 78,Bosnia and Herzegovina,5.386,0.945,1.212,0.845,0.212,0.263,0.006 80 | 79,Turkey,5.373,1.183,1.360,0.808,0.195,0.083,0.106 81 | 80,Malaysia,5.339,1.221,1.171,0.828,0.508,0.260,0.024 82 | 81,Belarus,5.323,1.067,1.465,0.789,0.235,0.094,0.142 83 | 82,Greece,5.287,1.181,1.156,0.999,0.067,0.000,0.034 84 | 83,Mongolia,5.285,0.948,1.531,0.667,0.317,0.235,0.038 85 | 84,North Macedonia,5.274,0.983,1.294,0.838,0.345,0.185,0.034 86 | 85,Nigeria,5.265,0.696,1.111,0.245,0.426,0.215,0.041 87 | 86,Kyrgyzstan,5.261,0.551,1.438,0.723,0.508,0.300,0.023 88 | 87,Turkmenistan,5.247,1.052,1.538,0.657,0.394,0.244,0.028 89 | 88,Algeria,5.211,1.002,1.160,0.785,0.086,0.073,0.114 90 | 89,Morocco,5.208,0.801,0.782,0.782,0.418,0.036,0.076 91 | 90,Azerbaijan,5.208,1.043,1.147,0.769,0.351,0.035,0.182 92 | 91,Lebanon,5.197,0.987,1.224,0.815,0.216,0.166,0.027 93 | 92,Indonesia,5.192,0.931,1.203,0.660,0.491,0.498,0.028 94 | 93,China,5.191,1.029,1.125,0.893,0.521,0.058,0.100 95 | 94,Vietnam,5.175,0.741,1.346,0.851,0.543,0.147,0.073 96 | 95,Bhutan,5.082,0.813,1.321,0.604,0.457,0.370,0.167 97 | 96,Cameroon,5.044,0.549,0.910,0.331,0.381,0.187,0.037 98 | 97,Bulgaria,5.011,1.092,1.513,0.815,0.311,0.081,0.004 99 | 98,Ghana,4.996,0.611,0.868,0.486,0.381,0.245,0.040 100 | 99,Ivory Coast,4.944,0.569,0.808,0.232,0.352,0.154,0.090 101 | 100,Nepal,4.913,0.446,1.226,0.677,0.439,0.285,0.089 102 | 101,Jordan,4.906,0.837,1.225,0.815,0.383,0.110,0.130 103 | 102,Benin,4.883,0.393,0.437,0.397,0.349,0.175,0.082 104 | 103,Congo (Brazzaville),4.812,0.673,0.799,0.508,0.372,0.105,0.093 105 | 104,Gabon,4.799,1.057,1.183,0.571,0.295,0.043,0.055 106 | 105,Laos,4.796,0.764,1.030,0.551,0.547,0.266,0.164 107 | 106,South Africa,4.722,0.960,1.351,0.469,0.389,0.130,0.055 108 | 107,Albania,4.719,0.947,0.848,0.874,0.383,0.178,0.027 109 | 108,Venezuela,4.707,0.960,1.427,0.805,0.154,0.064,0.047 110 | 109,Cambodia,4.700,0.574,1.122,0.637,0.609,0.232,0.062 111 | 110,Palestinian Territories,4.696,0.657,1.247,0.672,0.225,0.103,0.066 112 | 111,Senegal,4.681,0.450,1.134,0.571,0.292,0.153,0.072 113 | 112,Somalia,4.668,0.000,0.698,0.268,0.559,0.243,0.270 114 | 113,Namibia,4.639,0.879,1.313,0.477,0.401,0.070,0.056 115 | 114,Niger,4.628,0.138,0.774,0.366,0.318,0.188,0.102 116 | 115,Burkina Faso,4.587,0.331,1.056,0.380,0.255,0.177,0.113 117 | 116,Armenia,4.559,0.850,1.055,0.815,0.283,0.095,0.064 118 | 117,Iran,4.548,1.100,0.842,0.785,0.305,0.270,0.125 119 | 118,Guinea,4.534,0.380,0.829,0.375,0.332,0.207,0.086 120 | 119,Georgia,4.519,0.886,0.666,0.752,0.346,0.043,0.164 121 | 120,Gambia,4.516,0.308,0.939,0.428,0.382,0.269,0.167 122 | 121,Kenya,4.509,0.512,0.983,0.581,0.431,0.372,0.053 123 | 122,Mauritania,4.490,0.570,1.167,0.489,0.066,0.106,0.088 124 | 123,Mozambique,4.466,0.204,0.986,0.390,0.494,0.197,0.138 125 | 124,Tunisia,4.461,0.921,1.000,0.815,0.167,0.059,0.055 126 | 125,Bangladesh,4.456,0.562,0.928,0.723,0.527,0.166,0.143 127 | 126,Iraq,4.437,1.043,0.980,0.574,0.241,0.148,0.089 128 | 127,Congo (Kinshasa),4.418,0.094,1.125,0.357,0.269,0.212,0.053 129 | 128,Mali,4.390,0.385,1.105,0.308,0.327,0.153,0.052 130 | 129,Sierra Leone,4.374,0.268,0.841,0.242,0.309,0.252,0.045 131 | 130,Sri Lanka,4.366,0.949,1.265,0.831,0.470,0.244,0.047 132 | 131,Myanmar,4.360,0.710,1.181,0.555,0.525,0.566,0.172 133 | 132,Chad,4.350,0.350,0.766,0.192,0.174,0.198,0.078 134 | 133,Ukraine,4.332,0.820,1.390,0.739,0.178,0.187,0.010 135 | 134,Ethiopia,4.286,0.336,1.033,0.532,0.344,0.209,0.100 136 | 135,Swaziland,4.212,0.811,1.149,0.000,0.313,0.074,0.135 137 | 136,Uganda,4.189,0.332,1.069,0.443,0.356,0.252,0.060 138 | 137,Egypt,4.166,0.913,1.039,0.644,0.241,0.076,0.067 139 | 138,Zambia,4.107,0.578,1.058,0.426,0.431,0.247,0.087 140 | 139,Togo,4.085,0.275,0.572,0.410,0.293,0.177,0.085 141 | 140,India,4.015,0.755,0.765,0.588,0.498,0.200,0.085 142 | 141,Liberia,3.975,0.073,0.922,0.443,0.370,0.233,0.033 143 | 142,Comoros,3.973,0.274,0.757,0.505,0.142,0.275,0.078 144 | 143,Madagascar,3.933,0.274,0.916,0.555,0.148,0.169,0.041 145 | 144,Lesotho,3.802,0.489,1.169,0.168,0.359,0.107,0.093 146 | 145,Burundi,3.775,0.046,0.447,0.380,0.220,0.176,0.180 147 | 146,Zimbabwe,3.663,0.366,1.114,0.433,0.361,0.151,0.089 148 | 147,Haiti,3.597,0.323,0.688,0.449,0.026,0.419,0.110 149 | 148,Botswana,3.488,1.041,1.145,0.538,0.455,0.025,0.100 150 | 149,Syria,3.462,0.619,0.378,0.440,0.013,0.331,0.141 151 | 150,Malawi,3.410,0.191,0.560,0.495,0.443,0.218,0.089 152 | 151,Yemen,3.380,0.287,1.163,0.463,0.143,0.108,0.077 153 | 152,Rwanda,3.334,0.359,0.711,0.614,0.555,0.217,0.411 154 | 153,Tanzania,3.231,0.476,0.885,0.499,0.417,0.276,0.147 155 | 154,Afghanistan,3.203,0.350,0.517,0.361,0.000,0.158,0.025 156 | 155,Central African Republic,3.083,0.026,0.000,0.105,0.225,0.235,0.035 157 | 156,South Sudan,2.853,0.306,0.575,0.295,0.010,0.202,0.091 158 | -------------------------------------------------------------------------------- /machine_learning/dbscan/dbscan.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | from sklearn.datasets import make_moons 4 | import warnings 5 | 6 | 7 | def euclidean_distance(q, p): 8 | """ 9 | Calculates the Euclidean distance 10 | between points q and p 11 | 12 | Distance can only be calculated between numeric values 13 | >>> euclidean_distance([1,'a'],[1,2]) 14 | Traceback (most recent call last): 15 | ... 16 | ValueError: Non-numeric input detected 17 | 18 | The dimentions of both the points must be the same 19 | >>> euclidean_distance([1,1,1],[1,2]) 20 | Traceback (most recent call last): 21 | ... 22 | ValueError: expected dimensions to be 2-d, instead got p:3 and q:2 23 | 24 | Supports only two dimentional points 25 | >>> euclidean_distance([1,1,1],[1,2]) 26 | Traceback (most recent call last): 27 | ... 28 | ValueError: expected dimensions to be 2-d, instead got p:3 and q:2 29 | 30 | Input should be in the format [x,y] or (x,y) 31 | >>> euclidean_distance(1,2) 32 | Traceback (most recent call last): 33 | ... 34 | TypeError: inputs must be iterable, either list [x,y] or tuple (x,y) 35 | """ 36 | if not hasattr(q, "__iter__") or not hasattr(p, "__iter__"): 37 | raise TypeError("inputs must be iterable, either list [x,y] or tuple (x,y)") 38 | 39 | if isinstance(q, str) or isinstance(p, str): 40 | raise TypeError("inputs cannot be str") 41 | 42 | if len(q) != 2 or len(p) != 2: 43 | raise ValueError( 44 | "expected dimensions to be 2-d, instead got p:{} and q:{}".format( 45 | len(q), len(p) 46 | ) 47 | ) 48 | 49 | for num in q + p: 50 | try: 51 | num = int(num) 52 | except: 53 | raise ValueError("Non-numeric input detected") 54 | 55 | a = pow((q[0] - p[0]), 2) 56 | b = pow((q[1] - p[1]), 2) 57 | return pow((a + b), 0.5) 58 | 59 | 60 | def find_neighbors(db, q, eps): 61 | """ 62 | Finds all points in the db that 63 | are within a distance of eps from Q 64 | 65 | eps value should be a number 66 | >>> find_neighbors({ (1,2):{'label':'undefined'}, (2,3):{'label':'undefined'}}, (2,5),'a') 67 | Traceback (most recent call last): 68 | ... 69 | ValueError: eps should be either int or float 70 | 71 | Q must be a 2-d point as list or tuple 72 | >>> find_neighbors({ (1,2):{'label':'undefined'}, (2,3):{'label':'undefined'}}, 2, 0.5) 73 | Traceback (most recent call last): 74 | ... 75 | TypeError: Q must a 2-dimentional point in the format (x,y) or [x,y] 76 | 77 | Points must be in correct format 78 | >>> find_neighbors([], (2,2) ,0.4) 79 | Traceback (most recent call last): 80 | ... 81 | TypeError: db must be a dict of points in the format {(x,y):{'label':'boolean/undefined'}} 82 | """ 83 | 84 | if not isinstance(eps, (int, float)): 85 | raise ValueError("eps should be either int or float") 86 | 87 | if not hasattr(q, "__iter__"): 88 | raise TypeError("Q must a 2-dimentional point in the format (x,y) or [x,y]") 89 | 90 | if not isinstance(db, dict): 91 | raise TypeError( 92 | "db must be a dict of points in the format {(x,y):{'label':'boolean/undefined'}}" 93 | ) 94 | 95 | return [p for p in db if euclidean_distance(q, p) <= eps] 96 | 97 | 98 | def plot_cluster(db, clusters, ax): 99 | """ 100 | Extracts all the points in the db and puts them together 101 | as seperate clusters and finally plots them 102 | 103 | db cannot be empty 104 | >>> fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(7, 5)) 105 | >>> plot_cluster({},[1,2], axes[1] ) 106 | Traceback (most recent call last): 107 | ... 108 | Exception: db is empty. No points to cluster 109 | 110 | clusters cannot be empty 111 | >>> fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(7, 5)) 112 | >>> plot_cluster({ (1,2):{'label':'undefined'}, (2,3):{'label':'undefined'}},[],axes[1] ) 113 | Traceback (most recent call last): 114 | ... 115 | Exception: nothing to cluster. Empty clusters 116 | 117 | clusters cannot be empty 118 | >>> fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(7, 5)) 119 | >>> plot_cluster({ (1,2):{'label':'undefined'}, (2,3):{'label':'undefined'}},[],axes[1] ) 120 | Traceback (most recent call last): 121 | ... 122 | Exception: nothing to cluster. Empty clusters 123 | 124 | ax must be a plotable 125 | >>> plot_cluster({ (1,2):{'label':'1'}, (2,3):{'label':'2'}},[1,2], [] ) 126 | Traceback (most recent call last): 127 | ... 128 | TypeError: ax must be an slot in a matplotlib figure 129 | """ 130 | if len(db) == 0: 131 | raise Exception("db is empty. No points to cluster") 132 | 133 | if len(clusters) == 0: 134 | raise Exception("nothing to cluster. Empty clusters") 135 | 136 | if not hasattr(ax, "plot"): 137 | raise TypeError("ax must be an slot in a matplotlib figure") 138 | 139 | temp = [] 140 | noise = [] 141 | for i in clusters: 142 | stack = [] 143 | for k, v in db.items(): 144 | if v["label"] == i: 145 | stack.append(k) 146 | elif v["label"] == "noise": 147 | noise.append(k) 148 | temp.append(stack) 149 | 150 | color = iter(plt.cm.rainbow(np.linspace(0, 1, len(clusters)))) 151 | for i in range(0, len(temp)): 152 | c = next(color) 153 | x = [l[0] for l in temp[i]] 154 | y = [l[1] for l in temp[i]] 155 | ax.plot(x, y, "ro", c=c) 156 | 157 | x = [l[0] for l in noise] 158 | y = [l[1] for l in noise] 159 | ax.plot(x, y, "ro", c="0") 160 | 161 | 162 | def dbscan(db, eps, min_pts): 163 | """ 164 | Implementation of the DBSCAN algorithm 165 | 166 | Points must be in correct format 167 | >>> dbscan([], (2,2) ,0.4) 168 | Traceback (most recent call last): 169 | ... 170 | TypeError: db must be a dict of points in the format {(x,y):{'label':'boolean/undefined'}} 171 | 172 | eps value should be a number 173 | >>> dbscan({ (1,2):{'label':'undefined'}, (2,3):{'label':'undefined'}},'a',20 ) 174 | Traceback (most recent call last): 175 | ... 176 | ValueError: eps should be either int or float 177 | 178 | min_pts value should be an integer 179 | >>> dbscan({ (1,2):{'label':'undefined'}, (2,3):{'label':'undefined'}},0.4,20.0 ) 180 | Traceback (most recent call last): 181 | ... 182 | ValueError: min_pts should be int 183 | 184 | db cannot be empty 185 | >>> dbscan({},0.4,20.0 ) 186 | Traceback (most recent call last): 187 | ... 188 | Exception: db is empty, nothing to cluster 189 | 190 | min_pts cannot be negative 191 | >>> dbscan({ (1,2):{'label':'undefined'}, (2,3):{'label':'undefined'}}, 0.4, -20) 192 | Traceback (most recent call last): 193 | ... 194 | ValueError: min_pts or eps cannot be negative 195 | 196 | eps cannot be negative 197 | >>> dbscan({ (1,2):{'label':'undefined'}, (2,3):{'label':'undefined'}},-0.4, 20) 198 | Traceback (most recent call last): 199 | ... 200 | ValueError: min_pts or eps cannot be negative 201 | 202 | """ 203 | if not isinstance(db, dict): 204 | raise TypeError( 205 | "db must be a dict of points in the format {(x,y):{'label':'boolean/undefined'}}" 206 | ) 207 | 208 | if len(db) == 0: 209 | raise Exception("db is empty, nothing to cluster") 210 | 211 | if not isinstance(eps, (int, float)): 212 | raise ValueError("eps should be either int or float") 213 | 214 | if not isinstance(min_pts, int): 215 | raise ValueError("min_pts should be int") 216 | 217 | if min_pts < 0 or eps < 0: 218 | raise ValueError("min_pts or eps cannot be negative") 219 | 220 | if min_pts == 0: 221 | warnings.warn("min_pts is 0. Are you sure you want this ?") 222 | 223 | if eps == 0: 224 | warnings.warn("eps is 0. Are you sure you want this ?") 225 | 226 | clusters = [] 227 | c = 0 228 | for p in db: 229 | if db[p]["label"] != "undefined": 230 | continue 231 | neighbors = find_neighbors(db, p, eps) 232 | if len(neighbors) < min_pts: 233 | db[p]["label"] = "noise" 234 | continue 235 | c += 1 236 | clusters.append(c) 237 | db[p]["label"] = c 238 | neighbors.remove(p) 239 | seed_set = neighbors.copy() 240 | while seed_set != []: 241 | q = seed_set.pop(0) 242 | if db[q]["label"] == "noise": 243 | db[q]["label"] = c 244 | if db[q]["label"] != "undefined": 245 | continue 246 | db[q]["label"] = c 247 | neighbors_n = find_neighbors(db, q, eps) 248 | if len(neighbors_n) >= min_pts: 249 | seed_set = seed_set + neighbors_n 250 | return db, clusters 251 | 252 | 253 | if __name__ == "__main__": 254 | 255 | fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(7, 5)) 256 | 257 | x, label = make_moons(n_samples=200, noise=0.1, random_state=19) 258 | 259 | axes[0].plot(x[:, 0], x[:, 1], "ro") 260 | 261 | points = {(point[0], point[1]): {"label": "undefined"} for point in x} 262 | 263 | eps = 0.25 264 | 265 | min_pts = 12 266 | 267 | db, clusters = dbscan(points, eps, min_pts) 268 | 269 | plot_cluster(db, clusters, axes[1]) 270 | 271 | plt.show() 272 | -------------------------------------------------------------------------------- /DIRECTORY.md: -------------------------------------------------------------------------------- 1 | # List of all files 2 | 3 | ## Automaton 4 | * [Netflix Scrapper](https://github.com/TheAlgorithms/Jupyter/blob/master/Automaton/Netflix_Scrapper.ipynb) 5 | * [Pushdown Automata Implementation](https://github.com/TheAlgorithms/Jupyter/blob/master/Automaton/Pushdown_Automata_Implementation.ipynb) 6 | * [Scrapnewsfromindiatoday](https://github.com/TheAlgorithms/Jupyter/blob/master/Automaton/ScrapNewsfromIndiaToday.ipynb) 7 | 8 | ## [Gradient Descent](https://github.com/TheAlgorithms/Jupyter/blob/master//gradient_descent.ipynb) 9 | 10 | ## [Gradient Descent](https://github.com/TheAlgorithms/Jupyter/blob/master//gradient_descent.py) 11 | 12 | ## Machine Learning 13 | * Arima 14 | * [Arima With Pyramid](https://github.com/TheAlgorithms/Jupyter/blob/master/machine_learning/ARIMA/ARIMA%20with%20pyramid.ipynb) 15 | * Associative Mining 16 | * [Association](https://github.com/TheAlgorithms/Jupyter/blob/master/machine_learning/Associative%20Mining/Association.ipynb) 17 | * Basics Of Tensorflow 18 | * [Basic Of Tensorflow](https://github.com/TheAlgorithms/Jupyter/blob/master/machine_learning/Basics%20of%20tensorflow/Basic%20of%20TensorFlow.ipynb) 19 | * [Bayesian Belief Networks](https://github.com/TheAlgorithms/Jupyter/blob/master/machine_learning/bayesian_belief_networks.ipynb) 20 | * Cosine-Similarity 21 | * [Similarity](https://github.com/TheAlgorithms/Jupyter/blob/master/machine_learning/Cosine-Similarity/Similarity.ipynb) 22 | * Dbscan 23 | * [Dbscan](https://github.com/TheAlgorithms/Jupyter/blob/master/machine_learning/dbscan/dbscan.ipynb) 24 | * [Dbscan](https://github.com/TheAlgorithms/Jupyter/blob/master/machine_learning/dbscan/dbscan.py) 25 | * Decision Tree Regression With K-Fold Cross Validation 26 | * [K-Fold Cross-Validation Of Decision Tree Regression](https://github.com/TheAlgorithms/Jupyter/blob/master/machine_learning/Decision%20Tree%20regression%20with%20k-fold%20cross%20validation/k-fold%20cross-validation%20of%20decision%20tree%20regression.ipynb) 27 | * Decision Tree 28 | * [Decision Tree](https://github.com/TheAlgorithms/Jupyter/blob/master/machine_learning/Decision%20tree/Decision_Tree.ipynb) 29 | * Fundamentals Of Python 30 | * [Python Basics](https://github.com/TheAlgorithms/Jupyter/blob/master/machine_learning/Fundamentals%20of%20python/Python%20basics.ipynb) 31 | * [House Price Prediction](https://github.com/TheAlgorithms/Jupyter/blob/master/machine_learning/House_Price_Prediction.ipynb) 32 | * Linear Regression 33 | * [Linear Regression Using Pandas](https://github.com/TheAlgorithms/Jupyter/blob/master/machine_learning/Linear_Regression/linear_regression_using_pandas.ipynb) 34 | * [Linearregression](https://github.com/TheAlgorithms/Jupyter/blob/master/machine_learning/Linear_Regression/LinearRegression.ipynb) 35 | * Matplotlib 36 | * [Matplotlib](https://github.com/TheAlgorithms/Jupyter/blob/master/machine_learning/Matplotlib/Matplotlib.ipynb) 37 | * Movie Recommendation System 38 | * [Movie Recommendation System](https://github.com/TheAlgorithms/Jupyter/blob/master/machine_learning/Movie_recommendation_system/Movie_Recommendation_System.ipynb) 39 | * Naive Bayes Classification 40 | * [Naive Bayes Classification](https://github.com/TheAlgorithms/Jupyter/blob/master/machine_learning/Naive%20Bayes%20Classification/Naive_Bayes_Classification.ipynb) 41 | * Naive Bayes 42 | * [Naive Bayes](https://github.com/TheAlgorithms/Jupyter/blob/master/machine_learning/Naive_Bayes/naive_bayes.ipynb) 43 | * Natural Language Processing 44 | * [Movie Recommendation Sentance Embedding](https://github.com/TheAlgorithms/Jupyter/blob/master/machine_learning/Natural%20language%20processing/Movie_recommendation_Sentance_Embedding.ipynb) 45 | * [Named Entity Recognition With Conditional Random Fields](https://github.com/TheAlgorithms/Jupyter/blob/master/machine_learning/Natural%20language%20processing/Named%20Entity%20Recognition%20with%20Conditional%20Random%20Fields.ipynb) 46 | * Numpy 47 | * [Face Recognition Using Eigenfaces](https://github.com/TheAlgorithms/Jupyter/blob/master/machine_learning/Numpy/face_recognition_using_eigenFaces.ipynb) 48 | * [Fundamentals Of Numpy](https://github.com/TheAlgorithms/Jupyter/blob/master/machine_learning/Numpy/Fundamentals%20of%20Numpy.ipynb) 49 | * [Image Denoising Using Numpy](https://github.com/TheAlgorithms/Jupyter/blob/master/machine_learning/Numpy/Image_Denoising_Using_Numpy.ipynb) 50 | * Pandas 51 | * [Pandas](https://github.com/TheAlgorithms/Jupyter/blob/master/machine_learning/Pandas/Pandas.ipynb) 52 | * Piecewise 53 | * [Piecewise Linear Transform](https://github.com/TheAlgorithms/Jupyter/blob/master/machine_learning/Piecewise/piecewise_linear_transform.ipynb) 54 | * [Price Prediction Model](https://github.com/TheAlgorithms/Jupyter/blob/master/machine_learning/price_prediction_model.ipynb) 55 | * Prophet 56 | * [Prophet](https://github.com/TheAlgorithms/Jupyter/blob/master/machine_learning/Prophet/Prophet.ipynb) 57 | * Random Forest Classification 58 | * [Random Forest Classification](https://github.com/TheAlgorithms/Jupyter/blob/master/machine_learning/random_forest_classification/random_forest_classification.py) 59 | * [Random Forest Classifier](https://github.com/TheAlgorithms/Jupyter/blob/master/machine_learning/random_forest_classification/random_forest_classifier.ipynb) 60 | * Random Forest Regression 61 | * [Random Forest Regression](https://github.com/TheAlgorithms/Jupyter/blob/master/machine_learning/random_forest_regression/random_forest_regression.ipynb) 62 | * [Random Forest Regression](https://github.com/TheAlgorithms/Jupyter/blob/master/machine_learning/random_forest_regression/random_forest_regression.py) 63 | * Reuters One Vs Rest Classifier 64 | * [Reuters One Vs Rest Classifier](https://github.com/TheAlgorithms/Jupyter/blob/master/machine_learning/Reuters_one_vs_rest_classifier/reuters_one_vs_rest_classifier.ipynb) 65 | * Scikit-Learn 66 | * [Scikit-Learn](https://github.com/TheAlgorithms/Jupyter/blob/master/machine_learning/Scikit-learn/Scikit-learn.ipynb) 67 | * Support Vector Machine 68 | * [Support Vector Machine](https://github.com/TheAlgorithms/Jupyter/blob/master/machine_learning/Support_Vector_Machine/Support%20Vector%20Machine.ipynb) 69 | 70 | ## Neural Network 71 | * [02-Imdb-Binary-Classification](https://github.com/TheAlgorithms/Jupyter/blob/master/neural_network/02-imdb-binary-classification.ipynb) 72 | * [A-Simple-Gan](https://github.com/TheAlgorithms/Jupyter/blob/master/neural_network/A-simple-GAN.ipynb) 73 | * [Autoencoder](https://github.com/TheAlgorithms/Jupyter/blob/master/neural_network/autoencoder.ipynb) 74 | * [Cnn-Using Keras](https://github.com/TheAlgorithms/Jupyter/blob/master/neural_network/CNN-using%20keras.ipynb) 75 | * [Cnn Pytorch](https://github.com/TheAlgorithms/Jupyter/blob/master/neural_network/CNN_Pytorch.ipynb) 76 | * [Fully Connected Neural Network](https://github.com/TheAlgorithms/Jupyter/blob/master/neural_network/fully_connected_neural_network.ipynb) 77 | * Gans-Pytorch-Vanilla-Ls-Dc 78 | * [Gans Pytorch](https://github.com/TheAlgorithms/Jupyter/blob/master/neural_network/GANs-PyTorch-Vanilla-LS-DC/GANs_PyTorch.ipynb) 79 | * [Image Enhancement Using Gfp Gan](https://github.com/TheAlgorithms/Jupyter/blob/master/neural_network/GANs-PyTorch-Vanilla-LS-DC/Image_Enhancement_Using_GFP_GAN.ipynb) 80 | * [Logistic Regression](https://github.com/TheAlgorithms/Jupyter/blob/master/neural_network/Logistic_Regression.ipynb) 81 | * [Neural Network Mnist Dataset](https://github.com/TheAlgorithms/Jupyter/blob/master/neural_network/Neural_network_Mnist_Dataset.ipynb) 82 | * [Qlearning](https://github.com/TheAlgorithms/Jupyter/blob/master/neural_network/qlearning.ipynb) 83 | * Rnn 84 | * [Rnn](https://github.com/TheAlgorithms/Jupyter/blob/master/neural_network/RNN/rnn.ipynb) 85 | * [Sequence Labelling With A Bilstm In Pytorch](https://github.com/TheAlgorithms/Jupyter/blob/master/neural_network/Sequence%20Labelling%20with%20a%20BiLSTM%20in%20PyTorch.ipynb) 86 | * [Style Transfer Pytorch](https://github.com/TheAlgorithms/Jupyter/blob/master/neural_network/style_transfer_pytorch.ipynb) 87 | * Text Classification 88 | * [Text Classification Using Bert](https://github.com/TheAlgorithms/Jupyter/blob/master/neural_network/Text%20Classification/text_classification_using_BERT.ipynb) 89 | * [Variational Autoencoder](https://github.com/TheAlgorithms/Jupyter/blob/master/neural_network/variational_autoencoder.ipynb) 90 | 91 | ## Numerical Methods 92 | * [Euler Method For The Cauchy Problem](https://github.com/TheAlgorithms/Jupyter/blob/master/numerical_methods/euler_method_for_the_Cauchy_problem.ipynb) 93 | * [Newton Forward Divided Difference Formula](https://github.com/TheAlgorithms/Jupyter/blob/master/numerical_methods/newton_forward_divided_difference_formula.ipynb) 94 | * [Runge Kutta Methods](https://github.com/TheAlgorithms/Jupyter/blob/master/numerical_methods/Runge_Kutta_Methods.ipynb) 95 | * [The Rectangular Method](https://github.com/TheAlgorithms/Jupyter/blob/master/numerical_methods/the_rectangular_method.ipynb) 96 | * [The Simpson’S Method](https://github.com/TheAlgorithms/Jupyter/blob/master/numerical_methods/the_Simpson’s_method.ipynb) 97 | * [The Trapezium Method](https://github.com/TheAlgorithms/Jupyter/blob/master/numerical_methods/the_trapezium_method.ipynb) 98 | 99 | ## Other 100 | * [Clothing Detection](https://github.com/TheAlgorithms/Jupyter/blob/master/other/clothing_detection.ipynb) 101 | * [Food Wastage Analysis From 1961-2013 Fao](https://github.com/TheAlgorithms/Jupyter/blob/master/other/food_wastage_analysis_from_1961-2013_fao.ipynb) 102 | * [Lucas Kanade](https://github.com/TheAlgorithms/Jupyter/blob/master/other/lucas_kanade.ipynb) 103 | * [Simplex Standard](https://github.com/TheAlgorithms/Jupyter/blob/master/other/Simplex_standard.ipynb) 104 | * [Video Games Analysis](https://github.com/TheAlgorithms/Jupyter/blob/master/other/Video%20games%20analysis.ipynb) 105 | -------------------------------------------------------------------------------- /machine_learning/Linear_Regression/linear_regression_using_pandas.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# `RELIANCE - NSE Stock Data`\n", 8 | "\n", 9 | "The file contains RELIANCE - NSE Stock Data from 1-Jan-16 to 6-May-21\n", 10 | "\n", 11 | "The data can be used to forecast the stock prices of the future\n", 12 | "\n", 13 | "Its a timeseries data from the national stock exchange of India\n", 14 | "\n", 15 | "|| `Variable` | `Significance` |\n", 16 | "| ------------- |:-------------:|:-------------:|\n", 17 | "|1.|Symbol|Symbol of the listed stock on NSE|\n", 18 | "|2.|Series|To which series does the stock belong (Equity, Options Future)|\n", 19 | "|3.|Date|Date of the trade|\n", 20 | "|4.|Prev Close|Previous day closing value of the stock|\n", 21 | "|5.|Open Price|Current Day opening price of the stock|\n", 22 | "|6.|High Price|Highest price touched by the stock in current day `(Target Variable)`|\n", 23 | "|7.|Low Price|lowest price touched by the stock in current day|\n", 24 | "|8.|Last Price|The price at which last trade occured in current day|\n", 25 | "|9.|Close Price|Current day closing price of the stock|\n", 26 | "|10.|Average Price|Average price of the day|\n", 27 | "|11.|Total Traded Quantity|Total number of stocks traded in current day|\n", 28 | "|12.|Turnover||\n", 29 | "|13.|No. of Trades|Current day's total number of trades|\n", 30 | "|14.|Deliverabel Quantity|Current day deliveable quantity to the traders|\n", 31 | "|15.|% Dly Qt to Traded Qty|`(Deliverable Quantity/Total Traded Quantity)*100`|" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": null, 37 | "metadata": {}, 38 | "outputs": [], 39 | "source": [ 40 | "import pandas as pd\n", 41 | "\n", 42 | "data_path=\"./data/RILO - Copy.csv\"\n", 43 | "data=pd.read_csv(data_path)\n", 44 | "data" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": null, 50 | "metadata": {}, 51 | "outputs": [], 52 | "source": [ 53 | "# Renaming the columns to have snake_case naming style. (Just as a convention and for convenience)\n", 54 | "data.columns=[\"_\".join(column.lower().split()) for column in data.columns]\n", 55 | "data.columns" 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "execution_count": null, 61 | "metadata": {}, 62 | "outputs": [], 63 | "source": [ 64 | "# Using `.describe()` on an entire DataFrame we can get a summary of the distribution of continuous variables:\n", 65 | "data.describe()" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": null, 71 | "metadata": {}, 72 | "outputs": [], 73 | "source": [ 74 | "# Checking for null values\n", 75 | "data.isnull().sum()" 76 | ] 77 | }, 78 | { 79 | "cell_type": "markdown", 80 | "metadata": {}, 81 | "source": [ 82 | "### As shown above, we do not have any null values in our dataset. Now we can focus on feature selection and model building." 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": null, 88 | "metadata": {}, 89 | "outputs": [], 90 | "source": [ 91 | "# By using the correlation method `.corr()` we can get the relationship between each continuous variable:\n", 92 | "correlation=data.corr()\n", 93 | "correlation" 94 | ] 95 | }, 96 | { 97 | "cell_type": "markdown", 98 | "metadata": {}, 99 | "source": [ 100 | "### `Matplotlib`\n", 101 | "\n", 102 | "Matplotlib is a visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack.\n", 103 | "\n", 104 | "One of the greatest benefits of visualization is that it allows us visual access to huge amounts of data in easily digestible visuals. Matplotlib consists of several plots like line, bar, scatter, histogram etc.\n", 105 | "\n", 106 | "Matplotlib comes with a wide variety of plots. Plots helps to understand trends, patterns, and to make correlations. They’re typically instruments for reasoning about quantitative information\n", 107 | "\n", 108 | "### `Seaborn`\n", 109 | "\n", 110 | "Seaborn is a Python data visualization library based on matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics." 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": null, 116 | "metadata": {}, 117 | "outputs": [], 118 | "source": [ 119 | "# Using seaborn and matplotlib to have a better visualization of correlation\n", 120 | "import seaborn as sn\n", 121 | "import matplotlib.pyplot as plt\n", 122 | "\n", 123 | "plt.figure(figsize=(10,8))\n", 124 | "sn.heatmap(correlation,annot=True,linewidth=1,cmap='PuOr')\n", 125 | "plt.show()" 126 | ] 127 | }, 128 | { 129 | "cell_type": "markdown", 130 | "metadata": {}, 131 | "source": [ 132 | "From the above correlation matrix, we get a general idea of which variables can be treated as features to build our model. Lets list them out\n", 133 | "Considering all the variables having `|corr|>=0.5`\n", 134 | "\n", 135 | "- prev_close\n", 136 | "- no._of_trades\n", 137 | "- open_price\n", 138 | "- low_price\n", 139 | "- last_price\n", 140 | "- turnover\n", 141 | "- close_price\n", 142 | "- %_dly_qt_to_traded_qty\n", 143 | "- average_price\n", 144 | "\n", 145 | "\n", 146 | "\n", 147 | "\n", 148 | "\n", 149 | "Now that we have a rough idea about our features, lets confirm their behaviour aginst target variable using scatter plots." 150 | ] 151 | }, 152 | { 153 | "cell_type": "code", 154 | "execution_count": null, 155 | "metadata": {}, 156 | "outputs": [], 157 | "source": [ 158 | "plt.figure(figsize=(18,18))\n", 159 | "\n", 160 | "plt.subplot(3,3,1)\n", 161 | "plt.scatter(data.prev_close,data.high_price)\n", 162 | "plt.title('Relation with Previous Closing Price')\n", 163 | "\n", 164 | "plt.subplot(3,3,2)\n", 165 | "plt.scatter(data['no._of_trades'],data.high_price)\n", 166 | "plt.title('Relation with No. of trades')\n", 167 | "\n", 168 | "plt.subplot(3,3,3)\n", 169 | "plt.scatter(data.open_price,data.high_price)\n", 170 | "plt.title('Relation with Opening Price')\n", 171 | "\n", 172 | "plt.subplot(3,3,4)\n", 173 | "plt.scatter(data.low_price,data.high_price)\n", 174 | "plt.title('Relation with Low Price')\n", 175 | "\n", 176 | "plt.subplot(3,3,5)\n", 177 | "plt.scatter(data.last_price,data.high_price)\n", 178 | "plt.title('Relation with Last Price')\n", 179 | "\n", 180 | "plt.subplot(3,3,6)\n", 181 | "plt.scatter(data.turnover,data.high_price)\n", 182 | "plt.title('Relation with Turnover')\n", 183 | "\n", 184 | "plt.subplot(3,3,7)\n", 185 | "plt.scatter(data.close_price,data.high_price)\n", 186 | "plt.title('Relation with Closing Price')\n", 187 | "\n", 188 | "plt.subplot(3,3,8)\n", 189 | "plt.scatter(data['%_dly_qt_to_traded_qty'],data.high_price)\n", 190 | "plt.title('Relation with Deliverable quantity')\n", 191 | "\n", 192 | "plt.subplot(3,3,9)\n", 193 | "plt.scatter(data.average_price,data.high_price)\n", 194 | "plt.title('Relation with Average Price')\n", 195 | "\n", 196 | "plt.show" 197 | ] 198 | }, 199 | { 200 | "cell_type": "markdown", 201 | "metadata": {}, 202 | "source": [ 203 | "From above visualization, we are clear to choose features for the linear-regression model. Those are:\n", 204 | "\n", 205 | "- prev_close\n", 206 | "- ~~no._of_trades~~\n", 207 | "- open_price\n", 208 | "- low_price\n", 209 | "- last_price\n", 210 | "- ~~turnover~~\n", 211 | "- close_price\n", 212 | "- ~~%_dly_qt_to_traded_qty~~\n", 213 | "- average_price\n" 214 | ] 215 | }, 216 | { 217 | "cell_type": "code", 218 | "execution_count": null, 219 | "metadata": {}, 220 | "outputs": [], 221 | "source": [ 222 | "features=['prev_close','open_price','low_price','last_price','close_price','average_price']\n", 223 | "X=data[features]\n", 224 | "X" 225 | ] 226 | }, 227 | { 228 | "cell_type": "code", 229 | "execution_count": null, 230 | "metadata": {}, 231 | "outputs": [], 232 | "source": [ 233 | "# Target variable\n", 234 | "y=data.high_price\n", 235 | "y" 236 | ] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "execution_count": null, 241 | "metadata": {}, 242 | "outputs": [], 243 | "source": [ 244 | "# split data into training and validation data, for both features and target\n", 245 | "# The split is based on a random number generator. Supplying a numeric value to\n", 246 | "# the random_state argument guarantees we get the same split every time we\n", 247 | "# run this script.\n", 248 | "\n", 249 | "from sklearn.model_selection import train_test_split\n", 250 | "train_X,val_X,train_y,val_y=train_test_split(X,y,test_size=0.2,random_state=0)" 251 | ] 252 | }, 253 | { 254 | "cell_type": "code", 255 | "execution_count": null, 256 | "metadata": {}, 257 | "outputs": [], 258 | "source": [ 259 | "from sklearn.linear_model import LinearRegression\n", 260 | "# Define model\n", 261 | "model=LinearRegression()\n", 262 | "# Fit model\n", 263 | "model.fit(train_X,train_y)" 264 | ] 265 | }, 266 | { 267 | "cell_type": "code", 268 | "execution_count": null, 269 | "metadata": {}, 270 | "outputs": [], 271 | "source": [ 272 | "# We use .score method to get an idea of quality of our model\n", 273 | "model.score(val_X,val_y)" 274 | ] 275 | }, 276 | { 277 | "cell_type": "markdown", 278 | "metadata": {}, 279 | "source": [ 280 | "### Model Validation\n", 281 | "There are many metrics for summarizing model quality, but we'll start with one called `Mean Absolute Error (also called MAE)`. Let's break down this metric starting with the last word, error.\n", 282 | "\n", 283 | "\n", 284 | "`error=actual-predicted`\n", 285 | "\n", 286 | "So, if a stock cost Rs.4000 at a timeframe, and we predicted it would cost Rs.3980 the error is Rs.20.\n", 287 | "\n", 288 | "With the MAE metric, we take the absolute value of each error. This converts each error to a positive number. We then take the average of those absolute errors. This is our measure of model quality. In plain English, it can be said as\n", 289 | "\n", 290 | "> On average, our predictions are off by about X.\n" 291 | ] 292 | }, 293 | { 294 | "cell_type": "code", 295 | "execution_count": null, 296 | "metadata": {}, 297 | "outputs": [], 298 | "source": [ 299 | "from sklearn.metrics import mean_absolute_error\n", 300 | "# Get predicted prices of stock on validation data\n", 301 | "pred_y=model.predict(val_X)\n", 302 | "mean_absolute_error(val_y,pred_y)" 303 | ] 304 | } 305 | ], 306 | "metadata": { 307 | "interpreter": { 308 | "hash": "e7370f93d1d0cde622a1f8e1c04877d8463912d04d973331ad4851f04de6915a" 309 | }, 310 | "kernelspec": { 311 | "display_name": "Python 3.9.7 64-bit", 312 | "name": "python3" 313 | }, 314 | "language_info": { 315 | "codemirror_mode": { 316 | "name": "ipython", 317 | "version": 3 318 | }, 319 | "file_extension": ".py", 320 | "mimetype": "text/x-python", 321 | "name": "python", 322 | "nbconvert_exporter": "python", 323 | "pygments_lexer": "ipython3", 324 | "version": "3.9.7" 325 | }, 326 | "orig_nbformat": 4 327 | }, 328 | "nbformat": 4, 329 | "nbformat_minor": 2 330 | } 331 | -------------------------------------------------------------------------------- /machine_learning/random_forest_classification/Social_Network_Ads.csv: -------------------------------------------------------------------------------- 1 | User ID,Gender,Age,EstimatedSalary,Purchased 2 | 15624510,Male,19,19000,0 3 | 15810944,Male,35,20000,0 4 | 15668575,Female,26,43000,0 5 | 15603246,Female,27,57000,0 6 | 15804002,Male,19,76000,0 7 | 15728773,Male,27,58000,0 8 | 15598044,Female,27,84000,0 9 | 15694829,Female,32,150000,1 10 | 15600575,Male,25,33000,0 11 | 15727311,Female,35,65000,0 12 | 15570769,Female,26,80000,0 13 | 15606274,Female,26,52000,0 14 | 15746139,Male,20,86000,0 15 | 15704987,Male,32,18000,0 16 | 15628972,Male,18,82000,0 17 | 15697686,Male,29,80000,0 18 | 15733883,Male,47,25000,1 19 | 15617482,Male,45,26000,1 20 | 15704583,Male,46,28000,1 21 | 15621083,Female,48,29000,1 22 | 15649487,Male,45,22000,1 23 | 15736760,Female,47,49000,1 24 | 15714658,Male,48,41000,1 25 | 15599081,Female,45,22000,1 26 | 15705113,Male,46,23000,1 27 | 15631159,Male,47,20000,1 28 | 15792818,Male,49,28000,1 29 | 15633531,Female,47,30000,1 30 | 15744529,Male,29,43000,0 31 | 15669656,Male,31,18000,0 32 | 15581198,Male,31,74000,0 33 | 15729054,Female,27,137000,1 34 | 15573452,Female,21,16000,0 35 | 15776733,Female,28,44000,0 36 | 15724858,Male,27,90000,0 37 | 15713144,Male,35,27000,0 38 | 15690188,Female,33,28000,0 39 | 15689425,Male,30,49000,0 40 | 15671766,Female,26,72000,0 41 | 15782806,Female,27,31000,0 42 | 15764419,Female,27,17000,0 43 | 15591915,Female,33,51000,0 44 | 15772798,Male,35,108000,0 45 | 15792008,Male,30,15000,0 46 | 15715541,Female,28,84000,0 47 | 15639277,Male,23,20000,0 48 | 15798850,Male,25,79000,0 49 | 15776348,Female,27,54000,0 50 | 15727696,Male,30,135000,1 51 | 15793813,Female,31,89000,0 52 | 15694395,Female,24,32000,0 53 | 15764195,Female,18,44000,0 54 | 15744919,Female,29,83000,0 55 | 15671655,Female,35,23000,0 56 | 15654901,Female,27,58000,0 57 | 15649136,Female,24,55000,0 58 | 15775562,Female,23,48000,0 59 | 15807481,Male,28,79000,0 60 | 15642885,Male,22,18000,0 61 | 15789109,Female,32,117000,0 62 | 15814004,Male,27,20000,0 63 | 15673619,Male,25,87000,0 64 | 15595135,Female,23,66000,0 65 | 15583681,Male,32,120000,1 66 | 15605000,Female,59,83000,0 67 | 15718071,Male,24,58000,0 68 | 15679760,Male,24,19000,0 69 | 15654574,Female,23,82000,0 70 | 15577178,Female,22,63000,0 71 | 15595324,Female,31,68000,0 72 | 15756932,Male,25,80000,0 73 | 15726358,Female,24,27000,0 74 | 15595228,Female,20,23000,0 75 | 15782530,Female,33,113000,0 76 | 15592877,Male,32,18000,0 77 | 15651983,Male,34,112000,1 78 | 15746737,Male,18,52000,0 79 | 15774179,Female,22,27000,0 80 | 15667265,Female,28,87000,0 81 | 15655123,Female,26,17000,0 82 | 15595917,Male,30,80000,0 83 | 15668385,Male,39,42000,0 84 | 15709476,Male,20,49000,0 85 | 15711218,Male,35,88000,0 86 | 15798659,Female,30,62000,0 87 | 15663939,Female,31,118000,1 88 | 15694946,Male,24,55000,0 89 | 15631912,Female,28,85000,0 90 | 15768816,Male,26,81000,0 91 | 15682268,Male,35,50000,0 92 | 15684801,Male,22,81000,0 93 | 15636428,Female,30,116000,0 94 | 15809823,Male,26,15000,0 95 | 15699284,Female,29,28000,0 96 | 15786993,Female,29,83000,0 97 | 15709441,Female,35,44000,0 98 | 15710257,Female,35,25000,0 99 | 15582492,Male,28,123000,1 100 | 15575694,Male,35,73000,0 101 | 15756820,Female,28,37000,0 102 | 15766289,Male,27,88000,0 103 | 15593014,Male,28,59000,0 104 | 15584545,Female,32,86000,0 105 | 15675949,Female,33,149000,1 106 | 15672091,Female,19,21000,0 107 | 15801658,Male,21,72000,0 108 | 15706185,Female,26,35000,0 109 | 15789863,Male,27,89000,0 110 | 15720943,Male,26,86000,0 111 | 15697997,Female,38,80000,0 112 | 15665416,Female,39,71000,0 113 | 15660200,Female,37,71000,0 114 | 15619653,Male,38,61000,0 115 | 15773447,Male,37,55000,0 116 | 15739160,Male,42,80000,0 117 | 15689237,Male,40,57000,0 118 | 15679297,Male,35,75000,0 119 | 15591433,Male,36,52000,0 120 | 15642725,Male,40,59000,0 121 | 15701962,Male,41,59000,0 122 | 15811613,Female,36,75000,0 123 | 15741049,Male,37,72000,0 124 | 15724423,Female,40,75000,0 125 | 15574305,Male,35,53000,0 126 | 15678168,Female,41,51000,0 127 | 15697020,Female,39,61000,0 128 | 15610801,Male,42,65000,0 129 | 15745232,Male,26,32000,0 130 | 15722758,Male,30,17000,0 131 | 15792102,Female,26,84000,0 132 | 15675185,Male,31,58000,0 133 | 15801247,Male,33,31000,0 134 | 15725660,Male,30,87000,0 135 | 15638963,Female,21,68000,0 136 | 15800061,Female,28,55000,0 137 | 15578006,Male,23,63000,0 138 | 15668504,Female,20,82000,0 139 | 15687491,Male,30,107000,1 140 | 15610403,Female,28,59000,0 141 | 15741094,Male,19,25000,0 142 | 15807909,Male,19,85000,0 143 | 15666141,Female,18,68000,0 144 | 15617134,Male,35,59000,0 145 | 15783029,Male,30,89000,0 146 | 15622833,Female,34,25000,0 147 | 15746422,Female,24,89000,0 148 | 15750839,Female,27,96000,1 149 | 15749130,Female,41,30000,0 150 | 15779862,Male,29,61000,0 151 | 15767871,Male,20,74000,0 152 | 15679651,Female,26,15000,0 153 | 15576219,Male,41,45000,0 154 | 15699247,Male,31,76000,0 155 | 15619087,Female,36,50000,0 156 | 15605327,Male,40,47000,0 157 | 15610140,Female,31,15000,0 158 | 15791174,Male,46,59000,0 159 | 15602373,Male,29,75000,0 160 | 15762605,Male,26,30000,0 161 | 15598840,Female,32,135000,1 162 | 15744279,Male,32,100000,1 163 | 15670619,Male,25,90000,0 164 | 15599533,Female,37,33000,0 165 | 15757837,Male,35,38000,0 166 | 15697574,Female,33,69000,0 167 | 15578738,Female,18,86000,0 168 | 15762228,Female,22,55000,0 169 | 15614827,Female,35,71000,0 170 | 15789815,Male,29,148000,1 171 | 15579781,Female,29,47000,0 172 | 15587013,Male,21,88000,0 173 | 15570932,Male,34,115000,0 174 | 15794661,Female,26,118000,0 175 | 15581654,Female,34,43000,0 176 | 15644296,Female,34,72000,0 177 | 15614420,Female,23,28000,0 178 | 15609653,Female,35,47000,0 179 | 15594577,Male,25,22000,0 180 | 15584114,Male,24,23000,0 181 | 15673367,Female,31,34000,0 182 | 15685576,Male,26,16000,0 183 | 15774727,Female,31,71000,0 184 | 15694288,Female,32,117000,1 185 | 15603319,Male,33,43000,0 186 | 15759066,Female,33,60000,0 187 | 15814816,Male,31,66000,0 188 | 15724402,Female,20,82000,0 189 | 15571059,Female,33,41000,0 190 | 15674206,Male,35,72000,0 191 | 15715160,Male,28,32000,0 192 | 15730448,Male,24,84000,0 193 | 15662067,Female,19,26000,0 194 | 15779581,Male,29,43000,0 195 | 15662901,Male,19,70000,0 196 | 15689751,Male,28,89000,0 197 | 15667742,Male,34,43000,0 198 | 15738448,Female,30,79000,0 199 | 15680243,Female,20,36000,0 200 | 15745083,Male,26,80000,0 201 | 15708228,Male,35,22000,0 202 | 15628523,Male,35,39000,0 203 | 15708196,Male,49,74000,0 204 | 15735549,Female,39,134000,1 205 | 15809347,Female,41,71000,0 206 | 15660866,Female,58,101000,1 207 | 15766609,Female,47,47000,0 208 | 15654230,Female,55,130000,1 209 | 15794566,Female,52,114000,0 210 | 15800890,Female,40,142000,1 211 | 15697424,Female,46,22000,0 212 | 15724536,Female,48,96000,1 213 | 15735878,Male,52,150000,1 214 | 15707596,Female,59,42000,0 215 | 15657163,Male,35,58000,0 216 | 15622478,Male,47,43000,0 217 | 15779529,Female,60,108000,1 218 | 15636023,Male,49,65000,0 219 | 15582066,Male,40,78000,0 220 | 15666675,Female,46,96000,0 221 | 15732987,Male,59,143000,1 222 | 15789432,Female,41,80000,0 223 | 15663161,Male,35,91000,1 224 | 15694879,Male,37,144000,1 225 | 15593715,Male,60,102000,1 226 | 15575002,Female,35,60000,0 227 | 15622171,Male,37,53000,0 228 | 15795224,Female,36,126000,1 229 | 15685346,Male,56,133000,1 230 | 15691808,Female,40,72000,0 231 | 15721007,Female,42,80000,1 232 | 15794253,Female,35,147000,1 233 | 15694453,Male,39,42000,0 234 | 15813113,Male,40,107000,1 235 | 15614187,Male,49,86000,1 236 | 15619407,Female,38,112000,0 237 | 15646227,Male,46,79000,1 238 | 15660541,Male,40,57000,0 239 | 15753874,Female,37,80000,0 240 | 15617877,Female,46,82000,0 241 | 15772073,Female,53,143000,1 242 | 15701537,Male,42,149000,1 243 | 15736228,Male,38,59000,0 244 | 15780572,Female,50,88000,1 245 | 15769596,Female,56,104000,1 246 | 15586996,Female,41,72000,0 247 | 15722061,Female,51,146000,1 248 | 15638003,Female,35,50000,0 249 | 15775590,Female,57,122000,1 250 | 15730688,Male,41,52000,0 251 | 15753102,Female,35,97000,1 252 | 15810075,Female,44,39000,0 253 | 15723373,Male,37,52000,0 254 | 15795298,Female,48,134000,1 255 | 15584320,Female,37,146000,1 256 | 15724161,Female,50,44000,0 257 | 15750056,Female,52,90000,1 258 | 15609637,Female,41,72000,0 259 | 15794493,Male,40,57000,0 260 | 15569641,Female,58,95000,1 261 | 15815236,Female,45,131000,1 262 | 15811177,Female,35,77000,0 263 | 15680587,Male,36,144000,1 264 | 15672821,Female,55,125000,1 265 | 15767681,Female,35,72000,0 266 | 15600379,Male,48,90000,1 267 | 15801336,Female,42,108000,1 268 | 15721592,Male,40,75000,0 269 | 15581282,Male,37,74000,0 270 | 15746203,Female,47,144000,1 271 | 15583137,Male,40,61000,0 272 | 15680752,Female,43,133000,0 273 | 15688172,Female,59,76000,1 274 | 15791373,Male,60,42000,1 275 | 15589449,Male,39,106000,1 276 | 15692819,Female,57,26000,1 277 | 15727467,Male,57,74000,1 278 | 15734312,Male,38,71000,0 279 | 15764604,Male,49,88000,1 280 | 15613014,Female,52,38000,1 281 | 15759684,Female,50,36000,1 282 | 15609669,Female,59,88000,1 283 | 15685536,Male,35,61000,0 284 | 15750447,Male,37,70000,1 285 | 15663249,Female,52,21000,1 286 | 15638646,Male,48,141000,0 287 | 15734161,Female,37,93000,1 288 | 15631070,Female,37,62000,0 289 | 15761950,Female,48,138000,1 290 | 15649668,Male,41,79000,0 291 | 15713912,Female,37,78000,1 292 | 15586757,Male,39,134000,1 293 | 15596522,Male,49,89000,1 294 | 15625395,Male,55,39000,1 295 | 15760570,Male,37,77000,0 296 | 15566689,Female,35,57000,0 297 | 15725794,Female,36,63000,0 298 | 15673539,Male,42,73000,1 299 | 15705298,Female,43,112000,1 300 | 15675791,Male,45,79000,0 301 | 15747043,Male,46,117000,1 302 | 15736397,Female,58,38000,1 303 | 15678201,Male,48,74000,1 304 | 15720745,Female,37,137000,1 305 | 15637593,Male,37,79000,1 306 | 15598070,Female,40,60000,0 307 | 15787550,Male,42,54000,0 308 | 15603942,Female,51,134000,0 309 | 15733973,Female,47,113000,1 310 | 15596761,Male,36,125000,1 311 | 15652400,Female,38,50000,0 312 | 15717893,Female,42,70000,0 313 | 15622585,Male,39,96000,1 314 | 15733964,Female,38,50000,0 315 | 15753861,Female,49,141000,1 316 | 15747097,Female,39,79000,0 317 | 15594762,Female,39,75000,1 318 | 15667417,Female,54,104000,1 319 | 15684861,Male,35,55000,0 320 | 15742204,Male,45,32000,1 321 | 15623502,Male,36,60000,0 322 | 15774872,Female,52,138000,1 323 | 15611191,Female,53,82000,1 324 | 15674331,Male,41,52000,0 325 | 15619465,Female,48,30000,1 326 | 15575247,Female,48,131000,1 327 | 15695679,Female,41,60000,0 328 | 15713463,Male,41,72000,0 329 | 15785170,Female,42,75000,0 330 | 15796351,Male,36,118000,1 331 | 15639576,Female,47,107000,1 332 | 15693264,Male,38,51000,0 333 | 15589715,Female,48,119000,1 334 | 15769902,Male,42,65000,0 335 | 15587177,Male,40,65000,0 336 | 15814553,Male,57,60000,1 337 | 15601550,Female,36,54000,0 338 | 15664907,Male,58,144000,1 339 | 15612465,Male,35,79000,0 340 | 15810800,Female,38,55000,0 341 | 15665760,Male,39,122000,1 342 | 15588080,Female,53,104000,1 343 | 15776844,Male,35,75000,0 344 | 15717560,Female,38,65000,0 345 | 15629739,Female,47,51000,1 346 | 15729908,Male,47,105000,1 347 | 15716781,Female,41,63000,0 348 | 15646936,Male,53,72000,1 349 | 15768151,Female,54,108000,1 350 | 15579212,Male,39,77000,0 351 | 15721835,Male,38,61000,0 352 | 15800515,Female,38,113000,1 353 | 15591279,Male,37,75000,0 354 | 15587419,Female,42,90000,1 355 | 15750335,Female,37,57000,0 356 | 15699619,Male,36,99000,1 357 | 15606472,Male,60,34000,1 358 | 15778368,Male,54,70000,1 359 | 15671387,Female,41,72000,0 360 | 15573926,Male,40,71000,1 361 | 15709183,Male,42,54000,0 362 | 15577514,Male,43,129000,1 363 | 15778830,Female,53,34000,1 364 | 15768072,Female,47,50000,1 365 | 15768293,Female,42,79000,0 366 | 15654456,Male,42,104000,1 367 | 15807525,Female,59,29000,1 368 | 15574372,Female,58,47000,1 369 | 15671249,Male,46,88000,1 370 | 15779744,Male,38,71000,0 371 | 15624755,Female,54,26000,1 372 | 15611430,Female,60,46000,1 373 | 15774744,Male,60,83000,1 374 | 15629885,Female,39,73000,0 375 | 15708791,Male,59,130000,1 376 | 15793890,Female,37,80000,0 377 | 15646091,Female,46,32000,1 378 | 15596984,Female,46,74000,0 379 | 15800215,Female,42,53000,0 380 | 15577806,Male,41,87000,1 381 | 15749381,Female,58,23000,1 382 | 15683758,Male,42,64000,0 383 | 15670615,Male,48,33000,1 384 | 15715622,Female,44,139000,1 385 | 15707634,Male,49,28000,1 386 | 15806901,Female,57,33000,1 387 | 15775335,Male,56,60000,1 388 | 15724150,Female,49,39000,1 389 | 15627220,Male,39,71000,0 390 | 15672330,Male,47,34000,1 391 | 15668521,Female,48,35000,1 392 | 15807837,Male,48,33000,1 393 | 15592570,Male,47,23000,1 394 | 15748589,Female,45,45000,1 395 | 15635893,Male,60,42000,1 396 | 15757632,Female,39,59000,0 397 | 15691863,Female,46,41000,1 398 | 15706071,Male,51,23000,1 399 | 15654296,Female,50,20000,1 400 | 15755018,Male,36,33000,0 401 | 15594041,Female,49,36000,1 -------------------------------------------------------------------------------- /machine_learning/Naive Bayes Classification/Customer_Behaviour.csv: -------------------------------------------------------------------------------- 1 | User ID,Gender,Age,EstimatedSalary,Purchased 2 | 15624510,Male,19,19000,0 3 | 15810944,Male,35,20000,0 4 | 15668575,Female,26,43000,0 5 | 15603246,Female,27,57000,0 6 | 15804002,Male,19,76000,0 7 | 15728773,Male,27,58000,0 8 | 15598044,Female,27,84000,0 9 | 15694829,Female,32,150000,1 10 | 15600575,Male,25,33000,0 11 | 15727311,Female,35,65000,0 12 | 15570769,Female,26,80000,0 13 | 15606274,Female,26,52000,0 14 | 15746139,Male,20,86000,0 15 | 15704987,Male,32,18000,0 16 | 15628972,Male,18,82000,0 17 | 15697686,Male,29,80000,0 18 | 15733883,Male,47,25000,1 19 | 15617482,Male,45,26000,1 20 | 15704583,Male,46,28000,1 21 | 15621083,Female,48,29000,1 22 | 15649487,Male,45,22000,1 23 | 15736760,Female,47,49000,1 24 | 15714658,Male,48,41000,1 25 | 15599081,Female,45,22000,1 26 | 15705113,Male,46,23000,1 27 | 15631159,Male,47,20000,1 28 | 15792818,Male,49,28000,1 29 | 15633531,Female,47,30000,1 30 | 15744529,Male,29,43000,0 31 | 15669656,Male,31,18000,0 32 | 15581198,Male,31,74000,0 33 | 15729054,Female,27,137000,1 34 | 15573452,Female,21,16000,0 35 | 15776733,Female,28,44000,0 36 | 15724858,Male,27,90000,0 37 | 15713144,Male,35,27000,0 38 | 15690188,Female,33,28000,0 39 | 15689425,Male,30,49000,0 40 | 15671766,Female,26,72000,0 41 | 15782806,Female,27,31000,0 42 | 15764419,Female,27,17000,0 43 | 15591915,Female,33,51000,0 44 | 15772798,Male,35,108000,0 45 | 15792008,Male,30,15000,0 46 | 15715541,Female,28,84000,0 47 | 15639277,Male,23,20000,0 48 | 15798850,Male,25,79000,0 49 | 15776348,Female,27,54000,0 50 | 15727696,Male,30,135000,1 51 | 15793813,Female,31,89000,0 52 | 15694395,Female,24,32000,0 53 | 15764195,Female,18,44000,0 54 | 15744919,Female,29,83000,0 55 | 15671655,Female,35,23000,0 56 | 15654901,Female,27,58000,0 57 | 15649136,Female,24,55000,0 58 | 15775562,Female,23,48000,0 59 | 15807481,Male,28,79000,0 60 | 15642885,Male,22,18000,0 61 | 15789109,Female,32,117000,0 62 | 15814004,Male,27,20000,0 63 | 15673619,Male,25,87000,0 64 | 15595135,Female,23,66000,0 65 | 15583681,Male,32,120000,1 66 | 15605000,Female,59,83000,0 67 | 15718071,Male,24,58000,0 68 | 15679760,Male,24,19000,0 69 | 15654574,Female,23,82000,0 70 | 15577178,Female,22,63000,0 71 | 15595324,Female,31,68000,0 72 | 15756932,Male,25,80000,0 73 | 15726358,Female,24,27000,0 74 | 15595228,Female,20,23000,0 75 | 15782530,Female,33,113000,0 76 | 15592877,Male,32,18000,0 77 | 15651983,Male,34,112000,1 78 | 15746737,Male,18,52000,0 79 | 15774179,Female,22,27000,0 80 | 15667265,Female,28,87000,0 81 | 15655123,Female,26,17000,0 82 | 15595917,Male,30,80000,0 83 | 15668385,Male,39,42000,0 84 | 15709476,Male,20,49000,0 85 | 15711218,Male,35,88000,0 86 | 15798659,Female,30,62000,0 87 | 15663939,Female,31,118000,1 88 | 15694946,Male,24,55000,0 89 | 15631912,Female,28,85000,0 90 | 15768816,Male,26,81000,0 91 | 15682268,Male,35,50000,0 92 | 15684801,Male,22,81000,0 93 | 15636428,Female,30,116000,0 94 | 15809823,Male,26,15000,0 95 | 15699284,Female,29,28000,0 96 | 15786993,Female,29,83000,0 97 | 15709441,Female,35,44000,0 98 | 15710257,Female,35,25000,0 99 | 15582492,Male,28,123000,1 100 | 15575694,Male,35,73000,0 101 | 15756820,Female,28,37000,0 102 | 15766289,Male,27,88000,0 103 | 15593014,Male,28,59000,0 104 | 15584545,Female,32,86000,0 105 | 15675949,Female,33,149000,1 106 | 15672091,Female,19,21000,0 107 | 15801658,Male,21,72000,0 108 | 15706185,Female,26,35000,0 109 | 15789863,Male,27,89000,0 110 | 15720943,Male,26,86000,0 111 | 15697997,Female,38,80000,0 112 | 15665416,Female,39,71000,0 113 | 15660200,Female,37,71000,0 114 | 15619653,Male,38,61000,0 115 | 15773447,Male,37,55000,0 116 | 15739160,Male,42,80000,0 117 | 15689237,Male,40,57000,0 118 | 15679297,Male,35,75000,0 119 | 15591433,Male,36,52000,0 120 | 15642725,Male,40,59000,0 121 | 15701962,Male,41,59000,0 122 | 15811613,Female,36,75000,0 123 | 15741049,Male,37,72000,0 124 | 15724423,Female,40,75000,0 125 | 15574305,Male,35,53000,0 126 | 15678168,Female,41,51000,0 127 | 15697020,Female,39,61000,0 128 | 15610801,Male,42,65000,0 129 | 15745232,Male,26,32000,0 130 | 15722758,Male,30,17000,0 131 | 15792102,Female,26,84000,0 132 | 15675185,Male,31,58000,0 133 | 15801247,Male,33,31000,0 134 | 15725660,Male,30,87000,0 135 | 15638963,Female,21,68000,0 136 | 15800061,Female,28,55000,0 137 | 15578006,Male,23,63000,0 138 | 15668504,Female,20,82000,0 139 | 15687491,Male,30,107000,1 140 | 15610403,Female,28,59000,0 141 | 15741094,Male,19,25000,0 142 | 15807909,Male,19,85000,0 143 | 15666141,Female,18,68000,0 144 | 15617134,Male,35,59000,0 145 | 15783029,Male,30,89000,0 146 | 15622833,Female,34,25000,0 147 | 15746422,Female,24,89000,0 148 | 15750839,Female,27,96000,1 149 | 15749130,Female,41,30000,0 150 | 15779862,Male,29,61000,0 151 | 15767871,Male,20,74000,0 152 | 15679651,Female,26,15000,0 153 | 15576219,Male,41,45000,0 154 | 15699247,Male,31,76000,0 155 | 15619087,Female,36,50000,0 156 | 15605327,Male,40,47000,0 157 | 15610140,Female,31,15000,0 158 | 15791174,Male,46,59000,0 159 | 15602373,Male,29,75000,0 160 | 15762605,Male,26,30000,0 161 | 15598840,Female,32,135000,1 162 | 15744279,Male,32,100000,1 163 | 15670619,Male,25,90000,0 164 | 15599533,Female,37,33000,0 165 | 15757837,Male,35,38000,0 166 | 15697574,Female,33,69000,0 167 | 15578738,Female,18,86000,0 168 | 15762228,Female,22,55000,0 169 | 15614827,Female,35,71000,0 170 | 15789815,Male,29,148000,1 171 | 15579781,Female,29,47000,0 172 | 15587013,Male,21,88000,0 173 | 15570932,Male,34,115000,0 174 | 15794661,Female,26,118000,0 175 | 15581654,Female,34,43000,0 176 | 15644296,Female,34,72000,0 177 | 15614420,Female,23,28000,0 178 | 15609653,Female,35,47000,0 179 | 15594577,Male,25,22000,0 180 | 15584114,Male,24,23000,0 181 | 15673367,Female,31,34000,0 182 | 15685576,Male,26,16000,0 183 | 15774727,Female,31,71000,0 184 | 15694288,Female,32,117000,1 185 | 15603319,Male,33,43000,0 186 | 15759066,Female,33,60000,0 187 | 15814816,Male,31,66000,0 188 | 15724402,Female,20,82000,0 189 | 15571059,Female,33,41000,0 190 | 15674206,Male,35,72000,0 191 | 15715160,Male,28,32000,0 192 | 15730448,Male,24,84000,0 193 | 15662067,Female,19,26000,0 194 | 15779581,Male,29,43000,0 195 | 15662901,Male,19,70000,0 196 | 15689751,Male,28,89000,0 197 | 15667742,Male,34,43000,0 198 | 15738448,Female,30,79000,0 199 | 15680243,Female,20,36000,0 200 | 15745083,Male,26,80000,0 201 | 15708228,Male,35,22000,0 202 | 15628523,Male,35,39000,0 203 | 15708196,Male,49,74000,0 204 | 15735549,Female,39,134000,1 205 | 15809347,Female,41,71000,0 206 | 15660866,Female,58,101000,1 207 | 15766609,Female,47,47000,0 208 | 15654230,Female,55,130000,1 209 | 15794566,Female,52,114000,0 210 | 15800890,Female,40,142000,1 211 | 15697424,Female,46,22000,0 212 | 15724536,Female,48,96000,1 213 | 15735878,Male,52,150000,1 214 | 15707596,Female,59,42000,0 215 | 15657163,Male,35,58000,0 216 | 15622478,Male,47,43000,0 217 | 15779529,Female,60,108000,1 218 | 15636023,Male,49,65000,0 219 | 15582066,Male,40,78000,0 220 | 15666675,Female,46,96000,0 221 | 15732987,Male,59,143000,1 222 | 15789432,Female,41,80000,0 223 | 15663161,Male,35,91000,1 224 | 15694879,Male,37,144000,1 225 | 15593715,Male,60,102000,1 226 | 15575002,Female,35,60000,0 227 | 15622171,Male,37,53000,0 228 | 15795224,Female,36,126000,1 229 | 15685346,Male,56,133000,1 230 | 15691808,Female,40,72000,0 231 | 15721007,Female,42,80000,1 232 | 15794253,Female,35,147000,1 233 | 15694453,Male,39,42000,0 234 | 15813113,Male,40,107000,1 235 | 15614187,Male,49,86000,1 236 | 15619407,Female,38,112000,0 237 | 15646227,Male,46,79000,1 238 | 15660541,Male,40,57000,0 239 | 15753874,Female,37,80000,0 240 | 15617877,Female,46,82000,0 241 | 15772073,Female,53,143000,1 242 | 15701537,Male,42,149000,1 243 | 15736228,Male,38,59000,0 244 | 15780572,Female,50,88000,1 245 | 15769596,Female,56,104000,1 246 | 15586996,Female,41,72000,0 247 | 15722061,Female,51,146000,1 248 | 15638003,Female,35,50000,0 249 | 15775590,Female,57,122000,1 250 | 15730688,Male,41,52000,0 251 | 15753102,Female,35,97000,1 252 | 15810075,Female,44,39000,0 253 | 15723373,Male,37,52000,0 254 | 15795298,Female,48,134000,1 255 | 15584320,Female,37,146000,1 256 | 15724161,Female,50,44000,0 257 | 15750056,Female,52,90000,1 258 | 15609637,Female,41,72000,0 259 | 15794493,Male,40,57000,0 260 | 15569641,Female,58,95000,1 261 | 15815236,Female,45,131000,1 262 | 15811177,Female,35,77000,0 263 | 15680587,Male,36,144000,1 264 | 15672821,Female,55,125000,1 265 | 15767681,Female,35,72000,0 266 | 15600379,Male,48,90000,1 267 | 15801336,Female,42,108000,1 268 | 15721592,Male,40,75000,0 269 | 15581282,Male,37,74000,0 270 | 15746203,Female,47,144000,1 271 | 15583137,Male,40,61000,0 272 | 15680752,Female,43,133000,0 273 | 15688172,Female,59,76000,1 274 | 15791373,Male,60,42000,1 275 | 15589449,Male,39,106000,1 276 | 15692819,Female,57,26000,1 277 | 15727467,Male,57,74000,1 278 | 15734312,Male,38,71000,0 279 | 15764604,Male,49,88000,1 280 | 15613014,Female,52,38000,1 281 | 15759684,Female,50,36000,1 282 | 15609669,Female,59,88000,1 283 | 15685536,Male,35,61000,0 284 | 15750447,Male,37,70000,1 285 | 15663249,Female,52,21000,1 286 | 15638646,Male,48,141000,0 287 | 15734161,Female,37,93000,1 288 | 15631070,Female,37,62000,0 289 | 15761950,Female,48,138000,1 290 | 15649668,Male,41,79000,0 291 | 15713912,Female,37,78000,1 292 | 15586757,Male,39,134000,1 293 | 15596522,Male,49,89000,1 294 | 15625395,Male,55,39000,1 295 | 15760570,Male,37,77000,0 296 | 15566689,Female,35,57000,0 297 | 15725794,Female,36,63000,0 298 | 15673539,Male,42,73000,1 299 | 15705298,Female,43,112000,1 300 | 15675791,Male,45,79000,0 301 | 15747043,Male,46,117000,1 302 | 15736397,Female,58,38000,1 303 | 15678201,Male,48,74000,1 304 | 15720745,Female,37,137000,1 305 | 15637593,Male,37,79000,1 306 | 15598070,Female,40,60000,0 307 | 15787550,Male,42,54000,0 308 | 15603942,Female,51,134000,0 309 | 15733973,Female,47,113000,1 310 | 15596761,Male,36,125000,1 311 | 15652400,Female,38,50000,0 312 | 15717893,Female,42,70000,0 313 | 15622585,Male,39,96000,1 314 | 15733964,Female,38,50000,0 315 | 15753861,Female,49,141000,1 316 | 15747097,Female,39,79000,0 317 | 15594762,Female,39,75000,1 318 | 15667417,Female,54,104000,1 319 | 15684861,Male,35,55000,0 320 | 15742204,Male,45,32000,1 321 | 15623502,Male,36,60000,0 322 | 15774872,Female,52,138000,1 323 | 15611191,Female,53,82000,1 324 | 15674331,Male,41,52000,0 325 | 15619465,Female,48,30000,1 326 | 15575247,Female,48,131000,1 327 | 15695679,Female,41,60000,0 328 | 15713463,Male,41,72000,0 329 | 15785170,Female,42,75000,0 330 | 15796351,Male,36,118000,1 331 | 15639576,Female,47,107000,1 332 | 15693264,Male,38,51000,0 333 | 15589715,Female,48,119000,1 334 | 15769902,Male,42,65000,0 335 | 15587177,Male,40,65000,0 336 | 15814553,Male,57,60000,1 337 | 15601550,Female,36,54000,0 338 | 15664907,Male,58,144000,1 339 | 15612465,Male,35,79000,0 340 | 15810800,Female,38,55000,0 341 | 15665760,Male,39,122000,1 342 | 15588080,Female,53,104000,1 343 | 15776844,Male,35,75000,0 344 | 15717560,Female,38,65000,0 345 | 15629739,Female,47,51000,1 346 | 15729908,Male,47,105000,1 347 | 15716781,Female,41,63000,0 348 | 15646936,Male,53,72000,1 349 | 15768151,Female,54,108000,1 350 | 15579212,Male,39,77000,0 351 | 15721835,Male,38,61000,0 352 | 15800515,Female,38,113000,1 353 | 15591279,Male,37,75000,0 354 | 15587419,Female,42,90000,1 355 | 15750335,Female,37,57000,0 356 | 15699619,Male,36,99000,1 357 | 15606472,Male,60,34000,1 358 | 15778368,Male,54,70000,1 359 | 15671387,Female,41,72000,0 360 | 15573926,Male,40,71000,1 361 | 15709183,Male,42,54000,0 362 | 15577514,Male,43,129000,1 363 | 15778830,Female,53,34000,1 364 | 15768072,Female,47,50000,1 365 | 15768293,Female,42,79000,0 366 | 15654456,Male,42,104000,1 367 | 15807525,Female,59,29000,1 368 | 15574372,Female,58,47000,1 369 | 15671249,Male,46,88000,1 370 | 15779744,Male,38,71000,0 371 | 15624755,Female,54,26000,1 372 | 15611430,Female,60,46000,1 373 | 15774744,Male,60,83000,1 374 | 15629885,Female,39,73000,0 375 | 15708791,Male,59,130000,1 376 | 15793890,Female,37,80000,0 377 | 15646091,Female,46,32000,1 378 | 15596984,Female,46,74000,0 379 | 15800215,Female,42,53000,0 380 | 15577806,Male,41,87000,1 381 | 15749381,Female,58,23000,1 382 | 15683758,Male,42,64000,0 383 | 15670615,Male,48,33000,1 384 | 15715622,Female,44,139000,1 385 | 15707634,Male,49,28000,1 386 | 15806901,Female,57,33000,1 387 | 15775335,Male,56,60000,1 388 | 15724150,Female,49,39000,1 389 | 15627220,Male,39,71000,0 390 | 15672330,Male,47,34000,1 391 | 15668521,Female,48,35000,1 392 | 15807837,Male,48,33000,1 393 | 15592570,Male,47,23000,1 394 | 15748589,Female,45,45000,1 395 | 15635893,Male,60,42000,1 396 | 15757632,Female,39,59000,0 397 | 15691863,Female,46,41000,1 398 | 15706071,Male,51,23000,1 399 | 15654296,Female,50,20000,1 400 | 15755018,Male,36,33000,0 401 | 15594041,Female,49,36000,1 402 | -------------------------------------------------------------------------------- /neural_network/qlearning.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np\n", 10 | "import pickle" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 3, 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [ 19 | "GAMMA = 0.9\n", 20 | "LEARNING_RATE = 0.6" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 4, 26 | "metadata": {}, 27 | "outputs": [], 28 | "source": [ 29 | "if __name__ == \"__main__\":\n", 30 | " with open(\"Qtable.txt\", \"rb\") as f:\n", 31 | " \"\"\"\n", 32 | " This code is responsible for loading Qtable.txt if already present\n", 33 | " \"\"\"\n", 34 | " Q_table = pickle.load(f)" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 5, 40 | "metadata": {}, 41 | "outputs": [], 42 | "source": [ 43 | "def next_best_action(state: int, Q_table: np.ndarray) -> int:\n", 44 | " \"\"\"\n", 45 | " Return the most suitable action value from Q_table or a random action\n", 46 | " if there is no np.argmax(Q_table[state]).\n", 47 | " >>> next_best_action(1, []) in [0, 1, 2, 3]\n", 48 | " True\n", 49 | " \"\"\"\n", 50 | " action = np.argmax(Q_table[state])\n", 51 | " return action if action is not None else np.random.choice([0, 1, 2, 3])" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 6, 57 | "metadata": {}, 58 | "outputs": [], 59 | "source": [ 60 | "def state_action_reward(player: object, x_food: int, y_food: int) -> tuple:\n", 61 | " \"\"\"\n", 62 | " This function returns state, action and reward to update the Qtable.\n", 63 | " \"\"\"\n", 64 | " x_agent, y_agent = player.body[0].pos\n", 65 | " current_state = state(player, x_food, y_food)\n", 66 | " current_action = next_best_action(state, Q_table)\n", 67 | " current_reward = reward(player, x_food, y_food)\n", 68 | " return (current_state, current_action, current_reward)" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": 7, 74 | "metadata": {}, 75 | "outputs": [], 76 | "source": [ 77 | "# States to consider:\n", 78 | "# * Food's relative positioning wrt the head\n", 79 | "# Checking for food in nine directions\n", 80 | "###\n", 81 | "# #\n", 82 | "###\n", 83 | "# * obstruction Ahead, Right, Left\n", 84 | "\n", 85 | "def state(player: object, x_food: int, y_food: int) -> int:\n", 86 | " \"\"\"\n", 87 | " This function Checks for the food in nine directions and returns state.\n", 88 | " \"\"\"\n", 89 | " x_agent, y_agent = player.body[0].pos\n", 90 | " states = []\n", 91 | "\n", 92 | " # Code to check for obstacles in front of the agent\n", 93 | " DangerAhead = (\n", 94 | " (player.dirnx == -1 and x_agent + player.delx < 0)\n", 95 | " or (player.dirnx == -1 and ((x_agent + player.delx, y_agent) in player.body))\n", 96 | " or (player.dirnx == 1 and x_agent + player.delx > 14)\n", 97 | " or (player.dirnx == 1 and ((x_agent + player.delx, y_agent) in player.body))\n", 98 | " or (player.dirny == 1 and y_agent + player.dely > 14)\n", 99 | " or (player.dirny == 1 and ((x_agent, y_agent + player.dely) in player.body))\n", 100 | " or (player.dirny == -1 and y_agent + player.dely < 0)\n", 101 | " or (player.dirny == -1 and ((x_agent, y_agent + player.dely) in player.body))\n", 102 | " )\n", 103 | " # Code to check for obstacles to the left of the agent\n", 104 | " DangerLeft = (\n", 105 | " (player.dirnx == -1 and y_agent + 1 > 14)\n", 106 | " or (player.dirnx == -1 and ((x_agent, y_agent + 1) in player.body))\n", 107 | " or (player.dirnx == 1 and y_agent - 1 < 0)\n", 108 | " or (player.dirnx == 1 and ((x_agent, y_agent - 1) in player.body))\n", 109 | " or (player.dirny == 1 and x_agent + 1 > 14)\n", 110 | " or (player.dirny == 1 and ((x_agent + 1, y_agent) in player.body))\n", 111 | " or (player.dirny == -1 and x_agent - 1 < 0)\n", 112 | " or (player.dirny == -1 and ((x_agent - 1, y_agent) in player.body))\n", 113 | " )\n", 114 | " # Code to check for obstacles to the right of the agent\n", 115 | " DangerRight = (\n", 116 | " (player.dirnx == -1 and y_agent - 1 < 0)\n", 117 | " or (player.dirnx == -1 and ((x_agent, y_agent - 1) in player.body))\n", 118 | " or (player.dirnx == 1 and y_agent + 1 > 14)\n", 119 | " or (player.dirnx == 1 and ((x_agent, y_agent + 1) in player.body))\n", 120 | " or (player.dirny == 1 and x_agent - 1 < 0)\n", 121 | " or (player.dirny == 1 and ((x_agent - 1, y_agent) in player.body))\n", 122 | " or (player.dirny == -1 and x_agent + 1 > 14)\n", 123 | " or (player.dirny == -1 and ((x_agent + 1, y_agent) in player.body))\n", 124 | " )\n", 125 | " # Code for food straight wrt head\n", 126 | " FoodStraightAhead = (\n", 127 | " (player.dirnx == 1 and y_agent == y_food and x_food > x_agent)\n", 128 | " or (player.dirnx == -1 and y_agent == y_food and x_food < x_agent)\n", 129 | " or (player.dirny == 1 and y_agent < y_food and x_food == x_agent)\n", 130 | " or (player.dirny == -1 and y_agent > y_food and x_food == x_agent)\n", 131 | " )\n", 132 | " # Code for food which is ahead and right wrt head\n", 133 | " FoodAheadRight = (\n", 134 | " (player.dirnx == 1 and y_agent < y_food and x_food > x_agent)\n", 135 | " or (player.dirnx == -1 and y_agent > y_food and x_food < x_agent)\n", 136 | " or (player.dirny == 1 and y_agent < y_food and x_food < x_agent)\n", 137 | " or (player.dirny == -1 and y_agent > y_food and x_food > x_agent)\n", 138 | " )\n", 139 | " # Code for food which is ahead and right wrt head\n", 140 | " FoodAheadLeft = (\n", 141 | " (player.dirnx == 1 and y_agent > y_food and x_food > x_agent)\n", 142 | " or (player.dirnx == -1 and y_agent < y_food and x_food < x_agent)\n", 143 | " or (player.dirny == 1 and y_agent < y_food and x_food > x_agent)\n", 144 | " or (player.dirny == -1 and y_agent > y_food and x_food < x_agent)\n", 145 | " )\n", 146 | " # Code for food which is ahead and right wrt head\n", 147 | " FoodBehindRight = (\n", 148 | " (player.dirnx == 1 and y_agent < y_food and x_food < x_agent)\n", 149 | " or (player.dirnx == -1 and y_agent > y_food and x_food > x_agent)\n", 150 | " or (player.dirny == 1 and y_agent > y_food and x_food < x_agent)\n", 151 | " or (player.dirny == -1 and y_agent < y_food and x_food > x_agent)\n", 152 | " )\n", 153 | " # Code for food which is ahead and right wrt head\n", 154 | " FoodBehindLeft = (\n", 155 | " (player.dirnx == 1 and y_agent > y_food and x_food < x_agent)\n", 156 | " or (player.dirnx == -1 and y_agent < y_food and x_food > x_agent)\n", 157 | " or (player.dirny == 1 and y_agent > y_food and x_food > x_agent)\n", 158 | " or (player.dirny == -1 and y_agent < y_food and x_food < x_agent)\n", 159 | " )\n", 160 | " # Code for food exactly behind\n", 161 | " FoodBehind = (\n", 162 | " (player.dirnx == 1 and y_agent == y_food and x_food < x_agent)\n", 163 | " or (player.dirnx == -1 and y_agent == y_food and x_food > x_agent)\n", 164 | " or (player.dirny == 1 and y_agent > y_food and x_food == x_agent)\n", 165 | " or (player.dirny == -1 and y_agent < y_food and x_food == x_agent)\n", 166 | " )\n", 167 | " # Code for food left\n", 168 | " FoodLeft = (\n", 169 | " (player.dirnx == 1 and y_agent > y_food and x_food == x_agent)\n", 170 | " or (player.dirnx == -1 and y_agent < y_food and x_food == x_agent)\n", 171 | " or (player.dirny == 1 and y_agent == y_food and x_food > x_agent)\n", 172 | " or (player.dirny == -1 and y_agent == y_food and x_food < x_agent)\n", 173 | " )\n", 174 | " # Code for food right\n", 175 | " FoodRight = (\n", 176 | " (player.dirnx == 1 and y_agent < y_food and x_food == x_agent)\n", 177 | " or (player.dirnx == -1 and y_agent > y_food and x_food == x_agent)\n", 178 | " or (player.dirny == 1 and y_agent == y_food and x_food < x_agent)\n", 179 | " or (player.dirny == -1 and y_agent == y_food and x_food > x_agent)\n", 180 | " )\n", 181 | " # Adding to states list while priortizing danger over eating food\n", 182 | " states = [int(x) for x in (DangerAhead, DangerLeft, DangerRight)]\n", 183 | " if sum(states) == 0:\n", 184 | " states += [int(x) for x in (\n", 185 | " FoodStraightAhead, FoodAheadRight, FoodAheadLeft, FoodBehindRight,\n", 186 | " FoodBehindLeft, FoodBehind, FoodLeft, FoodRight\n", 187 | " )]\n", 188 | " else:\n", 189 | " states += [0] * 8\n", 190 | " state = 0\n", 191 | " for i in range(11):\n", 192 | " if states[i] == 1:\n", 193 | " state = i\n", 194 | " return state" 195 | ] 196 | }, 197 | { 198 | "cell_type": "code", 199 | "execution_count": 8, 200 | "metadata": {}, 201 | "outputs": [], 202 | "source": [ 203 | "def euler_dist(x1: int, y1: int, x2: int, y2: int) -> int:\n", 204 | " \"\"\"\n", 205 | " For calculation of Euler Distance.\n", 206 | " \"\"\"\n", 207 | " dist = (x1 - x2) ** 2 + (y1 - y2) ** 2\n", 208 | " return dist ** 0.5" 209 | ] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": 9, 214 | "metadata": {}, 215 | "outputs": [], 216 | "source": [ 217 | "# Reward conditions:\n", 218 | "# * +10 for eating food\n", 219 | "# * -12 for dying\n", 220 | "# * -2 for getting closer\n", 221 | "# * -7 for going away from the fruit\n", 222 | "\n", 223 | "\n", 224 | "def reward(player: object, x_food: int, y_food: int) -> int:\n", 225 | " \"\"\"\n", 226 | " This function assigns the reward to the agent according to the action taken\n", 227 | " \"\"\"\n", 228 | " x_agent, y_agent = player.body[0].pos\n", 229 | " if x_agent == x_food and y_agent == y_food:\n", 230 | " return +10\n", 231 | " elif (\n", 232 | " (player.dirnx == -1 and x_agent + player.delx <= 0)\n", 233 | " or (\n", 234 | " player.dirnx == -1\n", 235 | " and ((x_agent + player.delx, y_agent + player.dely) in player.body)\n", 236 | " )\n", 237 | " or (player.dirnx == 1 and x_agent + player.delx >= 14)\n", 238 | " or (\n", 239 | " player.dirnx == 1\n", 240 | " and ((x_agent + player.dirnx, y_agent + player.dely) in player.body)\n", 241 | " )\n", 242 | " or (player.dirny == 1 and y_agent + player.dely >= 14)\n", 243 | " or (\n", 244 | " player.dirny == 1\n", 245 | " and ((x_agent + player.delx, y_agent + player.dely) in player.body)\n", 246 | " )\n", 247 | " or (player.dirny == -1 and y_agent + player.dely <= 0)\n", 248 | " or (\n", 249 | " player.dirny == -1\n", 250 | " and ((x_agent + player.delx, y_agent + player.dely) in player.body)\n", 251 | " )\n", 252 | " or (player.resetDone is True)\n", 253 | " ):\n", 254 | " return -12\n", 255 | " elif (\n", 256 | " euler_dist(x_agent + player.delx, y_agent + player.dely, x_food, y_food)\n", 257 | " - euler_dist(x_agent, y_agent, x_food, y_food)\n", 258 | " > 0\n", 259 | " ):\n", 260 | " return -2\n", 261 | " elif (\n", 262 | " euler_dist(x_agent + player.delx, y_agent + player.dely, x_food, y_food)\n", 263 | " - euler_dist(x_agent, y_agent, x_food, y_food)\n", 264 | " < 0\n", 265 | " ):\n", 266 | " return -7\n" 267 | ] 268 | }, 269 | { 270 | "cell_type": "code", 271 | "execution_count": 10, 272 | "metadata": {}, 273 | "outputs": [], 274 | "source": [ 275 | "def learn(\n", 276 | " state: int, action: int, reward: int, next_state: int,\n", 277 | " next_action: int, i: int, trialNumber: int, epsilon: float) -> type(None):\n", 278 | " \"\"\"\n", 279 | " This function is for iteratively updating the Qtable.\n", 280 | " \"\"\"\n", 281 | " currentQ = Q_table[state][action]\n", 282 | " nextQ = Q_table[next_state][next_action]\n", 283 | " # Qlearning Algorithm to get new q value for the q table.\n", 284 | " newQ = (1 - LEARNING_RATE) * currentQ + LEARNING_RATE * (reward + GAMMA * nextQ)\n", 285 | " Q_table[state][action] = newQ\n", 286 | " state = next_state\n", 287 | " currentQ = nextQ\n", 288 | " if trialNumber % 100 == 0:\n", 289 | " print(\"Printing Q_table: \")\n", 290 | " print(Q_table)" 291 | ] 292 | }, 293 | { 294 | "cell_type": "code", 295 | "execution_count": null, 296 | "metadata": {}, 297 | "outputs": [], 298 | "source": [] 299 | } 300 | ], 301 | "metadata": { 302 | "kernelspec": { 303 | "display_name": "Python 3", 304 | "language": "python", 305 | "name": "python3" 306 | }, 307 | "language_info": { 308 | "codemirror_mode": { 309 | "name": "ipython", 310 | "version": 3 311 | }, 312 | "file_extension": ".py", 313 | "mimetype": "text/x-python", 314 | "name": "python", 315 | "nbconvert_exporter": "python", 316 | "pygments_lexer": "ipython3", 317 | "version": "3.6.8" 318 | } 319 | }, 320 | "nbformat": 4, 321 | "nbformat_minor": 2 322 | } 323 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .history/ 2 | .dist/ 3 | 4 | 5 | ### JetBrains template 6 | 7 | # User-specific stuff 8 | .idea 9 | .idea/**/workspace.xml 10 | .idea/**/tasks.xml 11 | .idea/**/usage.statistics.xml 12 | .idea/**/dictionaries 13 | .idea/**/shelf 14 | 15 | # Generated files 16 | .idea/**/contentModel.xml 17 | 18 | # Sensitive or high-churn files 19 | .idea/**/dataSources/ 20 | .idea/**/dataSources.ids 21 | .idea/**/dataSources.local.xml 22 | .idea/**/sqlDataSources.xml 23 | .idea/**/dynamic.xml 24 | .idea/**/uiDesigner.xml 25 | .idea/**/dbnavigator.xml 26 | 27 | # Gradle 28 | .idea/**/gradle.xml 29 | .idea/**/libraries 30 | 31 | # Gradle and Maven with auto-import 32 | # When using Gradle or Maven with auto-import, you should exclude module files, 33 | # since they will be recreated, and may cause churn. Uncomment if using 34 | # auto-import. 35 | .idea/artifacts 36 | .idea/compiler.xml 37 | .idea/jarRepositories.xml 38 | .idea/modules.xml 39 | .idea/*.iml 40 | .idea/modules 41 | *.iml 42 | *.ipr 43 | 44 | # CMake 45 | cmake-build-*/ 46 | 47 | # Mongo Explorer plugin 48 | .idea/**/mongoSettings.xml 49 | 50 | # File-based project format 51 | *.iws 52 | 53 | # IntelliJ 54 | out/ 55 | 56 | # mpeltonen/sbt-idea plugin 57 | .idea_modules/ 58 | 59 | # JIRA plugin 60 | atlassian-ide-plugin.xml 61 | 62 | # Cursive Clojure plugin 63 | .idea/replstate.xml 64 | 65 | # Crashlytics plugin (for Android Studio and IntelliJ) 66 | com_crashlytics_export_strings.xml 67 | crashlytics.properties 68 | crashlytics-build.properties 69 | fabric.properties 70 | 71 | # Editor-based Rest Client 72 | .idea/httpRequests 73 | 74 | # Android studio 3.1+ serialized cache file 75 | .idea/caches/build_file_checksums.ser 76 | 77 | ### Eclipse template 78 | .metadata 79 | bin/ 80 | tmp/ 81 | *.tmp 82 | *.bak 83 | *.swp 84 | *~.nib 85 | local.properties 86 | .settings/ 87 | .loadpath 88 | .recommenders 89 | 90 | # External tool builders 91 | .externalToolBuilders/ 92 | 93 | # Locally stored "Eclipse launch configurations" 94 | *.launch 95 | 96 | # PyDev specific (Python IDE for Eclipse) 97 | *.pydevproject 98 | 99 | # CDT-specific (C/C++ Development Tooling) 100 | .cproject 101 | 102 | # CDT- autotools 103 | .autotools 104 | 105 | # Java annotation processor (APT) 106 | .factorypath 107 | 108 | # PDT-specific (PHP Development Tools) 109 | .buildpath 110 | 111 | # sbteclipse plugin 112 | .target 113 | 114 | # Tern plugin 115 | .tern-project 116 | 117 | # TeXlipse plugin 118 | .texlipse 119 | 120 | # STS (Spring Tool Suite) 121 | .springBeans 122 | 123 | # Code Recommenders 124 | .recommenders/ 125 | 126 | # Annotation Processing 127 | .apt_generated/ 128 | .apt_generated_test/ 129 | 130 | # Scala IDE specific (Scala & Java development for Eclipse) 131 | .cache-main 132 | .scala_dependencies 133 | .worksheet 134 | 135 | # Uncomment this line if you wish to ignore the project description file. 136 | # Typically, this file would be tracked if it contains build/dependency configurations: 137 | #.project 138 | 139 | ### Python template 140 | # Byte-compiled / optimized / DLL files 141 | __pycache__/ 142 | *.py[cod] 143 | *$py.class 144 | 145 | # C extensions 146 | *.so 147 | 148 | # Distribution / packaging 149 | .Python 150 | build/ 151 | develop-eggs/ 152 | dist/ 153 | downloads/ 154 | eggs/ 155 | .eggs/ 156 | lib/ 157 | lib64/ 158 | parts/ 159 | sdist/ 160 | var/ 161 | wheels/ 162 | share/python-wheels/ 163 | *.egg-info/ 164 | .installed.cfg 165 | *.egg 166 | MANIFEST 167 | 168 | # PyInstaller 169 | # Usually these files are written by a python script from a template 170 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 171 | *.manifest 172 | *.spec 173 | 174 | # Installer logs 175 | pip-log.txt 176 | pip-delete-this-directory.txt 177 | 178 | # Unit test / coverage reports 179 | htmlcov/ 180 | .tox/ 181 | .nox/ 182 | .coverage 183 | .coverage.* 184 | .cache 185 | nosetests.xml 186 | coverage.xml 187 | *.cover 188 | *.py,cover 189 | .hypothesis/ 190 | .pytest_cache/ 191 | cover/ 192 | 193 | # Translations 194 | *.mo 195 | *.pot 196 | 197 | # Django stuff: 198 | *.log 199 | local_settings.py 200 | db.sqlite3 201 | db.sqlite3-journal 202 | 203 | # Flask stuff: 204 | instance/ 205 | .webassets-cache 206 | 207 | # Scrapy stuff: 208 | .scrapy 209 | 210 | # Sphinx documentation 211 | docs/_build/ 212 | 213 | # PyBuilder 214 | .pybuilder/ 215 | target/ 216 | 217 | # Jupyter Notebook 218 | .ipynb_checkpoints 219 | 220 | # IPython 221 | profile_default/ 222 | ipython_config.py 223 | 224 | # pyenv 225 | # For a library or package, you might want to ignore these files since the code is 226 | # intended to run in multiple environments; otherwise, check them in: 227 | # .python-version 228 | 229 | # pipenv 230 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 231 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 232 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 233 | # install all needed dependencies. 234 | #Pipfile.lock 235 | 236 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 237 | __pypackages__/ 238 | 239 | # Celery stuff 240 | celerybeat-schedule 241 | celerybeat.pid 242 | 243 | # SageMath parsed files 244 | *.sage.py 245 | 246 | # Environments 247 | .env 248 | .venv 249 | env/ 250 | venv/ 251 | ENV/ 252 | env.bak/ 253 | venv.bak/ 254 | 255 | # Spyder project settings 256 | .spyderproject 257 | .spyproject 258 | 259 | # Rope project settings 260 | .ropeproject 261 | 262 | # mkdocs documentation 263 | /site 264 | 265 | # mypy 266 | .mypy_cache/ 267 | .dmypy.json 268 | dmypy.json 269 | 270 | # Pyre type checker 271 | .pyre/ 272 | 273 | # pytype static type analyzer 274 | .pytype/ 275 | 276 | # Cython debug symbols 277 | cython_debug/ 278 | 279 | ### VisualStudio template 280 | ## Ignore Visual Studio temporary files, build results, and 281 | ## files generated by popular Visual Studio add-ons. 282 | 283 | 284 | # User-specific files 285 | *.rsuser 286 | *.suo 287 | *.user 288 | *.userosscache 289 | *.sln.docstates 290 | 291 | # User-specific files (MonoDevelop/Xamarin Studio) 292 | *.userprefs 293 | 294 | # Mono auto generated files 295 | mono_crash.* 296 | 297 | # Build results 298 | [Dd]ebug/ 299 | [Dd]ebugPublic/ 300 | [Rr]elease/ 301 | [Rr]eleases/ 302 | x64/ 303 | x86/ 304 | [Ww][Ii][Nn]32/ 305 | [Aa][Rr][Mm]/ 306 | [Aa][Rr][Mm]64/ 307 | bld/ 308 | [Bb]in/ 309 | [Oo]bj/ 310 | [Ll]og/ 311 | [Ll]ogs/ 312 | 313 | # Visual Studio 2015/2017 cache/options directory 314 | .vs/ 315 | # Uncomment if you have tasks that create the project's static files in wwwroot 316 | #wwwroot/ 317 | 318 | # Visual Studio 2017 auto generated files 319 | Generated\ Files/ 320 | 321 | # MSTest test Results 322 | [Tt]est[Rr]esult*/ 323 | [Bb]uild[Ll]og.* 324 | 325 | # NUnit 326 | *.VisualState.xml 327 | TestResult.xml 328 | nunit-*.xml 329 | 330 | # Build Results of an ATL Project 331 | [Dd]ebugPS/ 332 | [Rr]eleasePS/ 333 | dlldata.c 334 | 335 | # Benchmark Results 336 | BenchmarkDotNet.Artifacts/ 337 | 338 | # .NET Core 339 | project.lock.json 340 | project.fragment.lock.json 341 | artifacts/ 342 | 343 | # ASP.NET Scaffolding 344 | ScaffoldingReadMe.txt 345 | 346 | # StyleCop 347 | StyleCopReport.xml 348 | 349 | # Files built by Visual Studio 350 | *_i.c 351 | *_p.c 352 | *_h.h 353 | *.ilk 354 | *.meta 355 | *.obj 356 | *.iobj 357 | *.pch 358 | *.pdb 359 | *.ipdb 360 | *.pgc 361 | *.pgd 362 | *.rsp 363 | *.sbr 364 | *.tlb 365 | *.tli 366 | *.tlh 367 | *.tmp_proj 368 | *_wpftmp.csproj 369 | *.vspscc 370 | *.vssscc 371 | .builds 372 | *.pidb 373 | *.svclog 374 | *.scc 375 | 376 | # Chutzpah Test files 377 | _Chutzpah* 378 | 379 | # Visual C++ cache files 380 | ipch/ 381 | *.aps 382 | *.ncb 383 | *.opendb 384 | *.opensdf 385 | *.sdf 386 | *.cachefile 387 | *.VC.db 388 | *.VC.VC.opendb 389 | 390 | # Visual Studio profiler 391 | *.psess 392 | *.vsp 393 | *.vspx 394 | *.sap 395 | 396 | # Visual Studio Trace Files 397 | *.e2e 398 | 399 | # TFS 2012 Local Workspace 400 | $tf/ 401 | 402 | # Guidance Automation Toolkit 403 | *.gpState 404 | 405 | # ReSharper is a .NET coding add-in 406 | _ReSharper*/ 407 | *.[Rr]e[Ss]harper 408 | *.DotSettings.user 409 | 410 | # TeamCity is a build add-in 411 | _TeamCity* 412 | 413 | # DotCover is a Code Coverage Tool 414 | *.dotCover 415 | 416 | # AxoCover is a Code Coverage Tool 417 | .axoCover/* 418 | !.axoCover/settings.json 419 | 420 | # Coverlet is a free, cross platform Code Coverage Tool 421 | coverage*.json 422 | coverage*.xml 423 | coverage*.info 424 | 425 | # Visual Studio code coverage results 426 | *.coverage 427 | *.coveragexml 428 | 429 | # NCrunch 430 | _NCrunch_* 431 | .*crunch*.local.xml 432 | nCrunchTemp_* 433 | 434 | # MightyMoose 435 | *.mm.* 436 | AutoTest.Net/ 437 | 438 | # Web workbench (sass) 439 | .sass-cache/ 440 | 441 | # Installshield output folder 442 | [Ee]xpress/ 443 | 444 | # DocProject is a documentation generator add-in 445 | DocProject/buildhelp/ 446 | DocProject/Help/*.HxT 447 | DocProject/Help/*.HxC 448 | DocProject/Help/*.hhc 449 | DocProject/Help/*.hhk 450 | DocProject/Help/*.hhp 451 | DocProject/Help/Html2 452 | DocProject/Help/html 453 | 454 | # Click-Once directory 455 | publish/ 456 | 457 | # Publish Web Output 458 | *.[Pp]ublish.xml 459 | *.azurePubxml 460 | # Note: Comment the next line if you want to checkin your web deploy settings, 461 | # but database connection strings (with potential passwords) will be unencrypted 462 | *.pubxml 463 | *.publishproj 464 | 465 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 466 | # checkin your Azure Web App publish settings, but sensitive information contained 467 | # in these scripts will be unencrypted 468 | PublishScripts/ 469 | 470 | # NuGet Packages 471 | *.nupkg 472 | # NuGet Symbol Packages 473 | *.snupkg 474 | # The packages folder can be ignored because of Package Restore 475 | **/[Pp]ackages/* 476 | # except build/, which is used as an MSBuild target. 477 | !**/[Pp]ackages/build/ 478 | # Uncomment if necessary however generally it will be regenerated when needed 479 | #!**/[Pp]ackages/repositories.config 480 | # NuGet v3's project.json files produces more ignorable files 481 | *.nuget.props 482 | *.nuget.targets 483 | 484 | # Microsoft Azure Build Output 485 | csx/ 486 | *.build.csdef 487 | 488 | # Microsoft Azure Emulator 489 | ecf/ 490 | rcf/ 491 | 492 | # Windows Store app package directories and files 493 | AppPackages/ 494 | BundleArtifacts/ 495 | Package.StoreAssociation.xml 496 | _pkginfo.txt 497 | *.appx 498 | *.appxbundle 499 | *.appxupload 500 | 501 | # Visual Studio cache files 502 | # files ending in .cache can be ignored 503 | *.[Cc]ache 504 | # but keep track of directories ending in .cache 505 | !?*.[Cc]ache/ 506 | 507 | # Others 508 | ClientBin/ 509 | ~$* 510 | *~ 511 | *.dbmdl 512 | *.dbproj.schemaview 513 | *.jfm 514 | *.pfx 515 | *.publishsettings 516 | orleans.codegen.cs 517 | 518 | 519 | # RIA/Silverlight projects 520 | Generated_Code/ 521 | 522 | # Backup & report files from converting an old project file 523 | # to a newer Visual Studio version. Backup files are not needed, 524 | # because we have git ;-) 525 | _UpgradeReport_Files/ 526 | Backup*/ 527 | UpgradeLog*.XML 528 | UpgradeLog*.htm 529 | ServiceFabricBackup/ 530 | *.rptproj.bak 531 | 532 | # SQL Server files 533 | *.mdf 534 | *.ldf 535 | *.ndf 536 | 537 | # Business Intelligence projects 538 | *.rdl.data 539 | *.bim.layout 540 | *.bim_*.settings 541 | *.rptproj.rsuser 542 | *- [Bb]ackup.rdl 543 | *- [Bb]ackup ([0-9]).rdl 544 | *- [Bb]ackup ([0-9][0-9]).rdl 545 | 546 | # Microsoft Fakes 547 | FakesAssemblies/ 548 | 549 | # GhostDoc plugin setting file 550 | *.GhostDoc.xml 551 | 552 | # Node.js Tools for Visual Studio 553 | .ntvs_analysis.dat 554 | node_modules/ 555 | 556 | # Visual Studio 6 build log 557 | *.plg 558 | 559 | # Visual Studio 6 workspace options file 560 | *.opt 561 | 562 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 563 | *.vbw 564 | 565 | # Visual Studio LightSwitch build output 566 | **/*.HTMLClient/GeneratedArtifacts 567 | **/*.DesktopClient/GeneratedArtifacts 568 | **/*.DesktopClient/ModelManifest.xml 569 | **/*.Server/GeneratedArtifacts 570 | **/*.Server/ModelManifest.xml 571 | _Pvt_Extensions 572 | 573 | # Paket dependency manager 574 | .paket/paket.exe 575 | paket-files/ 576 | 577 | # FAKE - F# Make 578 | .fake/ 579 | 580 | # CodeRush personal settings 581 | .cr/personal 582 | 583 | # Python Tools for Visual Studio (PTVS) 584 | *.pyc 585 | 586 | # Cake - Uncomment if you are using it 587 | # tools/** 588 | # !tools/packages.config 589 | 590 | # Tabs Studio 591 | *.tss 592 | 593 | # Telerik's JustMock configuration file 594 | *.jmconfig 595 | 596 | # BizTalk build output 597 | *.btp.cs 598 | *.btm.cs 599 | *.odx.cs 600 | *.xsd.cs 601 | 602 | # OpenCover UI analysis results 603 | OpenCover/ 604 | 605 | # Azure Stream Analytics local run output 606 | ASALocalRun/ 607 | 608 | # MSBuild Binary and Structured Log 609 | *.binlog 610 | 611 | # NVidia Nsight GPU debugger configuration file 612 | *.nvuser 613 | 614 | # MFractors (Xamarin productivity tool) working folder 615 | .mfractor/ 616 | 617 | # Local History for Visual Studio 618 | .localhistory/ 619 | 620 | # BeatPulse healthcheck temp database 621 | healthchecksdb 622 | 623 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 624 | MigrationBackup/ 625 | 626 | # Ionide (cross platform F# VS Code tools) working folder 627 | .ionide/ 628 | 629 | # Fody - auto-generated XML schema 630 | FodyWeavers.xsd 631 | 632 | ### macOS template 633 | # General 634 | .DS_Store 635 | .AppleDouble 636 | .LSOverride 637 | 638 | # Icon must end with two \r 639 | Icon 640 | 641 | # Thumbnails 642 | ._* 643 | 644 | # Files that might appear in the root of a volume 645 | .DocumentRevisions-V100 646 | .fseventsd 647 | .Spotlight-V100 648 | .TemporaryItems 649 | .Trashes 650 | .VolumeIcon.icns 651 | .com.apple.timemachine.donotpresent 652 | 653 | # Directories potentially created on remote AFP share 654 | .AppleDB 655 | .AppleDesktop 656 | Network Trash Folder 657 | Temporary Items 658 | .apdisk 659 | 660 | ### VirtualEnv template 661 | 662 | .Python 663 | [Bb]in 664 | [Ii]nclude 665 | [Ll]ib 666 | [Ll]ib64 667 | [Ll]ocal 668 | [Ss]cripts 669 | pyvenv.cfg 670 | pip-selfcheck.json 671 | 672 | ### JupyterNotebooks template 673 | 674 | 675 | */.ipynb_checkpoints/* 676 | 677 | 678 | -------------------------------------------------------------------------------- /machine_learning/Decision tree/Decision_Tree.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "#!/usr/bin/env python\n", 10 | "# coding: utf-8\n", 11 | "\n", 12 | "# In[1]:\n", 13 | "\n", 14 | "\n", 15 | "import numpy as np\n", 16 | "\n", 17 | "\n", 18 | "# In[ ]:\n", 19 | "\n", 20 | "\n", 21 | "class D_TREE :\n", 22 | " \n", 23 | " def fit(self,Xin):\n", 24 | " #fitting the values\n", 25 | " self.X=Xin #training_dataset_\n", 26 | " self.my_tree=self.tree(Xin) #calls tree() function to create a tree based on the dataset provided\n", 27 | " \n", 28 | " \n", 29 | " \n", 30 | " def label_count(self,t):\n", 31 | " #count the unique labels\n", 32 | " count = {} #a dictionary that will store the no of times every label has occurred\n", 33 | " for i in range(len(t)):\n", 34 | " lbl = t[i][-1] #The last field or column in t actually contains the labels \n", 35 | " if lbl not in count:\n", 36 | " count[lbl] = 0 #If the label is not present previously,initialize it with zero\n", 37 | " count[lbl]+=1 #Everytime a particular label is encountered its count is increased by 1 \n", 38 | " return count\n", 39 | "\n", 40 | " \n", 41 | " \n", 42 | " \n", 43 | " class Question :\n", 44 | " #stores the question and matches the question \n", 45 | " def __init__(self,col,value):\n", 46 | " self.col = col #The column to which the question belongs to\n", 47 | " self.question = value #the particualr cell in the column which is treated as question\n", 48 | " \n", 49 | " \n", 50 | " def is_digit_or_char(self,n):\n", 51 | " #checks whether a particular value is a number or not\n", 52 | " return isinstance(n,int) or isinstance(n,float)\n", 53 | " \n", 54 | " def check(self,row):\n", 55 | " value=row[self.col] #the value to be tested with the question\n", 56 | " if(self.is_digit_or_char(self.question)):\n", 57 | " return value>=self.question #if the value is numeric in nature check whether it is greater or equal to question\n", 58 | " else :\n", 59 | " return value==self.question #if the value is a character or string check whether it is equal to the question or not\n", 60 | " \n", 61 | " \n", 62 | " \n", 63 | "\n", 64 | " def gini(self,t):\n", 65 | " #Calculates the gini score\n", 66 | " label = np.unique(t) #No of unique labels\n", 67 | " impurity = 1\n", 68 | " \n", 69 | " for i in range(len(label)):\n", 70 | " impurity -= (np.sum(t[:,-1]==label[i])/t.shape[0])**2 #formula for calculating impurity based on probability\n", 71 | " \n", 72 | " return impurity\n", 73 | "\n", 74 | " \n", 75 | " \n", 76 | "\n", 77 | " def information_gain(self,l,r,current_uncertainity):\n", 78 | " #Information gain is calculated\n", 79 | " p = len (l) / float ( len(l) + len(r) ) \n", 80 | " return current_uncertainity - p*self.gini(l) - (1-p)*self.gini(r)\n", 81 | " \n", 82 | " \n", 83 | " \n", 84 | " \n", 85 | " def best_split(self,t):\n", 86 | " #Selects the best question and split based on the gini score\n", 87 | " maxm=0\n", 88 | " best_question = None\n", 89 | " tr_row=[]\n", 90 | " fl_row=[]\n", 91 | " \n", 92 | " for i in range(t.shape[1]-1):\n", 93 | " y=np.unique(t[:,i]) #no of unique labels in a particular column\n", 94 | " m=y.shape[0] #no of examples\n", 95 | " for j in range(m):\n", 96 | " question = self.Question(i,y[j]) #each unique label is considered a question one at a time\n", 97 | " tr_row , fl_row = self.split(t,question)#splits the rows based on the question\n", 98 | " if(len(fl_row)==0 or len(tr_row)==0):\n", 99 | " continue #if any of the branch has zero rows,the question is skipped\n", 100 | " \n", 101 | " info_gain= self.information_gain(tr_row,fl_row,self.gini(t)) #information gain is calculated\n", 102 | " \n", 103 | " if(info_gain>maxm):\n", 104 | " \"\"\"best question\n", 105 | " with maximum informaion\n", 106 | " gain is selected\"\"\"\n", 107 | " maxm = info_gain \n", 108 | " best_question = question\n", 109 | " \n", 110 | " return maxm,best_question\n", 111 | "\n", 112 | " \n", 113 | " \n", 114 | " \n", 115 | " def split(self,t,question)\n", 116 | " #Splits the dataset based on the best question\n", 117 | " tr_row=[] \n", 118 | " fl_row=[]\n", 119 | " for k in range(t.shape[0]):\n", 120 | " \"\"\"checks every row of the dataset \n", 121 | " with the queston & if it matches,\n", 122 | " it is appended to the true rows\n", 123 | " else to the false rows\"\"\"\n", 124 | " if question.check(t[k]):\n", 125 | " tr_row=np.append(tr_row,t[k]) \n", 126 | " else:\n", 127 | " fl_row=np.append(fl_row,t[k])\n", 128 | " \n", 129 | " tr_row = np.reshape(tr_row,(len(tr_row)//t.shape[1],t.shape[1])) #just reshapes the one-d matrix into a readable 2d matrix\n", 130 | " fl_row = np.reshape(fl_row,(len(fl_row)//t.shape[1],t.shape[1])) #just reshapes the one-d matrix into a readable 2d matrix\n", 131 | " \n", 132 | " return tr_row,fl_row\n", 133 | " \n", 134 | " \n", 135 | " \n", 136 | " \n", 137 | " \n", 138 | " class Decision_Node:\n", 139 | " #Stores the different question,true branch and false branch for all parts of the tree\n", 140 | " def __init__(self,question,true_branch,false_branch):\n", 141 | " self.question = question \n", 142 | " self.true_branch = true_branch\n", 143 | " self.false_branch = false_branch\n", 144 | "\n", 145 | "\n", 146 | " \n", 147 | " \n", 148 | " class Leaf:\n", 149 | " #the terminal of a tree is the leaf\n", 150 | " def __init__(self,t):\n", 151 | " self.predictions = D_TREE().label_count(t) \n", 152 | "\n", 153 | " \n", 154 | " \n", 155 | " \n", 156 | "\n", 157 | " def tree(self,t):\n", 158 | " \"\"\"the most important part of the entire algorithm\n", 159 | " this is where the tree is constructed from the root \n", 160 | " to the leaves\"\"\"\n", 161 | " gain,question = self.best_split(t) #best question with maximum gain is selected\n", 162 | " if(gain==0):\n", 163 | " return self.Leaf(t) #no gain indicates that leaf is reached\n", 164 | " \n", 165 | " \"\"\"if the control has reached this far,it means\n", 166 | " there is useful gain and teh datset can be subdivided\n", 167 | " or branched into true rows and false rows\"\"\"\n", 168 | " true_rows , false_rows = self.split(t,question) \n", 169 | " true_node = self.tree(true_rows) #A recursion is carried out till all the true rows are found out\n", 170 | " false_node= self.tree(false_rows) #after true rows,the false rows are assigned to the node in a reverse fashion\n", 171 | " \n", 172 | " return self.Decision_Node(question,true_node,false_node) \n", 173 | " \n", 174 | " \n", 175 | " \n", 176 | " \n", 177 | " \n", 178 | " def check_testing_data(self,test,node):\n", 179 | " #checks the testing data by recursively calling itself\n", 180 | " if isinstance(node,self.Leaf):\n", 181 | " return node.predictions #when the leaf is reached prediction is made\n", 182 | " \n", 183 | " \"\"\"a row is made to travel in the tree,till it reaches a leaf,\n", 184 | " it is checked with all decision nodes, and accordingly\n", 185 | " it travels along true branch or false branch,till\n", 186 | " it reaches a leaf\"\"\"\n", 187 | " if(node.question.check(test)):\n", 188 | " return self.check_testing_data(test,node.true_branch)\n", 189 | " else:\n", 190 | " return self.check_testing_data(test,node.false_branch)\n", 191 | " \n", 192 | " \n", 193 | " \n", 194 | " \n", 195 | " \n", 196 | " def print_leaf(self,LEAF):\n", 197 | " #prints a leaf\n", 198 | " p={}\n", 199 | " for i in LEAF.keys():\n", 200 | " p[i] = str(100*LEAF[i]/float(sum(LEAF.values()))) + \"%\"\n", 201 | " \n", 202 | " print(p)\n", 203 | " \n", 204 | " \n", 205 | " \n", 206 | " \n", 207 | " \n", 208 | " def pred(self,X_test):\n", 209 | " #predicts values for test data\n", 210 | " y_pred=[0]*X_test.shape[0]\n", 211 | " for i in range(X_test.shape[0]):\n", 212 | " \"\"\"when a row reaches a particular leaf\n", 213 | " it is assigned the label which\n", 214 | " appears maximum in the leaf\"\"\"\n", 215 | " r= self.check_testing_data(X_test[i],self.my_tree) #deals with one row at a time\n", 216 | " y_pred[i] = max(r.keys(), key=(lambda k: r[k])) \n", 217 | " return y_pred\n", 218 | " \n", 219 | " \n", 220 | " \n", 221 | " \n", 222 | " \n", 223 | " def accuracy(self,y_test,y_pred):\n", 224 | " #Calculate the accuracy of the model\n", 225 | " return np.mean(y_test==y_pred)*100\n", 226 | " \n", 227 | " \n", 228 | " \n", 229 | "\n" 230 | ] 231 | }, 232 | { 233 | "cell_type": "code", 234 | "execution_count": 2, 235 | "metadata": {}, 236 | "outputs": [], 237 | "source": [ 238 | "#Importing libraries\n", 239 | "import numpy as np\n", 240 | "\n" 241 | ] 242 | }, 243 | { 244 | "cell_type": "code", 245 | "execution_count": 3, 246 | "metadata": {}, 247 | "outputs": [], 248 | "source": [ 249 | "#Importing data set\n", 250 | "from sklearn.datasets import load_breast_cancer\n", 251 | "data = load_breast_cancer()\n", 252 | "\n", 253 | "\n", 254 | "X=data['data']\n", 255 | "Y=data['target']\n", 256 | "Y=np.reshape(Y,(Y.shape[0],1))\n", 257 | "X=X[:,:5]\n", 258 | "X=np.append(X,Y,axis=1)" 259 | ] 260 | }, 261 | { 262 | "cell_type": "code", 263 | "execution_count": 3, 264 | "metadata": {}, 265 | "outputs": [], 266 | "source": [ 267 | "#Separating training set and test set\n", 268 | "sz = len(X)//4\n", 269 | "X_test=X[:sz,:]\n", 270 | "X_train=X[sz:,:] #X_train already contains Y_train as its last column so no need to define it separately\n", 271 | "Y_test=X_test[:,-1]\n", 272 | "n=X.shape[1]" 273 | ] 274 | }, 275 | { 276 | "cell_type": "code", 277 | "execution_count": 4, 278 | "metadata": {}, 279 | "outputs": [], 280 | "source": [ 281 | "#Training the algorithm\n", 282 | "train=D_TREE()\n", 283 | "train.fit(X_train)" 284 | ] 285 | }, 286 | { 287 | "cell_type": "code", 288 | "execution_count": 22, 289 | "metadata": {}, 290 | "outputs": [], 291 | "source": [ 292 | "#Predictions on test data\n", 293 | "y_pred=train.pred(X_test)" 294 | ] 295 | }, 296 | { 297 | "cell_type": "code", 298 | "execution_count": 21, 299 | "metadata": {}, 300 | "outputs": [ 301 | { 302 | "name": "stdout", 303 | "output_type": "stream", 304 | "text": [ 305 | "{1.0: '100.0%'}\n", 306 | "{0.0: '100.0%'}\n", 307 | "{0.0: '100.0%'}\n", 308 | "{1.0: '100.0%'}\n", 309 | "{0.0: '100.0%'}\n", 310 | "{1.0: '100.0%'}\n", 311 | "{0.0: '100.0%'}\n", 312 | "{0.0: '100.0%'}\n", 313 | "{0.0: '100.0%'}\n", 314 | "{1.0: '100.0%'}\n" 315 | ] 316 | } 317 | ], 318 | "source": [ 319 | "#A few predictions\n", 320 | "for i in range(10):\n", 321 | " train.print_leaf(train.check_testing_data(X_test[i],train.my_tree))" 322 | ] 323 | }, 324 | { 325 | "cell_type": "code", 326 | "execution_count": 18, 327 | "metadata": {}, 328 | "outputs": [ 329 | { 330 | "name": "stdout", 331 | "output_type": "stream", 332 | "text": [ 333 | "Accuracy of my model : 80.28169014084507 %\n" 334 | ] 335 | } 336 | ], 337 | "source": [ 338 | "print(\"Accuracy of my model : \",train.accuracy(Y_test,y_pred),\"%\")" 339 | ] 340 | }, 341 | { 342 | "cell_type": "code", 343 | "execution_count": 17, 344 | "metadata": {}, 345 | "outputs": [ 346 | { 347 | "name": "stdout", 348 | "output_type": "stream", 349 | "text": [ 350 | "accuracy of sklearn model = 80.28169014084507 %\n" 351 | ] 352 | } 353 | ], 354 | "source": [ 355 | "#calculating accuracy using sklearn model\n", 356 | "from sklearn import tree\n", 357 | "clf = tree.DecisionTreeClassifier()\n", 358 | "clf = clf.fit(X_train[:,0:n-1], Y_train)\n", 359 | "y_pred=clf.predict(X_test[:,:n-1])\n", 360 | "error=(y_pred==Y_test.flatten())\n", 361 | "print(\"accuracy of sklearn model = \",np.mean(error)*100,\"%\")" 362 | ] 363 | } 364 | ], 365 | "metadata": { 366 | "kernelspec": { 367 | "display_name": "Python 3", 368 | "language": "python", 369 | "name": "python3" 370 | }, 371 | "language_info": { 372 | "codemirror_mode": { 373 | "name": "ipython", 374 | "version": 3 375 | }, 376 | "file_extension": ".py", 377 | "mimetype": "text/x-python", 378 | "name": "python", 379 | "nbconvert_exporter": "python", 380 | "pygments_lexer": "ipython3", 381 | "version": "3.8.3" 382 | } 383 | }, 384 | "nbformat": 4, 385 | "nbformat_minor": 2 386 | } 387 | -------------------------------------------------------------------------------- /neural_network/CNN-using keras.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# A Simple Convolutional Neural Network (using Keras)\n", 8 | "\n", 9 | ">## Explaining the code \n", 10 | "> >#### Original Articles : \n", 11 | "* [An Intuitive Guide to CNN](https://www.freecodecamp.org/news/an-intuitive-guide-to-convolutional-neural-networks-260c2de0a050/)\n", 12 | "* [CNN : How to Build one in Keras & PyTorch](https://missinglink.ai/guides/neural-network-concepts/convolutional-neural-network-build-one-keras-pytorch/)" 13 | ] 14 | }, 15 | { 16 | "cell_type": "markdown", 17 | "metadata": {}, 18 | "source": [ 19 | "## Import the required libraries:\n", 20 | "1. **Numpy**:\n", 21 | "[NumPy](https://numpy.org) is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.\n", 22 | "\n", 23 | "1. **Keras**:[Keras](https://keras.io/) is a high-level neural networks API, written in Python.\n", 24 | " * Layers\n", 25 | " * **Activation** : This is a method that applies an activation function to the output.\n", 26 | " * **MaxPool2D** : This method creates a Pooling layer to implement Max Pooling operation.\n", 27 | " * **Conv2D** : This method is used to create a convolution layer.\n", 28 | " * **Flatten** : This method flattens (converting into 1-Dimension) the input without affecting the batch size.\n", 29 | " * **Dense** : This method is used to create a fully connected neural network. \n", 30 | " * Models\n", 31 | " * **Sequential**: The [Sequential model](https://keras.io/getting-started/sequential-model-guide/) is a linear stack of layers. We can create a Sequential model by passing a list of layer instances to the constructor or just by simply adding layers via the **.add()** method." 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": null, 37 | "metadata": {}, 38 | "outputs": [], 39 | "source": [ 40 | "import numpy as np\n", 41 | "from keras.layers import Conv2D, Activation, MaxPool2D, Flatten, Dense\n", 42 | "from keras.models import Sequential" 43 | ] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "metadata": {}, 48 | "source": [ 49 | "## The Convolutional Neural Network Architecture\n", 50 | "A typical Convolutional Neural Network consists of the following parts:\n", 51 | "\n", 52 | "### 1. The Hidden Layers/ Feature Extraction Part \n", 53 | "#### (The Covolution and Pooling Layer)\n", 54 | "In this part of the network a series of convolution and pooling operations takes place, which are used for detecting different features of the input image.\n", 55 | "\n", 56 | "* In the code snippet below, we initialise a **Sequential model**. At first we dont provide the constructor with the layers,but rather we go on adding the layers to the model by using the **.add()** fuction as can be seen from the following snippets of code. \n", 57 | "* This model lets us build a model by adding on one layer at a time." 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": null, 63 | "metadata": {}, 64 | "outputs": [], 65 | "source": [ 66 | "# Images fed into this model are 28 x 28 pixels with 3 channels\n", 67 | "img_shape = (28,28,1)\n", 68 | "# Set up the model\n", 69 | "model = Sequential()" 70 | ] 71 | }, 72 | { 73 | "cell_type": "markdown", 74 | "metadata": {}, 75 | "source": [ 76 | "* In the above line of codes,at first we define the **img_shape**.Here the **img_shape** denotes the input image shape to the model. It is important as the model needs to knpw what input shape it should expect to be fed.**Therefore the first layer in a Sequential model needs to always receive information about the input shape.**\n" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": null, 82 | "metadata": {}, 83 | "outputs": [], 84 | "source": [ 85 | "# Add convolutional layer with 3, 3 by 3 filters\n", 86 | "model.add(Conv2D(3,kernel_size=3,input_shape=img_shape))" 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "metadata": {}, 92 | "source": [ 93 | "### [Conv2D()](https://keras.io/layers/convolutional/)\n", 94 | "This function creates a 2D Convolution Layer. \n", 95 | "\n", 96 | "#### What does a Convolution Layer do?\n", 97 | "* A convolution layer scans a source image with a filter to extract features which may be important for classification. (The filter is also called the convolution kernel.)\n", 98 | "* The kernel also contains weights,which are tuned during the training of the model to achieve the most accurate predictions.\n", 99 | "* **Parameters** :\n", 100 | " * The first parameter (3 in this case), defines the number of neural nodes in each layer.\n", 101 | " * **kernel_size** defines the filter size—this is the area in square pixels the model will use to scan the image. Kernel size of 3 means the model looks at a square of 3×3 pixels at a time.\n", 102 | " * **input_shape** is the pixel size of the images.\n" 103 | ] 104 | }, 105 | { 106 | "cell_type": "markdown", 107 | "metadata": {}, 108 | "source": [ 109 | "### [MaxPool2D()](https://keras.io/layers/pooling/)\n", 110 | "This function creates a 2D Pooling Layer.\n", 111 | "\n", 112 | "#### What does a Pooling Layer do?\n", 113 | "\n", 114 | "* Its function is to **progressively reduce the spatial size of the representation to reduce the amount of parameters and computation in the network**, and hence to also **control overfitting**.\n", 115 | "* The Pooling Layer **operates independently on every depth slice of the input and resizes it spatially**, using the MAX operation.\n", 116 | "\n", 117 | "### Activation()\n", 118 | "The **Activation()** method is used to apply a specific activation function to the output of the Pooling layer.\n", 119 | "Here a **relu** activation function is added to the output layer." 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": null, 125 | "metadata": {}, 126 | "outputs": [], 127 | "source": [ 128 | "# Add relu activation to the layer \n", 129 | "model.add(Activation('relu'))\n", 130 | "#Pooling\n", 131 | "model.add(MaxPool2D(2))" 132 | ] 133 | }, 134 | { 135 | "cell_type": "markdown", 136 | "metadata": {}, 137 | "source": [ 138 | "### 2. Classification Layer\n", 139 | "After the convolution layer, there is classification layer that consists of fully connected layers, where neurons of one layer are connected with every activations from the previous layer.\n", 140 | "The thing about the fully connected layers is that it can only be fed **1-Dimensional** data. With the output data from the previous layer being **3-Dimensional** , it needs to the **flatten out**(converted into 1-Dimensional) before it can be fed into the Classification layer.\n", 141 | "\n", 142 | "For this a **Flatten** layer is added, which takes the output of the convolution layer and turns it into a format that can be used by the densely connected neural layer." 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": null, 148 | "metadata": {}, 149 | "outputs": [], 150 | "source": [ 151 | "#Fully connected layers\n", 152 | "# Use Flatten to convert 3D data to 1D\n", 153 | "model.add(Flatten())\n", 154 | "# Add dense layer with 10 neurons\n", 155 | "model.add(Dense(10))\n", 156 | "# we use the softmax activation function for our last layer\n", 157 | "model.add(Activation('softmax'))" 158 | ] 159 | }, 160 | { 161 | "cell_type": "markdown", 162 | "metadata": {}, 163 | "source": [ 164 | "### Dense()\n", 165 | "The final layer of the model is of type 'Dense', a densely-connected neural layer which will give the final classification/prediction.\n", 166 | "* The parameter(in this case 10), refers to **the number of output nodes.**\n", 167 | "* After that a **softmax** activation function is added to the output layer. It will take the output of the Dense layer and convert it into meaningful probabilities that will ultimately help in making the final prediction.\n" 168 | ] 169 | }, 170 | { 171 | "cell_type": "markdown", 172 | "metadata": {}, 173 | "source": [ 174 | "### Summary()\n", 175 | "Now just to get an overview of the model we have created ,we will use the **summary() function**. This will list out the details regarding the Layer,the Output shape and the number of parameters." 176 | ] 177 | }, 178 | { 179 | "cell_type": "code", 180 | "execution_count": null, 181 | "metadata": {}, 182 | "outputs": [], 183 | "source": [ 184 | "# give an overview of our model\n", 185 | "model.summary()" 186 | ] 187 | }, 188 | { 189 | "cell_type": "markdown", 190 | "metadata": {}, 191 | "source": [ 192 | "### Compilation:\n", 193 | "Before the training process, we have to put together a learning process in a particular form.This is done via compile(). It consists of 3 elements: \n", 194 | "* **An Optimiser** : string identifier of an existing optimizer/a call to an optimizer function\n", 195 | "* **A loss function** : string identifier of an existing loss function/a call to a loss function\n", 196 | "* **A list of metric** : string identifier of an existing metric or a call to metric function\n", 197 | " > * The metrics defines how the success of the model is evaluated.\n", 198 | " > * we will use the ‘accuracy’ metric to calculate an accuracy score on the testing/validation set of images." 199 | ] 200 | }, 201 | { 202 | "cell_type": "code", 203 | "execution_count": null, 204 | "metadata": {}, 205 | "outputs": [], 206 | "source": [ 207 | "model.compile(loss='categorical_crossentropy', optimizer = 'adam', metrics=['accuracy'])" 208 | ] 209 | }, 210 | { 211 | "cell_type": "markdown", 212 | "metadata": {}, 213 | "source": [ 214 | "Our model is complete.\n", 215 | "\n", 216 | "### The Training Phase:\n", 217 | "Now we will train the model.\n", 218 | "> We will use the MNIST dataset which is a benchmark deep learning dataset, containing 70,000 handwritten numbers from 0-9." 219 | ] 220 | }, 221 | { 222 | "cell_type": "code", 223 | "execution_count": null, 224 | "metadata": {}, 225 | "outputs": [], 226 | "source": [ 227 | "#importing the datasets\n", 228 | "from keras.datasets import mnist\n", 229 | "#loading the datasets\n", 230 | "(X_train, y_train), (X_test, y_test) = mnist.load_data()" 231 | ] 232 | }, 233 | { 234 | "cell_type": "code", 235 | "execution_count": null, 236 | "metadata": {}, 237 | "outputs": [], 238 | "source": [ 239 | "import matplotlib.pyplot as plt\n", 240 | "plt.imshow(X_train[0])\n", 241 | "print(X_train[0].shape)" 242 | ] 243 | }, 244 | { 245 | "cell_type": "markdown", 246 | "metadata": {}, 247 | "source": [ 248 | "#### In the code snippet below, reshaping is done.\n", 249 | "## But WHY?\n", 250 | "**Reshaping of the two sets of images, X_train and X_test is done so that their shape matches the shape expected by our CNN model.**" 251 | ] 252 | }, 253 | { 254 | "cell_type": "code", 255 | "execution_count": null, 256 | "metadata": {}, 257 | "outputs": [], 258 | "source": [ 259 | "X_train = X_train.reshape(60000,28,28,1)\n", 260 | "X_test = X_test.reshape(10000,28,28,1)" 261 | ] 262 | }, 263 | { 264 | "cell_type": "markdown", 265 | "metadata": {}, 266 | "source": [ 267 | "### One-hot encoding:\n", 268 | "In one-hot encoding we create a column for each classification category, with each column containing binary values indicating if the current image belongs to that category or not." 269 | ] 270 | }, 271 | { 272 | "cell_type": "code", 273 | "execution_count": null, 274 | "metadata": {}, 275 | "outputs": [], 276 | "source": [ 277 | "from keras.utils import to_categorical\n", 278 | "y_train = to_categorical(y_train)\n", 279 | "y_test = to_categorical(y_test)" 280 | ] 281 | }, 282 | { 283 | "cell_type": "markdown", 284 | "metadata": {}, 285 | "source": [ 286 | "### [fit( )](https://keras.io/models/sequential/)\n", 287 | ">* This method trains the model for a fixed number of epochs.\n", 288 | "* An epoch is an iteration over the entire x and y data provided. " 289 | ] 290 | }, 291 | { 292 | "cell_type": "code", 293 | "execution_count": null, 294 | "metadata": {}, 295 | "outputs": [], 296 | "source": [ 297 | "model.fit(X_train, y_train,validation_data=(X_test,y_test) ,epochs=10, verbose=2)" 298 | ] 299 | }, 300 | { 301 | "cell_type": "markdown", 302 | "metadata": {}, 303 | "source": [ 304 | "## Now its time to test our Model!!\n", 305 | "We can test our model against a/some specific inputs with the help of the **predict( )** function as done in the code snippet below.\n", 306 | "> * **Output Format**\n", 307 | " The output will consists of 10 probabilities, each representing the probability of being a particular digit from 0-9." 308 | ] 309 | }, 310 | { 311 | "cell_type": "code", 312 | "execution_count": null, 313 | "metadata": {}, 314 | "outputs": [], 315 | "source": [ 316 | "model.predict(X_test[:4])" 317 | ] 318 | }, 319 | { 320 | "cell_type": "markdown", 321 | "metadata": {}, 322 | "source": [ 323 | "### Improving the displaying of the results:\n", 324 | "> By directly displaying the predicted number instead of the probabilities.(i.e. just refining the output. )" 325 | ] 326 | }, 327 | { 328 | "cell_type": "code", 329 | "execution_count": null, 330 | "metadata": {}, 331 | "outputs": [], 332 | "source": [ 333 | "def return_Digit(x):\n", 334 | " a=np.where(x==max(x))\n", 335 | " print(int(a[0]))" 336 | ] 337 | }, 338 | { 339 | "cell_type": "code", 340 | "execution_count": null, 341 | "metadata": {}, 342 | "outputs": [], 343 | "source": [ 344 | "prediction=model.predict(X_test[:4])\n", 345 | "np.apply_along_axis(return_Digit,axis=1,arr=prediction)" 346 | ] 347 | }, 348 | { 349 | "cell_type": "markdown", 350 | "metadata": {}, 351 | "source": [ 352 | "### ---------------------------------------------------------------------------" 353 | ] 354 | } 355 | ], 356 | "metadata": { 357 | "kernelspec": { 358 | "display_name": "Python 3", 359 | "language": "python", 360 | "name": "python3" 361 | }, 362 | "language_info": { 363 | "codemirror_mode": { 364 | "name": "ipython", 365 | "version": 3 366 | }, 367 | "file_extension": ".py", 368 | "mimetype": "text/x-python", 369 | "name": "python", 370 | "nbconvert_exporter": "python", 371 | "pygments_lexer": "ipython3", 372 | "version": "3.7.4" 373 | } 374 | }, 375 | "nbformat": 4, 376 | "nbformat_minor": 2 377 | } 378 | -------------------------------------------------------------------------------- /machine_learning/Natural language processing/Movie_recommendation_Sentance_Embedding.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "Sentance_Embedding.ipynb", 7 | "provenance": [], 8 | "collapsed_sections": [] 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | } 14 | }, 15 | "cells": [ 16 | { 17 | "cell_type": "code", 18 | "metadata": { 19 | "id": "UN3QsCOJj3V4", 20 | "colab_type": "code", 21 | "colab": {} 22 | }, 23 | "source": [ 24 | "import nltk\n", 25 | "from nltk.stem.lancaster import LancasterStemmer\n", 26 | "\n", 27 | "stemmer = LancasterStemmer()\n", 28 | "\n", 29 | "# Import package\n", 30 | "\n", 31 | "import tensorflow as tf\n", 32 | "import json\n", 33 | "import tflearn\n", 34 | "import numpy as np \n", 35 | "import random\n", 36 | "import pickle" 37 | ], 38 | "execution_count": 0, 39 | "outputs": [] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "metadata": { 44 | "id": "PKQPFioif9Lw", 45 | "colab_type": "code", 46 | "colab": { 47 | "base_uri": "https://localhost:8080/", 48 | "height": 55 49 | }, 50 | "outputId": "8054baa6-36ad-4ec3-98e9-623703240098" 51 | }, 52 | "source": [ 53 | "with open('intents.json') as jsonFile:\n", 54 | " data = json.load(jsonFile)\n", 55 | "\n", 56 | "print(data['intents'])" 57 | ], 58 | "execution_count": 136, 59 | "outputs": [ 60 | { 61 | "output_type": "stream", 62 | "text": [ 63 | "[{'tag': 'greeting', 'patterns': ['Hi', 'How are you', 'Is anyone there?', 'Hello', 'Good day', 'Whats up'], 'responses': ['Hello!', 'Good to see you again!', 'Hi there, how can I help?'], 'context_set': ''}, {'tag': 'action and advanture', 'patterns': [' Elements of action/adventure (car chases, shootouts, explosions) and thriller', 'Combines action set-pieces with serious themes, character insight and/or emotional power', 'I like action movies', 'i like fighting movies', 'action sequences, such as fighting, stunts, car chases or explosions', 'Fighting is really awesome'], 'responses': ['I also like action movies :)', 'ohh nice', 'yeah action movie is awesome'], 'context_set': ''}, {'tag': 'comedy', 'patterns': ['These films are designed to make the audience laugh through amusement', 'very first silent movies were comedies, as slapstick comedy often relies on visual depictions', 'with many former stand-up comics transitioning to the film industry', 'satirical comedy-drama & the plot is', 'i like comedy movies', 'comedy is really love to watch', 'laughing is the best medicine', 'i love everyday'], 'responses': ['I also like comedy movies :)', 'ohh nice', 'yeah laughing is better for health'], 'context_set': ''}, {'tag': 'Horror', 'patterns': ['A horror film is a film that seeks to elicit fear for entertainment purposes', 'horror has existed as a film genre for more than a century', 'i like Horror movie', 'Horror movies are so thrilled', 'an intense feeling of fear, shock, or disgust.', 'I like intense and fear movies', 'Horror films effectively center on the dark side of life', 'i like dark movies', 'Horror Films are unsettling films designed to frighten and panic,monster'], 'responses': ['ohh i think you like science fiction kind of horror', 'Ohhh i also dark movie', 'Hmm horror movies are so thriilled'], 'context_set': ''}, {'tag': 'Romance', 'patterns': ['romance movies are romantic love stories', 'romance movie focus on passion, emotion, and the affectionate romantic involvement', 'Romance movie strong and pure love and romance the main plot focus', 'i love romantic and love movies', 'romantic love, obsessive love, sentimental love, spiritual love, forbidden love/romance, platonic love, sexual and passionate love, sacrificial love, explosive and destructive love', 'i want to watch Deep and true romantic love between two people', 'i like '], 'responses': ['Romantic movie are just awesome', 'Yeahh i like true love movies', 'Romance is everywhere :)', \"I recommond don't watch with family :(\"], 'context_set': ''}, {'tag': 'Accept', 'patterns': ['ohh really', 'nice', 'ohhh'], 'responses': ['yess', 'yeah', 'yepp', \"yeah that's true\"], 'context_set': ''}]\n" 64 | ], 65 | "name": "stdout" 66 | } 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "metadata": { 72 | "id": "tOnAzP9SgEZJ", 73 | "colab_type": "code", 74 | "colab": {} 75 | }, 76 | "source": [ 77 | "try:\n", 78 | " with open('data.pickle',\"rb\") as f:\n", 79 | " words, labels, training, output = pickle.load(f)\n", 80 | "except:\n", 81 | " words = []\n", 82 | " labels = []\n", 83 | " # doc_x contain pattern of words\n", 84 | " docs_x = [] \n", 85 | " # doc_y contain pattern of specific tag\n", 86 | " docs_y = []\n", 87 | "\n", 88 | " for intent in data[\"intents\"]:\n", 89 | " for pattern in intent[\"patterns\"]:\n", 90 | " # it's consider only root words by removing unnessary stuff from the sentance\n", 91 | " # Use Tokenization : that will help to grab the perticular word from the sentance\n", 92 | " # it will return the list which contain all the words in it \n", 93 | " # nltk.download('punkt')\n", 94 | "\n", 95 | " wrds = nltk.word_tokenize(pattern)\n", 96 | " words.extend(wrds)\n", 97 | "\n", 98 | " # append pattern of words\n", 99 | " docs_x.append(wrds)\n", 100 | " docs_y.append(intent[\"tag\"])\n", 101 | "\n", 102 | " # append tag in labels list\n", 103 | " if intent[\"tag\"] not in labels:\n", 104 | " labels.append(intent[\"tag\"])\n", 105 | "\n", 106 | " print(labels)\n", 107 | " print(docs_x)\n", 108 | " print(docs_y)\n", 109 | " # convert all the words into lowercase so that uppercase is not different then lowecase word \n", 110 | " unvalid_data = ['?', ')', '(', ',', '.', '&']\n", 111 | " words = [stemmer.stem(w.lower()) for w in words if w not in unvalid_data]\n", 112 | " print(words)\n", 113 | " \n", 114 | " # remove duplicate and sort\n", 115 | " words = sorted(list(set(words)))\n", 116 | " print(words)\n", 117 | " \n", 118 | " # sort labels\n", 119 | " labels = sorted(labels)\n", 120 | " print(labels)\n", 121 | "\n", 122 | " # we create a bag of words that will represent a any given pattern\n", 123 | " # we create 1 hot encoding which will contain the 1 or 0 based on the word exist or not\n", 124 | " # in the sentance \n", 125 | "\n", 126 | " # As neural network only understand numeric value rather then a word that's we need to convert them into numeric encoding\n", 127 | "\n", 128 | " # As bag of words represent by the encoding in the form 0 and 1\n", 129 | " training = []\n", 130 | " output = []\n", 131 | "\n", 132 | " ## if tag is present then it will be 1 or else 0 ( [0,0,0,0,1,0] in are case we have 6 tag ) \n", 133 | " out_empty = [0 for _ in range(len(labels))]\n", 134 | " print(out_empty)\n", 135 | " for x , doc in enumerate(docs_x):\n", 136 | " bag = []\n", 137 | "\n", 138 | " wrds= [stemmer.stem(w) for w in doc]\n", 139 | " #print(wrds)\n", 140 | " for w in words:\n", 141 | " if w in wrds:\n", 142 | " bag.append(1)\n", 143 | " else:\n", 144 | " bag.append(0)\n", 145 | " # print(bag)\n", 146 | "\n", 147 | " output_row = out_empty[:]\n", 148 | " output_row[labels.index(docs_y[x])] = 1 \n", 149 | "\n", 150 | " # get the training and output\n", 151 | " training.append(bag)\n", 152 | " output.append(output_row)\n", 153 | "\n", 154 | " training = np.array(training)\n", 155 | " output = np.array(output)\n", 156 | " #print(training)\n", 157 | " #print(output)\n", 158 | " with open('data.pickle',\"wb\") as f:\n", 159 | " pickle.dump( (words, labels, training, output) , f)\n" 160 | ], 161 | "execution_count": 0, 162 | "outputs": [] 163 | }, 164 | { 165 | "cell_type": "markdown", 166 | "metadata": { 167 | "id": "irLV8R0ui33I", 168 | "colab_type": "text" 169 | }, 170 | "source": [ 171 | "**Tensorflow** " 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "metadata": { 177 | "id": "_mHuiaGoR1bd", 178 | "colab_type": "code", 179 | "colab": {} 180 | }, 181 | "source": [ 182 | "# remove warning\n", 183 | "import warnings\n", 184 | "warnings.simplefilter('ignore')\n", 185 | "\n", 186 | "# work with tensorflow\n", 187 | "tf.reset_default_graph()\n", 188 | "\n", 189 | "# training[0] all list have same len so we can take training[1]\n", 190 | "net = tflearn.input_data(shape=[None,len(training[0])])\n", 191 | "\n", 192 | "# 2 pipes of 8 hidden layer \n", 193 | "net = tflearn.fully_connected(net,8)\n", 194 | "net = tflearn.fully_connected(net,8)\n", 195 | "# activation=\"softmax\" tells probabillity of each neuron in the list (helps to finds the response)\n", 196 | "net = tflearn.fully_connected(net , len(output[0]), activation=\"softmax\")\n", 197 | "\n", 198 | "net = tflearn.regression(net)\n", 199 | "model = tflearn.DNN(net)\n", 200 | "\n", 201 | "# --------- Explanation--------------------\n", 202 | "\n", 203 | "# INPUT DATA ---> HIDDEN LAYER ---> HIDDEN LAYER ----> OUTPUT DATA \n", 204 | "# 45 input neurons --> 8 fully connected neurons --> 8 neurons ---> 6 neurons (\"Softmax\") " 205 | ], 206 | "execution_count": 0, 207 | "outputs": [] 208 | }, 209 | { 210 | "cell_type": "code", 211 | "metadata": { 212 | "id": "xzumTFn4ZzRN", 213 | "colab_type": "code", 214 | "colab": { 215 | "base_uri": "https://localhost:8080/", 216 | "height": 126 217 | }, 218 | "outputId": "03d0d838-b16f-4ebb-dc1c-6ee037d6f9f1" 219 | }, 220 | "source": [ 221 | "# n_epoch means how much time it will se our data\n", 222 | "# try:\n", 223 | "# model.load(\"model.tflearn\")\n", 224 | "# except:\n", 225 | "model.fit(training, output, n_epoch=1000, batch_size=8, show_metric=True)\n", 226 | "model.save(\"model.tflearn\")" 227 | ], 228 | "execution_count": 139, 229 | "outputs": [ 230 | { 231 | "output_type": "stream", 232 | "text": [ 233 | "Training Step: 4999 | total loss: \u001b[1m\u001b[32m0.01296\u001b[0m\u001b[0m | time: 0.016s\n", 234 | "| Adam | epoch: 1000 | loss: 0.01296 - acc: 1.0000 -- iter: 32/39\n", 235 | "Training Step: 5000 | total loss: \u001b[1m\u001b[32m0.01272\u001b[0m\u001b[0m | time: 0.019s\n", 236 | "| Adam | epoch: 1000 | loss: 0.01272 - acc: 1.0000 -- iter: 39/39\n", 237 | "--\n", 238 | "INFO:tensorflow:/content/model.tflearn is not in all_model_checkpoint_paths. Manually adding it.\n" 239 | ], 240 | "name": "stdout" 241 | } 242 | ] 243 | }, 244 | { 245 | "cell_type": "markdown", 246 | "metadata": { 247 | "id": "TVqYlt1Ujhv9", 248 | "colab_type": "text" 249 | }, 250 | "source": [ 251 | "**Start prediction**" 252 | ] 253 | }, 254 | { 255 | "cell_type": "code", 256 | "metadata": { 257 | "id": "HrYt-uKWf20p", 258 | "colab_type": "code", 259 | "colab": {} 260 | }, 261 | "source": [ 262 | "def beg_of_words(s, words):\n", 263 | " # contain 0 \n", 264 | " bag = [0 for i in range(len(words))]\n", 265 | " \n", 266 | " s_words = nltk.word_tokenize(s)\n", 267 | " s_words = [stemmer.stem(word.lower()) for word in s_words]\n", 268 | "\n", 269 | " ## sentance (se)\n", 270 | " for se in s_words:\n", 271 | " for i,w in enumerate(words):\n", 272 | " # that mean corrent words which we were looking at present in the sentace\n", 273 | " if w == se:\n", 274 | " bag[i] = 1\n", 275 | " \n", 276 | " return np.array(bag) " 277 | ], 278 | "execution_count": 0, 279 | "outputs": [] 280 | }, 281 | { 282 | "cell_type": "markdown", 283 | "metadata": { 284 | "id": "D_m1XbwdmBTX", 285 | "colab_type": "text" 286 | }, 287 | "source": [ 288 | "**Chat Response**" 289 | ] 290 | }, 291 | { 292 | "cell_type": "code", 293 | "metadata": { 294 | "id": "oY7zlpYjmAXD", 295 | "colab_type": "code", 296 | "colab": {} 297 | }, 298 | "source": [ 299 | "def chat():\n", 300 | " print(\"start talking with the bot (type 'quit' to exit) \")\n", 301 | " print(\"\\n\");\n", 302 | " while True:\n", 303 | " user_input = input(\"Type something 😃 : \")\n", 304 | " \n", 305 | " if user_input.lower() == 'quit':\n", 306 | " break\n", 307 | " \n", 308 | " # give the predicted response based on the word\n", 309 | " result = model.predict([beg_of_words(user_input , words)])\n", 310 | " \n", 311 | " #index of greated value in the list\n", 312 | " result_index = np.argmax(result)\n", 313 | " \n", 314 | " #print the tag \n", 315 | "\n", 316 | " tag = labels[result_index]\n", 317 | " print(\"Movie Genre is {}\".format(tag))\n", 318 | "\n", 319 | " # print the response \n", 320 | " for intent in data['intents']:\n", 321 | " if tag == intent['tag']:\n", 322 | " response = intent['responses']\n", 323 | " \n", 324 | " print(\"🤖 : {}\".format(random.choice(response)))\n", 325 | " print(\"\\n\")\n" 326 | ], 327 | "execution_count": 0, 328 | "outputs": [] 329 | }, 330 | { 331 | "cell_type": "code", 332 | "metadata": { 333 | "id": "RqFWIE6OlE-7", 334 | "colab_type": "code", 335 | "colab": { 336 | "base_uri": "https://localhost:8080/", 337 | "height": 545 338 | }, 339 | "outputId": "60acdefa-aa78-4b12-bf41-7ebac82a1685" 340 | }, 341 | "source": [ 342 | "chat()" 343 | ], 344 | "execution_count": 154, 345 | "outputs": [ 346 | { 347 | "output_type": "stream", 348 | "text": [ 349 | "start talking with the bot (type 'quit' to exit) \n", 350 | "\n", 351 | "\n", 352 | "Type something 😃 : Hello\n", 353 | "Movie Genre is greeting\n", 354 | "🤖 : Hi there, how can I help?\n", 355 | "\n", 356 | "\n", 357 | "Type something 😃 : I love romantic movie\n", 358 | "Movie Genre is Romance\n", 359 | "🤖 : Yeahh i like true love movies\n", 360 | "\n", 361 | "\n", 362 | "Type something 😃 : i fear watching horror movie\n", 363 | "Movie Genre is Horror\n", 364 | "🤖 : Hmm horror movies are so thriilled\n", 365 | "\n", 366 | "\n", 367 | "Type something 😃 : i like fighting and stunts movies\n", 368 | "Movie Genre is action and advanture\n", 369 | "🤖 : ohh nice\n", 370 | "\n", 371 | "\n", 372 | "Type something 😃 : i love to watch comedy movies\n", 373 | "Movie Genre is comedy\n", 374 | "🤖 : ohh nice\n", 375 | "\n", 376 | "\n", 377 | "Type something 😃 : quit\n" 378 | ], 379 | "name": "stdout" 380 | } 381 | ] 382 | }, 383 | { 384 | "cell_type": "code", 385 | "metadata": { 386 | "id": "iYveRNbYnZL3", 387 | "colab_type": "code", 388 | "colab": {} 389 | }, 390 | "source": [ 391 | "" 392 | ], 393 | "execution_count": 0, 394 | "outputs": [] 395 | } 396 | ] 397 | } -------------------------------------------------------------------------------- /machine_learning/random_forest_regression/random_forest_regression.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "# Importing the libraries\n", 12 | "import numpy as np\n", 13 | "import matplotlib.pyplot as plt\n", 14 | "import pandas as pd\n", 15 | "from sklearn.ensemble import RandomForestRegressor" 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": 2, 21 | "metadata": { 22 | "collapsed": true 23 | }, 24 | "outputs": [], 25 | "source": [ 26 | "# Importing the dataset\n", 27 | "dataset = pd.read_csv('Position_Salaries.csv')\n", 28 | "X = dataset.iloc[:, 1:2].values\n", 29 | "y = dataset.iloc[:, 2].values" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 3, 35 | "metadata": {}, 36 | "outputs": [ 37 | { 38 | "data": { 39 | "text/plain": [ 40 | "RandomForestRegressor(bootstrap=True, criterion='mse', max_depth=None,\n", 41 | " max_features='auto', max_leaf_nodes=None,\n", 42 | " min_impurity_split=1e-07, min_samples_leaf=1,\n", 43 | " min_samples_split=2, min_weight_fraction_leaf=0.0,\n", 44 | " n_estimators=300, n_jobs=1, oob_score=False, random_state=0,\n", 45 | " verbose=0, warm_start=False)" 46 | ] 47 | }, 48 | "execution_count": 3, 49 | "metadata": {}, 50 | "output_type": "execute_result" 51 | } 52 | ], 53 | "source": [ 54 | "# Fitting Random Forest Regression to the dataset\n", 55 | "regressor = RandomForestRegressor(n_estimators = 300, random_state = 0)\n", 56 | "regressor.fit(X, y)" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 4, 62 | "metadata": { 63 | "collapsed": true 64 | }, 65 | "outputs": [], 66 | "source": [ 67 | "# Predicting a new result\n", 68 | "y_pred = regressor.predict(6.5)" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": 5, 74 | "metadata": {}, 75 | "outputs": [ 76 | { 77 | "name": "stdout", 78 | "output_type": "stream", 79 | "text": [ 80 | "[ 160333.33333333]\n" 81 | ] 82 | } 83 | ], 84 | "source": [ 85 | "print(y_pred)" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": 6, 91 | "metadata": {}, 92 | "outputs": [ 93 | { 94 | "data": { 95 | "image/png": "iVBORw0KGgoAAAANSUhEUgAAAaEAAAEWCAYAAADPZygPAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3XmcXFWd9/HPNx1ICAgJkGHJziSKcUGgBwPMuABCADE4\nIuBEySCYUWEE0UeB+MgaxEEFHBl4MgGBsU1YlYisg7KNsiSIQECGGMiCBALZIB2SdOf3/HFPm0pT\nvVSlum5X6vt+vepVt85dzu/e6q5fnXtPnauIwMzMLA998g7AzMzql5OQmZnlxknIzMxy4yRkZma5\ncRIyM7PcOAmZmVlunISsS5JGS+o1ffklHSLppRKWP1XSa5LekrSDpH+QNC+9/mQH61wi6dSKBV0C\nST+TdG4edVvlSZou6ewKbOfTkpoqEVNv4iRU49IHadtjg6Q1Ba8nlrnNxZI+VuFQS6n/QknrC/bj\nWUlHl7mt/sAPgI9HxHYRsRK4ELg0vb69yDq7Ap8DpqfXh6Rj+5akNyX9SdIJ5e9h7yDpZEmt7f6G\nLqtyDJ0mXEl9JYWk1Sm+xekLQs18dkXEyRFxUQU29UtgH0nvq8C2eo2aeSOtuPRBul1EbAcsBI4q\nKHvHtyZJfasfZcc6iaepYL++CcyQtHMZVewK9IuIuQVlI4C5HSwPcCLwq4h4u6BsYYple+D/ANdI\nGl1GPL3NQ4V/QxFxeqkbqNLf1PvS8T8I+AIwqdIVSOrTm5NbZCMLzAS+lHcsldRrD7hVRmpV3CBp\nhqQ3gc+3//ZZeHpL0gxgd+DO9M3zjILlTkjfRJdKOrOTOgemOpZKeknSWZKU5p0s6UFJP5a0D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ScdU\nYls9uU2rT05CZuVbExEfSiMvrwO+XOoGIuLkiGj7kejZ7eb5V/u2xXMSMquMh4DRAJLOSK2jZySd\nnsq2lfRrSX9M5cel8vslNUq6mGwU5yclNaV5b6VnSbokrfd0wbofS+u33ROoKf3iv0OS9pX0gKQ5\nku6WtJukPSU9VrDMSElPd7R85Q+d1bO+eQdgVuvS8P+HA3dJ2hc4EfgwIOBRSQ8AewB/iYgj0zo7\nFG4jIs6UdGpEfKhIFf9INqLBXsDOwOOSHkzz9gbeR3brgf8hGyXj4Q7i3Ar4d2BCRCxNyWxqRHxR\n0taSRkXEi8BxwA0dLQ98sZzjZFaMk5BZ+baR9GSafohsnL6vAL+IiNUAkm4F/gG4C/ihpO8Dt0fE\nQyXU8/fAjIhoJRtU8gHg74BVwGMRsTjV9SQwkg6SEPAe4P3AvanB1EB26wKAG8mSz8Xp+bguljer\nCCchs/Ktad9y6ehsWET8r6R9gCOA70m6JyLOr0AMawumW+n8f1rA3IgodhvvG4CbUtKMiHhB0gc6\nWd6sInxNyKyyHgKOTiMybwt8GnhI0u5Ac0T8jOwGasVum7A+nQIrts3jJDVIGkx2R9THiizXleeB\nwZL2h+z0nKT3AUTEn8mS2P8lS0idLm9WKW4JmVVQRDwh6Vo2JonpEfEHSYcBl0jaAKwnO23X3jTg\nKUlPRMTEgvJfAPsDfyS7sdi3ImKJpD1LjG1d6lb943RNqi/Z3WbnpkVuAC4hu7lbd5Y322weRdvM\nzHLj03FmZpYbJyEzM8uNk5CZmeXGScjMzHLjJGRmZrlxEjIzs9w4CZmZWW7+P0PNi1lCP0XzAAAA\nAElFTkSuQmCC\n", 96 | "text/plain": [ 97 | "" 98 | ] 99 | }, 100 | "metadata": {}, 101 | "output_type": "display_data" 102 | } 103 | ], 104 | "source": [ 105 | "# Visualising the Random Forest Regression results (higher resolution)\n", 106 | "X_grid = np.arange(min(X), max(X), 0.01)\n", 107 | "X_grid = X_grid.reshape((len(X_grid), 1))\n", 108 | "plt.scatter(X, y, color = 'red')\n", 109 | "plt.plot(X_grid, regressor.predict(X_grid), color = 'blue')\n", 110 | "plt.title('Truth or Bluff (Random Forest Regression)')\n", 111 | "plt.xlabel('Position level')\n", 112 | "plt.ylabel('Salary')\n", 113 | "plt.show()" 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "execution_count": null, 119 | "metadata": { 120 | "collapsed": true 121 | }, 122 | "outputs": [], 123 | "source": [] 124 | } 125 | ], 126 | "metadata": { 127 | "kernelspec": { 128 | "display_name": "Python 3", 129 | "language": "python", 130 | "name": "python3" 131 | }, 132 | "language_info": { 133 | "codemirror_mode": { 134 | "name": "ipython", 135 | "version": 3 136 | }, 137 | "file_extension": ".py", 138 | "mimetype": "text/x-python", 139 | "name": "python", 140 | "nbconvert_exporter": "python", 141 | "pygments_lexer": "ipython3", 142 | "version": "3.5.1" 143 | } 144 | }, 145 | "nbformat": 4, 146 | "nbformat_minor": 2 147 | } 148 | --------------------------------------------------------------------------------