Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.67 KB | None | 0 0
  1.  
  2. # Program with python
  3. #Authors: Bartosz Pacia, Jakub Szafraniak, Grzegorz Musiał
  4.  
  5. import numpy as np
  6. from math import sqrt
  7.  
  8.  
  9. # function to get unique values
  10. def unique(list1):
  11.     x = np.array(list1)
  12.     return np.unique(x)
  13.  
  14. def load_data_from_file(file):
  15.     data = open(file, "r").read()
  16.     data = data.split('\n')
  17.     return data
  18.  
  19. def convert_to_float(data):
  20.     data_tmp = []
  21.     for line in data:
  22.         data_tmp.append(line.split(","))
  23.     data = []
  24.     for line in data_tmp:
  25.         list_tmp = [float(num) for num in line[1:-1]]
  26.         list_tmp.append(line[-1])
  27.         data.append(list_tmp)
  28.     return data
  29.  
  30. # Append only last column
  31. def only_last_column_append(data):
  32.     tmp = []
  33.     for line in data:
  34.         tmp.append(line[-1])
  35.     return tmp
  36.  
  37. # Calculate the Euclidean distance between two vectors
  38. def euclidean_distance(row1, row2):
  39.     distance = 0.0
  40.     for i in range(len(row1) - 1):
  41.         distance += (row1[i] - row2[i]) ** 2
  42.     return sqrt(distance)
  43.  
  44.  
  45. def get_neighbors(train, test_row, num_neighbors):
  46.     distances = list()
  47.     for train_row in train:
  48.         dist = euclidean_distance(test_row, train_row)
  49.         distances.append((train_row, dist))
  50.     distances.sort(key=lambda tup: tup[1])
  51.     neighbors = list()
  52.     for i in range(num_neighbors):
  53.         neighbors.append(distances[i][0])
  54.     return neighbors
  55.  
  56. #-------------------Configurations-------------------
  57.  
  58. #K neares neighbours
  59. K= input("Value K: ")
  60. K = int(K, 10)
  61.  
  62. # New data
  63. new_object = [1,1,1,1,1,1,1,0,0,0,1,0,1,1,0,0]
  64.  
  65. #-------------------End configurations-------------------
  66.  
  67.  
  68. # Load data from file and convert numbers to float
  69. data = load_data_from_file("zoo.txt")
  70. data = convert_to_float(data)
  71.  
  72. # Show decision class
  73. classes = only_last_column_append(data)
  74. print("\nClasses object in file:")
  75. classes = unique(classes)
  76. print(classes)
  77.  
  78. # Only vector explanatory designs
  79. data_only_vector = []
  80. for line in data:
  81.     data_only_vector.append(line[:-1])
  82.  
  83. # Find nearest neighbors
  84. nearest_objects = get_neighbors(data_only_vector, new_object, K)
  85.  
  86. # Show objects - find neares neighbors (portion information)
  87. print("\nObject found(only vector):")
  88. for line in nearest_objects:
  89.     print(line)
  90.  
  91. # Find full object
  92. near_full_obj = []
  93. for line in nearest_objects:
  94.     index = data_only_vector.index(line)
  95.     near_full_obj.append(data[index])
  96.  
  97. # Show full objects
  98. print("\nShow ful information object:")
  99. for line in near_full_obj:
  100.     print(line)
  101.  
  102. only_classes = []
  103. for line in near_full_obj:
  104.     only_classes.append(line[-1])
  105.  
  106. print("\nNew object:", new_object)
  107. print("Class new object: ", max(only_classes, key=only_classes.count))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement