from scipy.io import arff
import numpy as np
import pandas as pd
radius = 4 # search radius
def distance(point1, point2):
return np.sqrt(np.sum([(point1[i] - point2[i]) ** 2 for i in range(4)]))
iris = arff.loadarff('../dataset/iris.arff')
df = pd.DataFrame(iris[0])
length = df.shape[0]
classes = list(set(df['class']))
classes_length = len(classes)
classes_dict = dict()
for i in range(classes_length):
classes_dict[classes[i]] = i
for i in range(length):
df.iloc[i, 4] = classes_dict[df.iloc[i, 4]]
if df.iloc[i, 4] != 0: # convert to an binary classification
df.iloc[i, 4] = 1
# df = df.sample(frac=1) # shuffle data randomly
train_data = df.iloc[0:100]
test_data = df.iloc[100:]
train_length = train_data.shape[0]
test_length = test_data.shape[0]
accuracy = 0
def my_iter(data, batch):
X = []
y = []
length = data.shape[0]
start = 0
while length - batch > 0:
X = data.iloc[start:start + batch, 0:4]
y = data.iloc[start:start + batch, 4]
yield X, y
start += batch
length -= batch
X = data.iloc[start:, 0:4]
y = data.iloc[start:, 4]
yield X, y
model_4 = np.random.rand(4)
learning_rate = 0.1
batch = 20
# predict
def h(x):
global model_4
return 1 / (1 + np.sum((np.exp(-model_4 * np.array(x))), axis=1))
# loss function
def J(x, y_true, batch):
global model_4
loss_sum = -np.sum(y_true * np.log(h(x)) + (1 - y_true) * np.log(1 - h(x)), axis=0)
derivative_loss_sum = np.sum(h(x) - y_true, axis=0)
return (1 / batch) * derivative_loss_sum, (1 / batch) * loss_sum
for i, (X, y) in enumerate(my_iter(train_data, batch)):
y_predict = np.array([1 if y_p >= 0.5 else 0 for y_p in h(X)])
delta, loss = J(X, y, X.shape[0])
print('loss ', loss)
print('acc ', np.sum(y_predict == y) / X.shape[0])
model_4 -= learning_rate * delta
print('test accuracy')
y_predict_test = np.array([1 if y_p >= 0.5 else 0 for y_p in h(test_data.iloc[:, 0:4])])
print('acc ', np.sum(y_predict_test == test_data.iloc[:, 4]) / test_length)