From 3dab8e03a465397a7b671128c155c9c03f8e0154 Mon Sep 17 00:00:00 2001 From: Robert Bergers Date: Sat, 29 Sep 2018 12:28:55 -0400 Subject: [PATCH 01/10] Just emboldened text and standardized some formatting throughout the file. modified: README.md modified: README.md modified: README.md --- README.md | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index bc31d5389..d840f89a4 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ These are for demonstration purposes only. There are many implementations of sor ### Bubble ![alt text][bubble-image] -From [Wikipedia][bubble-wiki]: Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. +From [Wikipedia][bubble-wiki]: **Bubble sort**, sometimes referred to as *sinking sort*, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. __Properties__ * Worst case performance O(n^2) @@ -24,7 +24,7 @@ __Properties__ ### Insertion ![alt text][insertion-image] -From [Wikipedia][insertion-wiki]: Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. +From [Wikipedia][insertion-wiki]: **Insertion sort** is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on *large* lists than more advanced algorithms such as quicksort, heapsort, or merge sort. __Properties__ * Worst case performance O(n^2) @@ -37,7 +37,7 @@ __Properties__ ### Merge ![alt text][merge-image] -From [Wikipedia][merge-wiki]: In computer science, merge sort (also commonly spelled mergesort) is an efficient, general-purpose, comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the implementation preserves the input order of equal elements in the sorted output. Mergesort is a divide and conquer algorithm that was invented by John von Neumann in 1945. +From [Wikipedia][merge-wiki]: **Merge sort** (also commonly spelled *mergesort*) is an efficient, general-purpose, comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the implementation preserves the input order of equal elements in the sorted output. Mergesort is a divide and conquer algorithm that was invented by John von Neumann in 1945. __Properties__ * Worst case performance O(n log n) @@ -50,7 +50,7 @@ __Properties__ ### Quick ![alt text][quick-image] -From [Wikipedia][quick-wiki]: Quicksort (sometimes called partition-exchange sort) is an efficient sorting algorithm, serving as a systematic method for placing the elements of an array in order. +From [Wikipedia][quick-wiki]: **Quicksort** (sometimes called *partition-exchange sort*) is an efficient sorting algorithm, serving as a systematic method for placing the elements of an array in order. __Properties__ * Worst case performance O(n^2) @@ -62,7 +62,7 @@ __Properties__ ### Selection ![alt text][selection-image] -From [Wikipedia][selection-wiki]: The algorithm divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging (swapping) it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right. +From [Wikipedia][selection-wiki]: **Selection sort** is an algorithm that divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging (swapping) it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right. __Properties__ * Worst case performance O(n^2) @@ -74,7 +74,7 @@ __Properties__ ### Shell ![alt text][shell-image] -From [Wikipedia][shell-wiki]: Shellsort is a generalization of insertion sort that allows the exchange of items that are far apart. The idea is to arrange the list of elements so that, starting anywhere, considering every nth element gives a sorted list. Such a list is said to be h-sorted. Equivalently, it can be thought of as h interleaved lists, each individually sorted. +From [Wikipedia][shell-wiki]: **Shellsort** is a generalization of *insertion sort* that allows the exchange of items that are far apart. The idea is to arrange the list of elements so that, starting anywhere, considering every nth element gives a sorted list. Such a list is said to be h-sorted. Equivalently, it can be thought of as h interleaved lists, each individually sorted. __Properties__ * Worst case performance O(nlog2 2n) @@ -85,7 +85,7 @@ __Properties__ ### Time-Complexity Graphs -Comparing the complexity of sorting algorithms (Bubble Sort, Insertion Sort, Selection Sort) +Comparing the complexity of sorting algorithms (*Bubble Sort*, *Insertion Sort*, *Selection Sort*) [Complexity Graphs](https://github.com/prateekiiest/Python/blob/master/sorts/sortinggraphs.png) @@ -96,8 +96,7 @@ Comparing the complexity of sorting algorithms (Bubble Sort, Insertion Sort, Sel ### Linear ![alt text][linear-image] -From [Wikipedia][linear-wiki]: linear search or sequential search is a method for finding a target value within a list. It sequentially checks each element of the list for the target value until a match is found or until all the elements have been searched. - Linear search runs in at worst linear time and makes at most n comparisons, where n is the length of the list. +From [Wikipedia][linear-wiki]: **Linear search** or *sequential search* is a method for finding a target value within a list. It sequentially checks each element of the list for the target value until a match is found or until all the elements have been searched. Linear search runs in at worst linear time and makes at most n comparisons, where n is the length of the list. __Properties__ * Worst case performance O(n) @@ -108,7 +107,7 @@ __Properties__ ### Binary ![alt text][binary-image] -From [Wikipedia][binary-wiki]: Binary search, also known as half-interval search or logarithmic search, is a search algorithm that finds the position of a target value within a sorted array. It compares the target value to the middle element of the array; if they are unequal, the half in which the target cannot lie is eliminated and the search continues on the remaining half until it is successful. +From [Wikipedia][binary-wiki]: **Binary search**, also known as *half-interval search* or *logarithmic search*, is a search algorithm that finds the position of a target value within a sorted array. It compares the target value to the middle element of the array; if they are unequal, the half in which the target cannot lie is eliminated and the search continues on the remaining half until it is successful. __Properties__ * Worst case performance O(log n) @@ -122,7 +121,7 @@ __Properties__ ### Caesar ![alt text][caesar]
-In cryptography, a **Caesar cipher**, also known as Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques.
+**Caesar cipher**, also known as Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques.
It is **a type of substitution cipher** in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be replaced by A, E would become B, and so on.
The method is named after **Julius Caesar**, who used it in his private correspondence.
The encryption step performed by a Caesar cipher is often incorporated as part of more complex schemes, such as the Vigenère cipher, and still has modern application in the ROT13 system. As with all single-alphabet substitution ciphers, the Caesar cipher is easily broken and in modern practice offers essentially no communication security. @@ -136,7 +135,7 @@ Many people have tried to implement encryption schemes that are essentially Vige ###### Source: [Wikipedia](https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher) ### Transposition -In cryptography, a **transposition cipher** is a method of encryption by which the positions held by units of plaintext (which are commonly characters or groups of characters) are shifted according to a regular system, so that the ciphertext constitutes a permutation of the plaintext. That is, the order of the units is changed (the plaintext is reordered).
+The **Transposition cipher** is a method of encryption by which the positions held by units of *plaintext* (which are commonly characters or groups of characters) are shifted according to a regular system, so that the *ciphertext* constitutes a permutation of the plaintext. That is, the order of the units is changed (the plaintext is reordered).
Mathematically a bijective function is used on the characters' positions to encrypt and an inverse function to decrypt. ###### Source: [Wikipedia](https://en.wikipedia.org/wiki/Transposition_cipher) From ee8c01179dfe3b4261ac0e53d22a5a6b254f6a37 Mon Sep 17 00:00:00 2001 From: Himani Negi <36325656+Himani2000@users.noreply.github.com> Date: Tue, 9 Oct 2018 01:51:14 +0530 Subject: [PATCH 02/10] Add files via upload --- machine_learning/NaiveBayes.ipynb | 1659 +++++++++++++++++++++++++++++ 1 file changed, 1659 insertions(+) create mode 100644 machine_learning/NaiveBayes.ipynb diff --git a/machine_learning/NaiveBayes.ipynb b/machine_learning/NaiveBayes.ipynb new file mode 100644 index 000000000..5a427c5cb --- /dev/null +++ b/machine_learning/NaiveBayes.ipynb @@ -0,0 +1,1659 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "from sklearn import datasets\n", + "import pandas as pd" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "iris = datasets.load_iris()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "df = pd.DataFrame(iris.data)\n", + "df.columns = [\"sl\", \"sw\", 'pl', 'pw']" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def abc(k, *val):\n", + " if k < val[0]:\n", + " return 0\n", + " else:\n", + " return 1" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0 1\n", + "1 0\n", + "2 0\n", + "3 0\n", + "4 1\n", + "5 1\n", + "6 0\n", + "7 1\n", + "8 0\n", + "9 0\n", + "10 1\n", + "11 0\n", + "12 0\n", + "13 0\n", + "14 1\n", + "15 1\n", + "16 1\n", + "17 1\n", + "18 1\n", + "19 1\n", + "20 1\n", + "21 1\n", + "22 0\n", + "23 1\n", + "24 0\n", + "25 1\n", + "26 1\n", + "27 1\n", + "28 1\n", + "29 0\n", + " ..\n", + "120 1\n", + "121 1\n", + "122 1\n", + "123 1\n", + "124 1\n", + "125 1\n", + "126 1\n", + "127 1\n", + "128 1\n", + "129 1\n", + "130 1\n", + "131 1\n", + "132 1\n", + "133 1\n", + "134 1\n", + "135 1\n", + "136 1\n", + "137 1\n", + "138 1\n", + "139 1\n", + "140 1\n", + "141 1\n", + "142 1\n", + "143 1\n", + "144 1\n", + "145 1\n", + "146 1\n", + "147 1\n", + "148 1\n", + "149 1\n", + "Name: sl, dtype: int64" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.sl.apply(abc, args=(5,))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "def label(val, *boundaries):\n", + " if (val < boundaries[0]):\n", + " return 'a'\n", + " elif (val < boundaries[1]):\n", + " return 'b'\n", + " elif (val < boundaries[2]):\n", + " return 'c'\n", + " else:\n", + " return 'd'\n", + "\n", + "def toLabel(df, old_feature_name):\n", + " second = df[old_feature_name].mean()\n", + " minimum = df[old_feature_name].min()\n", + " first = (minimum + second)/2\n", + " maximum = df[old_feature_name].max()\n", + " third = (maximum + second)/2\n", + " return df[old_feature_name].apply(label, args= (first, second, third))" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
slswplpwsl_labeledsw_labeledpl_labeledpw_labeled
05.13.51.40.2bcaa
14.93.01.40.2abaa
24.73.21.30.2acaa
34.63.11.50.2acaa
45.03.61.40.2acaa
55.43.91.70.4bdaa
64.63.41.40.3acaa
75.03.41.50.2acaa
84.42.91.40.2abaa
94.93.11.50.1acaa
105.43.71.50.2bcaa
114.83.41.60.2acaa
124.83.01.40.1abaa
134.33.01.10.1abaa
145.84.01.20.2bdaa
155.74.41.50.4bdaa
165.43.91.30.4bdaa
175.13.51.40.3bcaa
185.73.81.70.3bdaa
195.13.81.50.3bdaa
205.43.41.70.2bcaa
215.13.71.50.4bcaa
224.63.61.00.2acaa
235.13.31.70.5bcaa
244.83.41.90.2acaa
255.03.01.60.2abaa
265.03.41.60.4acaa
275.23.51.50.2bcaa
285.23.41.40.2bcaa
294.73.21.60.2acaa
...........................
1206.93.25.72.3dcdd
1215.62.84.92.0bbcd
1227.72.86.72.0dbdd
1236.32.74.91.8cbcc
1246.73.35.72.1ccdd
1257.23.26.01.8dcdc
1266.22.84.81.8cbcc
1276.13.04.91.8cbcc
1286.42.85.62.1cbdd
1297.23.05.81.6dbdc
1307.42.86.11.9dbdd
1317.93.86.42.0dddd
1326.42.85.62.2cbdd
1336.32.85.11.5cbcc
1346.12.65.61.4cbdc
1357.73.06.12.3dbdd
1366.33.45.62.4ccdd
1376.43.15.51.8ccdc
1386.03.04.81.8cbcc
1396.93.15.42.1dcdd
1406.73.15.62.4ccdd
1416.93.15.12.3dccd
1425.82.75.11.9bbcd
1436.83.25.92.3ccdd
1446.73.35.72.5ccdd
1456.73.05.22.3cbcd
1466.32.55.01.9cacd
1476.53.05.22.0cbcd
1486.23.45.42.3ccdd
1495.93.05.11.8cbcc
\n", + "

