• API
• FAQ
• Tools
• Archive
SHARE
TWEET Untitled a guest Feb 20th, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
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] == 'cp':
21.             ecoli_data[i] = '1'
22.         elif ecoli_data[i] == 'im':
23.             ecoli_data[i] = '0'
24.         else:
25.             pass
26.
27.     dataset = []
28.     for i in range(len(ecoli_data)):
29.         if (ecoli_data[i] == '1') or (ecoli_data[i] == '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 preprocessed_dataset, train_data, test_data
43.
44. weights = *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
56.
57.     activation_1 += weights * row
58.     activation_1 += weights * row
59.     activation_1 += weights * row
60.
61.     activation_2 = weights
62.
63.     activation_2 += weights * row
64.     activation_2 += weights * row
65.     activation_2 += weights * row
66.     activation_2 += weights * row
67.     return sigmoid(activation_1),sigmoid(activation_2)
68.
69.
70. def secondLayer(row,weights):
71.     activation_3 = weights
72.     activation_3 += weights * row
73.     activation_3 += weights * row
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))]
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 = weights + learningrate * error
95.             weights = weights + learningrate * error
96.             weights = weights + learningrate * error * row
97.             weights = weights + learningrate * error * row
98.             weights = weights + learningrate * error * row
99.             weights = weights + learningrate * error * row
100.             weights = weights + learningrate * error * row
101.             weights = weights + learningrate * error * row
102.             weights = weights + learningrate * error * row
103.
104.             # Second layer
105.             weights = weights + learningrate * error
106.             weights = weights + learningrate * error * first_layer
107.             weights = weights + learningrate * error * first_layer
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),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," Real value:", row[-1])
132.     print("Error:",prediction-row[-1])
133.     if(round(prediction)==row[-1]):
134.         accuracy += 1
135.
136.
137. accuracy = accuracy/len(test_data)
138. print("Accurary",accuracy)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top