Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. from io import StringIO
  2. import re
  3. import pandas as pd
  4. import csv
  5. import string
  6. import numpy as np
  7. import random
  8. import math
  9.  
  10.  
  11. def preprocess():
  12. f = open('ecoli.data', 'r')
  13.  
  14. ecoli_data = []
  15. for line in f:
  16. row = line.split()
  17. ecoli_data.append(row)
  18.  
  19. for i in range(len(ecoli_data)):
  20. if ecoli_data[i][8] == 'cp':
  21. ecoli_data[i][8] = '1'
  22. elif ecoli_data[i][8] == 'im':
  23. ecoli_data[i][8] = '0'
  24. else:
  25. pass
  26.  
  27. dataset = []
  28. for i in range(len(ecoli_data)):
  29. if (ecoli_data[i][8] == '1') or (ecoli_data[i][8] == '0'):
  30. dataset.append(ecoli_data[i])
  31.  
  32. for i in range(len(dataset)):
  33. dataset[i].pop(0)
  34.  
  35. preprocessed_dataset = [[float(x) for x in lst] for lst in dataset]
  36.  
  37. random.shuffle(preprocessed_dataset)
  38.  
  39. train_data = preprocessed_dataset[:70]
  40. test_data = preprocessed_dataset[30:]
  41.  
  42. return train_data, test_data, preprocessed_dataset
  43.  
  44. weights = [0]*12
  45. weights = [random.uniform(-1, 1) for i in weights]
  46.  
  47. def sigmoid(z):
  48. if (z < -100):
  49. return 0
  50. if (z > 100):
  51. return 1
  52. return 1.0 / (1 + math.exp(-z))
  53.  
  54. def firstLayer(row, weights):
  55. activation_1 = weights[0]
  56.  
  57. activation_1 += weights[1] * row[0]
  58. activation_1 += weights[2] * row[1]
  59. activation_1 += weights[3] * row[2]
  60.  
  61. activation_2 = weights[4]
  62.  
  63. activation_2 += weights[5] * row[3]
  64. activation_2 += weights[6] * row[4]
  65. activation_2 += weights[7] * row[5]
  66. activation_2 += weights[8] * row[6]
  67. return sigmoid(activation_1),sigmoid(activation_2)
  68.  
  69.  
  70. def secondLayer(row,weights):
  71. activation_3 = weights[9]
  72. activation_3 += weights[10] * row[0]
  73. activation_3 += weights[11] * row[1]
  74. return sigmoid(activation_3)
  75. #return 1.0 if activation_3 >= 0.0 else 0.0
  76.  
  77. def predict(row,weights):
  78. first_layer = firstLayer(row,weights)
  79. second_layer = secondLayer(first_layer,weights)
  80. return second_layer,first_layer
  81.  
  82. def train_weights(train, learningrate, epochs):
  83. #weights = [random.uniform(-1,1) for i in range(len(train[0]))]
  84. last_error = 0.0
  85. for epoch in range(epochs):
  86. sum_error = 0.0
  87. for row in train:
  88. prediction,first_layer = predict(row[:-1],weights)
  89. error = row[-1]-prediction
  90. #print(error)
  91. sum_error += error**2#abs(error)#math.abs(error)#**2**0.5
  92.  
  93. # First layer
  94. weights[0] = weights[0] + learningrate * error
  95. weights[4] = weights[4] + learningrate * error
  96. weights[1] = weights[1] + learningrate * error * row[0]
  97. weights[2] = weights[2] + learningrate * error * row[1]
  98. weights[3] = weights[3] + learningrate * error * row[2]
  99. weights[5] = weights[5] + learningrate * error * row[3]
  100. weights[6] = weights[6] + learningrate * error * row[4]
  101. weights[7] = weights[7] + learningrate * error * row[5]
  102. weights[8] = weights[8] + learningrate * error * row[6]
  103.  
  104. # Second layer
  105. weights[9] = weights[9] + learningrate * error
  106. weights[10] = weights[10] + learningrate * error * first_layer[0]
  107. weights[11] = weights[11] + learningrate * error * first_layer[1]
  108.  
  109. #for i in range(len(row)-1):
  110. # weights[i+1] = weights[i+1] + learningrate*error*row[i]
  111. if((epoch%100==0) or (last_error != sum_error)):
  112. print("Epoch "+str(epoch) + " Learning rate " + str(learningrate) + " Error " + str(sum_error))
  113. last_error = sum_error
  114. return weights
  115.  
  116. preprocessed_dataset, train_data, test_data = preprocess()
  117.  
  118. for row in preprocessed_dataset:
  119. print(predict(row[:-1],weights)[0],row[-1])
  120.  
  121.  
  122. learningrate = 0.01#0.00001
  123. epochs = 1000
  124. train_weights = train_weights(preprocessed_dataset,learningrate,epochs)
  125. print(train_weights)
  126.  
  127.  
  128. accuracy = 0.0
  129. for row in test_data:
  130. prediction = predict(row[:-1],weights)
  131. print("Prediction:",prediction[0]," Real value:", row[-1])
  132. print("Error:",prediction[0]-row[-1])
  133. if(round(prediction[0])==row[-1]):
  134. accuracy += 1
  135.  
  136.  
  137. accuracy = accuracy/len(test_data)
  138. print("Accurary",accuracy)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement