Skip to content

Commit

Permalink
kNN initialization added
Browse files Browse the repository at this point in the history
  • Loading branch information
metalcyanide committed Dec 15, 2019
1 parent c99c51f commit 3ea00ed
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
41 changes: 41 additions & 0 deletions kNN.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import random
# Choose one center uniformly at random from among the data points.
# For each data point x, compute D(x), the distance between x and the nearest center that has already been chosen.
# Choose one new data point at random as a new center, using a weighted probability distribution where a point x is chosen with probability proportional to D(x)2.
# Repeat Steps 2 and 3 until k centers have been chosen.
# Now that the initial centers have been chosen, proceed using standard k-means clustering.

#kmeans++ initialization of labels. data being a dictionary
def kMeansPPlusInitalization(data, distancematrix, numclusters):
initialpoints = dict()
initialpoints[numclusters] = [random.sample(data.keys(),1)]
numclusters -=1
while numclusters:
initialpoints[numclusters] = getNextPoint(data.keys(), distancematrix, initialpoints)
numclusters -=1

return initialpoints



def getNextPoint(keys, distancematrix, initialpoints):
genDistanceDict = dict()
for i in keys:
minimum = 999999999
if i in initialpoints.values():
continue
for j in initialpoints.values():
if(distancematrix[i][j] < minimum):
minimum = distancematrix[i][j]
genDistanceDict[i] = minimum

#code for prob. pick to be written




#nearestPoints be the list of list of two elements with point and label
def classifyPoints(nearestPoints, givenPoint):



14 changes: 8 additions & 6 deletions testmaps.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import googlemaps

def getnodedata(file_location):
def getNodeData(file_location):
f = open(file_location)
lines = f.readlines()
placesToVisit = []
Expand All @@ -22,7 +22,7 @@ def getnodedata(file_location):
def genDistanceMatrix(file_location = '/home/metalcyanide/cp/sample.txt', api_key):
gmaps = googlemaps.Client(key=api_key)
placeDict = dict()
placeDict = getnodedata(file_location)
placeDict = getNodeData(file_location)
print(placeDict.keys())
distancedict = dict()
# return 0
Expand All @@ -41,7 +41,9 @@ def genDistanceMatrix(file_location = '/home/metalcyanide/cp/sample.txt', api_ke

return distancedict

apikey = "YOUR_API_KEY"
mydict = dict()
mydict = genDistanceMatrix(apikey)
print(mydict)

def __init__ == main :
apikey = "YOUR_API_KEY"
mydict = dict()
mydict = genDistanceMatrix(apikey)
print(mydict)

0 comments on commit 3ea00ed

Please sign in to comment.