Skip to content

Commit 7c9a07c

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents e4d537a + f9156cf commit 7c9a07c

11 files changed

+214
-162
lines changed

.travis.yml

-14
This file was deleted.

data_structures/Binary Tree/binary_seach_tree.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Node:
88
def __init__(self, label):
99
self.label = label
1010
self.left = None
11-
self.rigt = None
11+
self.right = None
1212

1313
def getLabel(self):
1414
return self.label
@@ -23,10 +23,10 @@ def setLeft(self, left):
2323
self.left = left
2424

2525
def getRight(self):
26-
return self.rigt
26+
return self.right
2727

2828
def setRight(self, right):
29-
self.rigt = right
29+
self.right = right
3030

3131

3232
class BinarySearchTree:

Graphs/Breadth_First_Search.py renamed to data_structures/Graph/Breadth_First_Search.py

+23-21
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
class GRAPH:
22
"""docstring for GRAPH"""
33
def __init__(self, nodes):
4-
self.nodes=nodes
5-
self.graph=[[0]*nodes for i in range (nodes)]
6-
self.visited=[0]*nodes
4+
self.nodes = nodes
5+
self.graph = [[0]*nodes for i in range (nodes)]
6+
self.visited = [0]*nodes
77

88

99
def show(self):
@@ -23,7 +23,7 @@ def bfs(self,v):
2323
v = queue[0]
2424
for u in range(self.vertex):
2525
if self.graph[v][u] == 1:
26-
if visited[u]== False:
26+
if visited[u] is False:
2727
visited[u] = True
2828
queue.append(u)
2929
print('%d visited' % (u +1))
@@ -41,30 +41,32 @@ def bfs(self,v):
4141
g.add_edge(5,9)
4242
g.add_edge(6,10)
4343
g.bfs(4)
44-
=======
45-
print self.graph
44+
45+
print(self.graph)
4646

4747
def add_edge(self, i, j):
4848
self.graph[i][j]=1
4949
self.graph[j][i]=1
5050

51-
def bfs(self,s):
52-
queue=[s]
53-
self.visited[s]=1
54-
while len(queue)!=0:
55-
x=queue.pop(0)
51+
def bfs(self, s):
52+
queue = [s]
53+
self.visited[s] = 1
54+
while len(queue)!= 0:
55+
x = queue.pop(0)
5656
print(x)
57-
for i in range(0,self.nodes):
58-
if self.graph[x][i]==1 and self.visited[i]==0:
57+
for i in range(0, self.nodes):
58+
if self.graph[x][i] == 1 and self.visited[i] == 0:
5959
queue.append(i)
60-
self.visited[i]=1
60+
self.visited[i] = 1
6161

62-
n=int(input("Enter the number of Nodes : "))
63-
g=GRAPH(n)
64-
e=int(input("Enter the no of edges : "))
62+
n = int(input("Enter the number of Nodes : "))
63+
g = GRAPH(n)
64+
e = int(input("Enter the no of edges : "))
6565
print("Enter the edges (u v)")
66-
for i in range(0,e):
67-
u,v=map(int, raw_input().split())
68-
g.add_edge(u,v)
69-
s=int(input("Enter the source node :"))
66+
67+
for i in range(0, e):
68+
u ,v = map(int, raw_input().split())
69+
g.add_edge(u, v)
70+
71+
s = int(input("Enter the source node :"))
7072
g.bfs(s)
File renamed without changes.
File renamed without changes.

data_structures/Graph/P01_BreadthFirstSearch.py

-61
This file was deleted.

data_structures/Graph/P02_DepthFirstSearch.py

-61
This file was deleted.

