Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. import tensorflow as tf
  2. import numpy as np
  3. from keras.utils import to_categorical
  4. from model import create_model
  5. import matplotlib.pyplot as plt
  6.  
  7. colors = {
  8. 0:(0.11, 0.41, 0.00),
  9. 1:(0.33, 0.44, 0.55),
  10. 2:(0.66, 0.77, 0.88),
  11. 3:(0.99, 0.3, 0.2),
  12. 4:(0.65, 0.00, 0.00),
  13. 5:(0.00, 0.00, 0.3),
  14. 6:(0.52, 1.0, 0.1),
  15. 7:(0.0, 0.0, 1.0),
  16. 8:(0.54,0.78,0.1),
  17. 9:(0.54,0.12,0.1),
  18. 10:(0.0,0.24,0.87),
  19. 11:(0.87,0.0,0.87),
  20. 12:(0.24,0.1,0.71),
  21. 13:(0.9, 0.12, 0.71),
  22. 14:(0.94, 0.1, 0.37)
  23. }
  24.  
  25. def main():
  26. x_train,y_train,x_test,y_test,x_val,y_val,all_points,all_points_normal = load_data("clusters_file.txt")
  27. model = create_model(x_train,x_test,y_test,y_train,x_val,y_val)
  28. prediction = model.predict(all_points)
  29.  
  30. clusters = create_clusters(prediction)
  31.  
  32. for index,point in enumerate(all_points_normal):
  33. plt.scatter(int(point[0]), int(point[1]), color=colors[int(clusters[index])], marker='o')
  34.  
  35. plt.show()
  36.  
  37.  
  38. def create_clusters(predictions):
  39. clusters = []
  40. for point in predictions:
  41. clusters.append(np.argmax(point))
  42.  
  43. return clusters
  44.  
  45.  
  46.  
  47. def load_data(filename):
  48. x_train,y_train,x_test,y_test,x_val,y_val,all_points,all_points_normal = [],[],[],[],[],[],[],[]
  49. data = []
  50. with open(filename, 'r') as file:
  51. for line in file.readlines():
  52. data.append(list(map(float,line.split(','))))
  53. data = np.asarray(data)
  54. np.random.shuffle(data)
  55.  
  56. length = data.shape[0]
  57.  
  58. """Divide data"""
  59. train = data[:int(length *0.3)]
  60. test = data[int(length *0.3):int(length *0.4)]
  61. val = data[int(length *0.4):]
  62.  
  63. x_train, y_train = train[:, 0:2], train[:, 2]
  64. x_val, y_val = val[:, 0:2], val[:, 2]
  65. x_test, y_test = test[:, 0:2], test[:, 2]
  66.  
  67. all_points_normal = np.concatenate((x_test, x_train, x_val), axis=0)
  68.  
  69. y_train = to_categorical(y_train, num_classes=15)
  70. y_test = to_categorical(y_test, num_classes=15)
  71. y_val = to_categorical(y_val, num_classes=15)
  72.  
  73. """Normalize data"""
  74. normalization_parameter = 1000000
  75. x_train /= normalization_parameter
  76. x_val /= normalization_parameter
  77. x_test /= normalization_parameter
  78.  
  79. all_points = np.concatenate((x_test, x_train, x_val), axis=0)
  80.  
  81. return x_train,y_train,x_test,y_test,x_val,y_val,all_points,all_points_normal
  82.  
  83.  
  84. def load_model():
  85. pass
  86.  
  87. if __name__ == '__main__':
  88. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement