-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab16.py
55 lines (41 loc) · 1.28 KB
/
lab16.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from sklearn.preprocessing import MinMaxScaler, StandardScaler
import pandas as pd
import numpy as np
def sigmoid(x):
return 1 / (1 * np.e ** (-x))
df = pd.read_csv("iris.data", usecols=[0, 1, 2, 3],
names=["1", "2", "3", "4"])
data = []
for x in range(100):
data.append([df.iloc[x, 0], df.iloc[x, 1]])
averages = []
for j in range(100):
np.random.shuffle(data)
f10 = data[0:10]
sample = data[10:100]
trainX = sample
trainy = np.array([
0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1
])
testX = f10
testy = np.array([0, 1, 0, 1, 0,
1, 0, 1, 0, 1])
w = np.zeros(np.size(trainX, 1))
# print(w)
lr = 0.05
for i in range(5000):
w_diff = np.dot(np.transpose(trainy - sigmoid(np.dot(trainX, w))), trainX)
w = w * lr * w_diff
avg = sum(np.round(sigmoid(np.dot(testX, w))) == testy) / np.size(testy)
print(avg)
averages.append(avg)
print("Total avg: ", np.average(averages))