def createHistogram(bins, positiveData, negativeData, minmax1, minmax2): # Visible Deprecation Warning bins = int(bins) positiveHisto = np.zeros((bins, bins), dtype=int) negativeHisto = np.zeros((bins, bins), dtype=int) c1_Range = minmax1[1] - minmax1[0] c2_Range = minmax2[1] - minmax2[0] # Positive Data for p in positiveData: rowP = int(round((bins - 1) * ((p[0] - minmax1[0]) / c1_Range))) colP = int(round((bins - 1) * ((p[1] - minmax2[0]) / c2_Range))) positiveHisto[rowP][colP] += 1 # Negative Data for n in negativeData: rowN = int(round((bins - 1) * ((n[0] - minmax1[0]) / c1_Range))) colN = int(round((bins - 1) * ((n[1] - minmax2[0]) / c2_Range))) negativeHisto[rowN][colN] += 1 #============================================================================== # for r in range(len(negativeHisto)): # for c in range(negativeHisto.shape[0]): # print(negativeHisto[r][c], end = " ") # print() #============================================================================== return [positiveHisto, negativeHisto]
def breadth_first(graph, start=0): # instatiating the queue data structure # and add our graph's first node to it queue = Queue() queue.put(start) # create a variable to keep track all of vertices that visited (e.g traversed) # it ensures us to do not visit a node more than once in a traverse visited = np.zeros(graph.numVertices) while not queue.empty(): vertex = queue.get() # if the node that we just accesed in the queue, # has already visited before, then we simply do nothing! if visited[vertex] == 1: continue # print the node that we just visited print("Visit: ", vertex) # and add it to the list of visited vertices # setting visited[i] to one to indicate that, # this node has been visited visited[vertex] = 1 # after traversing some node, we need to access all it's neighbors ! for v in graph.get_adjacent_vertices(vertex): # if they are haven't been visited ... if visited[v] != 1: # so visit them ! # Sele E Rahem xD queue.put(v)
def kMeans(X, K=10, iterationLimit=1000, tol=10e-6): """Runs kmeans clustering.""" # X shape is (10, 43) muK = list() # Initializing d-dimensional random mean data for i in range(K): randomMean = list() for j in range(X.shape[1]): jMin = np.min(X[:, j]) jMax = np.max(X[:, j]) randomMean.append(np.random.uniform(jMin, jMax)) muK.append(randomMean) muK = np.array(muK) # (10, 43) # Initializing class labels and other parameters c = np.zeros([X.shape[0], 1]) c = c.astype(int) error = 10000 iterations = 0 while (iterations < iterationLimit and error > tol): # featureVector is number of observations # Assign points to a class label based on distance to centroid points for featureVector in range(X.shape[0]): kmin = math.inf kIndex = 0 # kr = clusters number for kr in range(K): dist = np.power(X[featureVector] - muK[kr], 2) meanSum = dist.sum() if (meanSum < kmin): kmin = meanSum kIndex = kr c[featureVector][0] = kIndex # Update Means Step tmpError = 0 for kr2 in range(K): countIndex = np.where(c == kr2) if (countIndex[0].shape[0] > 0): indexMatch = countIndex[0] finalList = X[indexMatch] #muK2 = np.sum(finalList, axis=0)/np.sum(finalList) muK2 = np.mean(finalList, axis=0) tmpError2 = np.max(np.absolute(muK[kr2] - muK2)) if (tmpError < tmpError2): tmpError = tmpError2 muK[kr2] = muK2 error = tmpError iterations += 1 print(iterations, " ", error) return c
def create_hist(input_data, max_1, min_1, slot_1, max_2, min_2, slot_2): i = 0 hist = np.zeros((bin_size, bin_size)) # print(hist) for h in np.arange(min_1, max_1, slot_1): j = 0 for s in np.arange(min_2, max_2, slot_2): temp_data = input_data[(np.where(np.logical_and(h <= input_data[:, 0], input_data[:, 0] < h + slot_1)))] hist[i][j] = temp_data[(np.where(np.logical_and(s <= temp_data[:, 1], temp_data[:, 1] < s + slot_2)))][:, 0].size j += 1 i += 1 return hist
#mplot.scatter(x,y) #plt.show() # Constructing 2D Histograms #define Bin Size B = 25 # Compute min and max of 2 principle components Pmin1 = min(P[:,0]) Pmax1 = max(P[:,0]) Pmin2 = min(P[:,1]) Pmax2 = max(P[:,1]) #Initialize Histogram to Zeroes Hp = np.zeros((B,B)) Hn = np.zeros((B,B)) #print (Hp, Hn) P6 = PosP[:,0] P6_2 = PosP[:,1] P9 = NegP[:,0] P9_2 = NegP[:,1] P1 = P[:,0] P2 = P[:,1] for (T,p1,p2) in zip(T,P1,P2): row = int((B-1)*((p1-Pmin1)/(Pmax1-Pmin1))) col = int((B-1)*((p2-Pmin2)/(Pmax2-Pmin2)))
####################################################################### # Test Area # with an example we can examine how breadth_first and # depth_first traversal works, # with instantiating the "AdjacencyMatrixGraph" class # for testing the "depth_first" function, # simply change the traversal method call ! ####################################################################### g = AdjacencyMatrixGraph(9) g.add_edge(0, 1) g.add_edge(1, 2) g.add_edge(2, 7) g.add_edge(2, 4) g.add_edge(2, 3) g.add_edge(1, 5) g.add_edge(5, 6) g.add_edge(6, 3) g.add_edge(3, 4) g.add_edge(6, 8) breadth_first(g, 2) # testing the depth_first algorithm # depth_first needs a visited parameter, # so we create a empty one! visited = np.zeros(g.numVertices) depth_first(g, visited)