150 rows × 8 columns

\n", + "
" + ], + "text/plain": [ + " sl sw pl pw sl_labeled sw_labeled pl_labeled pw_labeled\n", + "0 5.1 3.5 1.4 0.2 b c a a\n", + "1 4.9 3.0 1.4 0.2 a b a a\n", + "2 4.7 3.2 1.3 0.2 a c a a\n", + "3 4.6 3.1 1.5 0.2 a c a a\n", + "4 5.0 3.6 1.4 0.2 a c a a\n", + "5 5.4 3.9 1.7 0.4 b d a a\n", + "6 4.6 3.4 1.4 0.3 a c a a\n", + "7 5.0 3.4 1.5 0.2 a c a a\n", + "8 4.4 2.9 1.4 0.2 a b a a\n", + "9 4.9 3.1 1.5 0.1 a c a a\n", + "10 5.4 3.7 1.5 0.2 b c a a\n", + "11 4.8 3.4 1.6 0.2 a c a a\n", + "12 4.8 3.0 1.4 0.1 a b a a\n", + "13 4.3 3.0 1.1 0.1 a b a a\n", + "14 5.8 4.0 1.2 0.2 b d a a\n", + "15 5.7 4.4 1.5 0.4 b d a a\n", + "16 5.4 3.9 1.3 0.4 b d a a\n", + "17 5.1 3.5 1.4 0.3 b c a a\n", + "18 5.7 3.8 1.7 0.3 b d a a\n", + "19 5.1 3.8 1.5 0.3 b d a a\n", + "20 5.4 3.4 1.7 0.2 b c a a\n", + "21 5.1 3.7 1.5 0.4 b c a a\n", + "22 4.6 3.6 1.0 0.2 a c a a\n", + "23 5.1 3.3 1.7 0.5 b c a a\n", + "24 4.8 3.4 1.9 0.2 a c a a\n", + "25 5.0 3.0 1.6 0.2 a b a a\n", + "26 5.0 3.4 1.6 0.4 a c a a\n", + "27 5.2 3.5 1.5 0.2 b c a a\n", + "28 5.2 3.4 1.4 0.2 b c a a\n", + "29 4.7 3.2 1.6 0.2 a c a a\n", + ".. ... ... ... ... ... ... ... ...\n", + "120 6.9 3.2 5.7 2.3 d c d d\n", + "121 5.6 2.8 4.9 2.0 b b c d\n", + "122 7.7 2.8 6.7 2.0 d b d d\n", + "123 6.3 2.7 4.9 1.8 c b c c\n", + "124 6.7 3.3 5.7 2.1 c c d d\n", + "125 7.2 3.2 6.0 1.8 d c d c\n", + "126 6.2 2.8 4.8 1.8 c b c c\n", + "127 6.1 3.0 4.9 1.8 c b c c\n", + "128 6.4 2.8 5.6 2.1 c b d d\n", + "129 7.2 3.0 5.8 1.6 d b d c\n", + "130 7.4 2.8 6.1 1.9 d b d d\n", + "131 7.9 3.8 6.4 2.0 d d d d\n", + "132 6.4 2.8 5.6 2.2 c b d d\n", + "133 6.3 2.8 5.1 1.5 c b c c\n", + "134 6.1 2.6 5.6 1.4 c b d c\n", + "135 7.7 3.0 6.1 2.3 d b d d\n", + "136 6.3 3.4 5.6 2.4 c c d d\n", + "137 6.4 3.1 5.5 1.8 c c d c\n", + "138 6.0 3.0 4.8 1.8 c b c c\n", + "139 6.9 3.1 5.4 2.1 d c d d\n", + "140 6.7 3.1 5.6 2.4 c c d d\n", + "141 6.9 3.1 5.1 2.3 d c c d\n", + "142 5.8 2.7 5.1 1.9 b b c d\n", + "143 6.8 3.2 5.9 2.3 c c d d\n", + "144 6.7 3.3 5.7 2.5 c c d d\n", + "145 6.7 3.0 5.2 2.3 c b c d\n", + "146 6.3 2.5 5.0 1.9 c a c d\n", + "147 6.5 3.0 5.2 2.0 c b c d\n", + "148 6.2 3.4 5.4 2.3 c c d d\n", + "149 5.9 3.0 5.1 1.8 c b c c\n", + "\n", + "[150 rows x 8 columns]" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df['sl_labeled'] = toLabel(df, 'sl')\n", + "df['sw_labeled'] = toLabel(df, 'sw')\n", + "df['pl_labeled'] = toLabel(df, 'pl')\n", + "df['pw_labeled'] = toLabel(df, 'pw')\n", + "df" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "df.drop(['sl', 'sw', 'pl', 'pw'], axis = 1, inplace = True)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'a', 'b', 'c', 'd'}" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "set(df['sl_labeled'])" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "df[\"output\"] = iris.target" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
sl_labeledsw_labeledpl_labeledpw_labeledoutput
0bcaa0
1abaa0
2acaa0
3acaa0
4acaa0
5bdaa0
6acaa0
7acaa0
8abaa0
9acaa0
10bcaa0
11acaa0
12abaa0
13abaa0
14bdaa0
15bdaa0
16bdaa0
17bcaa0
18bdaa0
19bdaa0
20bcaa0
21bcaa0
22acaa0
23bcaa0
24acaa0
25abaa0
26acaa0
27bcaa0
28bcaa0
29acaa0
..................
120dcdd2
121bbcd2
122dbdd2
123cbcc2
124ccdd2
125dcdc2
126cbcc2
127cbcc2
128cbdd2
129dbdc2
130dbdd2
131dddd2
132cbdd2
133cbcc2
134cbdc2
135dbdd2
136ccdd2
137ccdc2
138cbcc2
139dcdd2
140ccdd2
141dccd2
142bbcd2
143ccdd2
144ccdd2
145cbcd2
146cacd2
147cbcd2
148ccdd2
149cbcc2
\n", + "