machine_learning/decision_tree.py

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
"""
2+
Implementation of a basic regression decision tree.
3+
Input data set: The input data set must be 1-dimensional with continuous labels.
4+
Output: The decision tree maps a real number input to a real number output.
5+
"""
6+
7+
import numpy as np
8+
9+
class Decision_Tree:
10+
def __init__(self, depth = 5, min_leaf_size = 5):
11+
self.depth = depth
12+
self.decision_boundary = 0
13+
self.left = None
14+
self.right = None
15+
self.min_leaf_size = min_leaf_size
16+
self.prediction = None
17+
18+
def mean_squared_error(self, labels, prediction):
19+
"""
20+
mean_squared_error:
21+
@param labels: a one dimensional numpy array
22+
@param prediction: a floating point value
23+
return value: mean_squared_error calculates the error if prediction is used to estimate the labels
24+
"""
25+
if labels.ndim != 1:
26+
print("Error: Input labels must be one dimensional")
27+
28+
return np.mean((labels - prediction) ** 2)
29+
30+
def train(self, X, y):
31+
"""
32+
train:
33+
@param X: a one dimensional numpy array
34+
@param y: a one dimensional numpy array.
35+
The contents of y are the labels for the corresponding X values
36+
37+
train does not have a return value
38+
"""
39+
40+
"""
41+
this section is to check that the inputs conform to our dimensionality constraints
42+
"""
43+
if X.ndim != 1:
44+
print("Error: Input data set must be one dimensional")
45+
return
46+
if len(X) != len(y):
47+
print("Error: X and y have different lengths")
48+
return
49+
if y.ndim != 1:
50+
print("Error: Data set labels must be one dimensional")
51+
return
52+
53+
if len(X) < 2 * self.min_leaf_size:
54+
self.prediction = np.mean(y)
55+
return
56+
57+
if self.depth == 1:
58+
self.prediction = np.mean(y)
59+
return
60+
61+
best_split = 0
62+
min_error = self.mean_squared_error(X,np.mean(y)) * 2
63+
64+
65+
"""
66+
loop over all possible splits for the decision tree. find the best split.
67+
if no split exists that is less than 2 * error for the entire array
68+
then the data set is not split and the average for the entire array is used as the predictor
69+
"""
70+
for i in range(len(X)):
71+
if len(X[:i]) < self.min_leaf_size:
72+
continue
73+
elif len(X[i:]) < self.min_leaf_size:
74+
continue
75+
else:
76+
error_left = self.mean_squared_error(X[:i], np.mean(y[:i]))
77+
error_right = self.mean_squared_error(X[i:], np.mean(y[i:]))
78+
error = error_left + error_right
79+
if error < min_error:
80+
best_split = i
81+
min_error = error
82+
83+
if best_split != 0:
84+
left_X = X[:best_split]
85+
left_y = y[:best_split]
86+
right_X = X[best_split:]
87+
right_y = y[best_split:]
88+
89+
self.decision_boundary = X[best_split]
90+
self.left = Decision_Tree(depth = self.depth - 1, min_leaf_size = self.min_leaf_size)
91+
self.right = Decision_Tree(depth = self.depth - 1, min_leaf_size = self.min_leaf_size)
92+
self.left.train(left_X, left_y)
93+
self.right.train(right_X, right_y)
94+
else:
95+
self.prediction = np.mean(y)
96+
97+
return
98+
99+
def predict(self, x):
100+
"""
101+
predict:
102+
@param x: a floating point value to predict the label of
103+
the prediction function works by recursively calling the predict function
104+
of the appropriate subtrees based on the tree's decision boundary
105+
"""
106+
if self.prediction is not None:
107+
return self.prediction
108+
elif self.left or self.right is not None:
109+
if x >= self.decision_boundary:
110+
return self.right.predict(x)
111+
else:
112+
return self.left.predict(x)
113+
else:
114+
print("Error: Decision tree not yet trained")
115+
return None
116+
117+
def main():
118+
"""
119+
In this demonstration we're generating a sample data set from the sin function in numpy.
120+
We then train a decision tree on the data set and use the decision tree to predict the
121+
label of 10 different test values. Then the mean squared error over this test is displayed.
122+
"""
123+
X = np.arange(-1., 1., 0.005)
124+
y = np.sin(X)
125+
126+
tree = Decision_Tree(depth = 10, min_leaf_size = 10)
127+
tree.train(X,y)
128+
129+
test_cases = (np.random.rand(10) * 2) - 1
130+
predictions = np.array([tree.predict(x) for x in test_cases])
131+
avg_error = np.mean((predictions - test_cases) ** 2)
132+
133+
print("Test values: " + str(test_cases))
134+
print("Predictions: " + str(predictions))
135+
print("Average error: " + str(avg_error))
136+
137+
138+
if __name__ == '__main__':
139+
main()

searches/binary_search.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ def binary_search_by_recursion(sorted_collection, item, left, right):
110110
if sorted_collection[midpoint] == item:
111111
return midpoint
112112
elif sorted_collection[midpoint] > item:
113-
return binary_search_by_recursion(sorted_collection, item, left, right-1)
113+
return binary_search_by_recursion(sorted_collection, item, left, midpoint-1)
114114
else:
115-
return binary_search_by_recursion(sorted_collection, item, left+1, right)
115+
return binary_search_by_recursion(sorted_collection, item, midpoint+1, right)
116116

117117
def __assert_sorted(collection):
118118
"""Check if collection is sorted, if not - raises :py:class:`ValueError`

0 commit comments

Comments
 (0)