Nexclap

KNN(K Nearest Neighbors)

This is my final version of K Nearest Neighbors. I still may need to add more points.


from statistics import mode from math import sqrt import operator data = [(1,1,"red"),(2,4,"red"),(6,3,"red"),(7,4,"blue"),(4,2,"blue"),(3,4,"green")] x = int(input("What is the x value?")) y = int(input("What is the y value?")) k = int(input("How many closest points would you like?")) point = (x,y) print(point) def distance(x2,y2): dist = ((x2[0]-x)**2)+((y2[1]-y)**2) return sqrt(dist) if k>len(data): print("Enter a different number of points") Dictionary = {} for i in range(0,len(data)): var = distance(data[i],data[i]) print(var) Dictionary[data[i]]=var sorted_Dictionary = sorted(Dictionary.items(), key=operator.itemgetter(1)) print(sorted_Dictionary) Dict = {} start = 0 var2 = [] for i in range(0,k): var1 = (sorted_Dictionary[i][0][2]) Dict[start] = var1 start = start + 1 for i in range(0,k): var2.append(Dict[i]) print(var2) def most_frequent(var2): return max(set(var2), key = var2.count) print("Your color is:") print(most_frequent(var2))

Application Focus

The posts I have seen have been great and many of them are focused on algorithms - correlation between stocks, decision trees, k nearest neighbors. I wanted to start a thread regarding where your work can be applied to. When I think of the k nearest neighbors algorithm, I think of pattern recognition. Using data to predict how often you make large purchases, how weather conditions will affect the extent of your allergies, etc. As you progress to more complex algorithms like SVM and K Means Clustering, let us know what cases you see these algorithms being used.

KNN

This is my KNN code


import math data = [(3,5,"red"),(5,5,"blue"),(520,505,"green"),(1,10,"red"),(1,2,"blue"),(2,3,"green"),(3,1,"purple")] def distance(set1,set2): distancelist=[] for x in range(0,len(data)-1): distance = (math.sqrt((data[set2][0]-data[set1][0])**2+(data[set2][1]-data[set1][1])**2)) distancelist.append(distance) set2=set2+1 set1=set1+1 return(distancelist) def knn(oddNum): newlist=[] minimum = 900 if(oddNum%2==1): for x in range(0,oddNum): for y in range(0,1): var1 = 0 var2 = 1 temp = distance(var1,var2) if(temp[x]<minimum): newlist.append(data[var2]) data.remove(data[var2]) x=x-1 minimum=900 var1=var1+1 var2=var2+1 print(newlist) for color in newlist: countb = 0 countr = 0 countg = 0 if(color[2]=="blue"): countb=countb+1 elif(color[2]=="red"): countr=countr+1 elif(color[2]=="green"): countg=countg+1 if(countb>=countr or countb>=countg): print("the color that this point has will most likely be blue") break elif(countr>=countb or countr>=countg): print("the color that this point has will most likely be red") break elif(countg>=countb or countg>=countr): print("the color that this point has will most likely be green") break #calling the funtion knn(1)
Announcements
  • Test Announcement from Nexclap

    Test message from nexclap - please ignore

    2023-02-13

  • Test message

    Test message from nexclap - please ignore

    2023-02-11

  • Test message

    Test message from nexclap - please ignore

    2023-02-11

  • View all