150 rows × 5 columns

\n", + "
" + ], + "text/plain": [ + " sl_labeled sw_labeled pl_labeled pw_labeled output\n", + "0 b c a a 0\n", + "1 a b a a 0\n", + "2 a c a a 0\n", + "3 a c a a 0\n", + "4 a c a a 0\n", + "5 b d a a 0\n", + "6 a c a a 0\n", + "7 a c a a 0\n", + "8 a b a a 0\n", + "9 a c a a 0\n", + "10 b c a a 0\n", + "11 a c a a 0\n", + "12 a b a a 0\n", + "13 a b a a 0\n", + "14 b d a a 0\n", + "15 b d a a 0\n", + "16 b d a a 0\n", + "17 b c a a 0\n", + "18 b d a a 0\n", + "19 b d a a 0\n", + "20 b c a a 0\n", + "21 b c a a 0\n", + "22 a c a a 0\n", + "23 b c a a 0\n", + "24 a c a a 0\n", + "25 a b a a 0\n", + "26 a c a a 0\n", + "27 b c a a 0\n", + "28 b c a a 0\n", + "29 a c a a 0\n", + ".. ... ... ... ... ...\n", + "120 d c d d 2\n", + "121 b b c d 2\n", + "122 d b d d 2\n", + "123 c b c c 2\n", + "124 c c d d 2\n", + "125 d c d c 2\n", + "126 c b c c 2\n", + "127 c b c c 2\n", + "128 c b d d 2\n", + "129 d b d c 2\n", + "130 d b d d 2\n", + "131 d d d d 2\n", + "132 c b d d 2\n", + "133 c b c c 2\n", + "134 c b d c 2\n", + "135 d b d d 2\n", + "136 c c d d 2\n", + "137 c c d c 2\n", + "138 c b c c 2\n", + "139 d c d d 2\n", + "140 c c d d 2\n", + "141 d c c d 2\n", + "142 b b c d 2\n", + "143 c c d d 2\n", + "144 c c d d 2\n", + "145 c b c d 2\n", + "146 c a c d 2\n", + "147 c b c d 2\n", + "148 c c d d 2\n", + "149 c b c c 2\n", + "\n", + "[150 rows x 5 columns]" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def fit(data):\n", + " output_name = data.columns[-1]\n", + " features = data.columns[0:-1]\n", + " counts = {}\n", + " possible_outputs = set(data[output_name])\n", + " for output in possible_outputs:\n", + " counts[output] = {}\n", + " smallData = data[data[output_name] == output]\n", + " counts[output][\"total_count\"] = len(smallData)\n", + " for f in features:\n", + " counts[output][f] = {}\n", + " possible_values = set(smallData[f])\n", + " for value in possible_values:\n", + " val_count = len(smallData[smallData[f] == value])\n", + " counts[output][f][value] = val_count\n", + " return counts" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{0: {'pl_labeled': {'a': 50},\n", + " 'pw_labeled': {'a': 50},\n", + " 'sl_labeled': {'a': 28, 'b': 22},\n", + " 'sw_labeled': {'a': 1, 'b': 7, 'c': 32, 'd': 10},\n", + " 'total_count': 50},\n", + " 1: {'pl_labeled': {'b': 7, 'c': 43},\n", + " 'pw_labeled': {'b': 10, 'c': 40},\n", + " 'sl_labeled': {'a': 3, 'b': 21, 'c': 24, 'd': 2},\n", + " 'sw_labeled': {'a': 13, 'b': 29, 'c': 8},\n", + " 'total_count': 50},\n", + " 2: {'pl_labeled': {'c': 20, 'd': 30},\n", + " 'pw_labeled': {'c': 16, 'd': 34},\n", + " 'sl_labeled': {'a': 1, 'b': 5, 'c': 29, 'd': 15},\n", + " 'sw_labeled': {'a': 5, 'b': 28, 'c': 15, 'd': 2},\n", + " 'total_count': 50}}" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "fit(df)" + ] + } + ], + "metadata": { + "anaconda-cloud": {}, + "kernelspec": { + "display_name": "Python [default]", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.5" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} From ade3ed3784fe8e34978563df76a7b4f7d5fe3665 Mon Sep 17 00:00:00 2001 From: Taru <32308101+epicalyx@users.noreply.github.com> Date: Wed, 17 Oct 2018 00:22:44 +0530 Subject: [PATCH 03/10] Logistic regression implementation implementation of logistic regression for binary classification --- machine_learning/logistic_regression.py | 97 +++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 machine_learning/logistic_regression.py diff --git a/machine_learning/logistic_regression.py b/machine_learning/logistic_regression.py new file mode 100644 index 000000000..70c0b2807 --- /dev/null +++ b/machine_learning/logistic_regression.py @@ -0,0 +1,97 @@ +#!/usr/bin/env python +# coding: utf-8 + +# # Logistic Regression from scratch + +# In[62]: + + +''' Implementing logistic regression for classification problem + Helpful resources : 1.Coursera ML course 2.https://medium.com/@martinpella/logistic-regression-from-scratch-in-python-124c5636b8ac''' + + +# In[63]: + + +#importing all the required libraries +import numpy as np +import matplotlib.pyplot as plt +get_ipython().run_line_magic('matplotlib', 'inline') +from sklearn import datasets + + +# In[67]: + + +#sigmoid function or logistic function is used as a hypothesis function in classification problems +def sigmoid_function(z): + return 1/(1+np.exp(-z)) + + +def cost_function(h,y): + return (-y*np.log(h)-(1-y)*np.log(1-h)).mean() + +# here alpha is the learning rate, X is the featue matrix,y is the target matrix +def logistic_reg(alpha,X,y,max_iterations=70000): + converged=False + iterations=0 + theta=np.zeros(X.shape[1]) + + num_iterations=0 + while not converged: + z=np.dot(X,theta) + h=sigmoid_function(z) + gradient = np.dot(X.T,(h-y))/y.size + theta=theta-(alpha)*gradient + + z=np.dot(X,theta) + h=sigmoid_function(z) + e=cost_function(h,y) + print('J=',e) + J=e + + iterations+=1 #update iterations + + + if iterations== max_iterations: + print("Maximum iterations exceeded!") + converged=True + + return theta + + + + + + + + +# In[68]: + + +if __name__=='__main__': + iris=datasets.load_iris() + X = iris.data[:, :2] + y = (iris.target != 0) * 1 + + alpha=0.1 + theta=logistic_reg(alpha,X,y,max_iterations=70000) + print(theta) + def predict_prob(X): + return sigmoid_function(np.dot(X,theta)) # predicting the value of probability from the logistic regression algorithm + + + plt.figure(figsize=(10, 6)) + plt.scatter(X[y == 0][:, 0], X[y == 0][:, 1], color='b', label='0') + plt.scatter(X[y == 1][:, 0], X[y == 1][:, 1], color='r', label='1') + x1_min, x1_max = X[:,0].min(), X[:,0].max(), + x2_min, x2_max = X[:,1].min(), X[:,1].max(), + xx1, xx2 = np.meshgrid(np.linspace(x1_min, x1_max), np.linspace(x2_min, x2_max)) + grid = np.c_[xx1.ravel(), xx2.ravel()] + probs = predict_prob(grid).reshape(xx1.shape) + plt.contour(xx1, xx2, probs, [0.5], linewidths=1, colors='black'); + + plt.legend(); + + + From f018ddc4c00f71e7123b780d409550e101e3c673 Mon Sep 17 00:00:00 2001 From: Taru <32308101+epicalyx@users.noreply.github.com> Date: Wed, 17 Oct 2018 00:52:32 +0530 Subject: [PATCH 04/10] requested changes addressed --- machine_learning/logistic_regression.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/machine_learning/logistic_regression.py b/machine_learning/logistic_regression.py index 70c0b2807..2e28fd96b 100644 --- a/machine_learning/logistic_regression.py +++ b/machine_learning/logistic_regression.py @@ -31,13 +31,13 @@ def sigmoid_function(z): def cost_function(h,y): return (-y*np.log(h)-(1-y)*np.log(1-h)).mean() -# here alpha is the learning rate, X is the featue matrix,y is the target matrix +# here alpha is the learning rate, X is the feature matrix,y is the target matrix def logistic_reg(alpha,X,y,max_iterations=70000): converged=False iterations=0 theta=np.zeros(X.shape[1]) - num_iterations=0 + while not converged: z=np.dot(X,theta) h=sigmoid_function(z) @@ -46,9 +46,9 @@ def logistic_reg(alpha,X,y,max_iterations=70000): z=np.dot(X,theta) h=sigmoid_function(z) - e=cost_function(h,y) - print('J=',e) - J=e + J=cost_function(h,y) + + iterations+=1 #update iterations From 7105f6f648aa728c44e04e2389ad141314e2818f Mon Sep 17 00:00:00 2001 From: Taru <32308101+epicalyx@users.noreply.github.com> Date: Wed, 17 Oct 2018 01:07:29 +0530 Subject: [PATCH 05/10] minor changes requested changes are addressed --- machine_learning/logistic_regression.py | 1 + 1 file changed, 1 insertion(+) diff --git a/machine_learning/logistic_regression.py b/machine_learning/logistic_regression.py index 2e28fd96b..de0cfd54d 100644 --- a/machine_learning/logistic_regression.py +++ b/machine_learning/logistic_regression.py @@ -55,6 +55,7 @@ def logistic_reg(alpha,X,y,max_iterations=70000): if iterations== max_iterations: print("Maximum iterations exceeded!") + print("Minimal cost function J=",J) converged=True return theta From 683474c64b4ad4c423aafcb967ac3897046a46bf Mon Sep 17 00:00:00 2001 From: Simon Landry Date: Tue, 16 Oct 2018 22:41:33 -0400 Subject: [PATCH 06/10] Add Problem 31 solution --- Project Euler/Problem 31/sol1.py | 53 ++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 Project Euler/Problem 31/sol1.py diff --git a/Project Euler/Problem 31/sol1.py b/Project Euler/Problem 31/sol1.py new file mode 100644 index 000000000..33653722f --- /dev/null +++ b/Project Euler/Problem 31/sol1.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +from __future__ import print_function +try: + raw_input # Python 2 +except NameError: + raw_input = input # Python 3 +''' +Coin sums +Problem 31 +In England the currency is made up of pound, £, and pence, p, and there are +eight coins in general circulation: + +1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p). +It is possible to make £2 in the following way: + +1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p +How many different ways can £2 be made using any number of coins? +''' + + +def one_pence(): + return 1 + + +def two_pence(x): + return 0 if x < 0 else two_pence(x - 2) + one_pence() + + +def five_pence(x): + return 0 if x < 0 else five_pence(x - 5) + two_pence(x) + + +def ten_pence(x): + return 0 if x < 0 else ten_pence(x - 10) + five_pence(x) + + +def twenty_pence(x): + return 0 if x < 0 else twenty_pence(x - 20) + ten_pence(x) + + +def fifty_pence(x): + return 0 if x < 0 else fifty_pence(x - 50) + twenty_pence(x) + + +def one_pound(x): + return 0 if x < 0 else one_pound(x - 100) + fifty_pence(x) + + +def two_pound(x): + return 0 if x < 0 else two_pound(x - 200) + one_pound(x) + + +print(two_pound(200)) From 94324e91b439fb24704707f3ed20ff2e607acee4 Mon Sep 17 00:00:00 2001 From: Srikumar Sastry Date: Fri, 19 Oct 2018 18:51:40 +0530 Subject: [PATCH 07/10] Create FractionalKnapsack.py (#438) --- dynamic_programming/FractionalKnapsack.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 dynamic_programming/FractionalKnapsack.py diff --git a/dynamic_programming/FractionalKnapsack.py b/dynamic_programming/FractionalKnapsack.py new file mode 100644 index 000000000..74e85b4b4 --- /dev/null +++ b/dynamic_programming/FractionalKnapsack.py @@ -0,0 +1,12 @@ +from itertools import accumulate +from bisect import bisect + +def fracKnapsack(vl, wt, W, n): + + r = list(sorted(zip(vl,wt), key=lambda x:x[0]/x[1],reverse=True)) + vl , wt = [i[0] for i in r],[i[1] for i in r] + acc=list(accumulate(wt)) + k = bisect(acc,W) + return 0 if k == 0 else sum(vl[:k])+(W-acc[k-1])*(vl[k])/(wt[k]) if k!=n else sum(vl[:k]) + +print("%.0f"%fracKnapsack([60, 100, 120],[10, 20, 30],50,3)) From c0f7df7e22021baf4f16d7584ee2eb03db138326 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20G=C3=B3mez?= Date: Fri, 19 Oct 2018 08:42:19 -0500 Subject: [PATCH 09/10] Fixed error on chr function when decrypt (#359) On line 23 when make the operations returns a float and chr function doesn't permit float values as parameters. --- ciphers/Onepad_Cipher.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ciphers/Onepad_Cipher.py b/ciphers/Onepad_Cipher.py index 7e1be5fdc..6afbd4524 100644 --- a/ciphers/Onepad_Cipher.py +++ b/ciphers/Onepad_Cipher.py @@ -20,7 +20,7 @@ class Onepad: '''Function to decrypt text using psedo-random numbers.''' plain = [] for i in range(len(key)): - p = (cipher[i]-(key[i])**2)/key[i] + p = int((cipher[i]-(key[i])**2)/key[i]) plain.append(chr(p)) plain = ''.join([i for i in plain]) return plain From b63a1115b23fcd9e32db6f28d6a569b80252d18e Mon Sep 17 00:00:00 2001 From: Kiyoto Kai <35162413+Kaiyoto@users.noreply.github.com> Date: Fri, 19 Oct 2018 22:45:53 +0530 Subject: [PATCH 10/10] Added a new Algorithm to check if a number is prime or not. (#487) * Added a new Algorithm to check if a number is prime or not. Added a new Algorithm to check if a number is prime or not. It takes one + half the amount of iterations of the square root of the number. Returns Boolean value. * Fixed possibility of being truncated Changed the 1/2 with a 0.5 * Fixed Major Error Instead of 3, 5, 7 The Loop as checking 2, 4, 6 which would cause all odd numbers to show prime. Fixed by subtracting one. * Fixed Minor Formatting issues Github Merged the 2 previous and current version to make a weird file. * Fixed possibility of being truncated Changed the 1/2 with a 0.5 --- Maths/PrimeCheck.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Maths/PrimeCheck.py diff --git a/Maths/PrimeCheck.py b/Maths/PrimeCheck.py new file mode 100644 index 000000000..79fd343db --- /dev/null +++ b/Maths/PrimeCheck.py @@ -0,0 +1,17 @@ +def primeCheck(number): + prime = True + for i in range(2, int(number**(0.5)+1), 2): + if i != 2: + i = i - 1 + if number % i == 0: + prime = False + break + return prime + +def main(): + print(primeCheck(37)) + print(primeCheck(100)) + print(primeCheck(77)) + +if __name__ == '__main__': + main()