Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions learning.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,8 @@ def LinearLearner(dataset, learning_rate=0.01, epochs=100):
examples = dataset.examples
num_examples = len(examples)

# X transpose
X_col = [dataset.values[i] for i in idx_i] # vertical columns of X
# X transpose: the actual value of each input feature across the examples
X_col = [[example[i] for example in examples] for i in idx_i] # vertical columns of X

# add dummy
ones = [1 for _ in range(len(examples))]
Expand All @@ -526,7 +526,7 @@ def LinearLearner(dataset, learning_rate=0.01, epochs=100):
err = []
# pass over all examples
for example in examples:
x = [1] + example
x = [1] + [example[i] for i in idx_i]
y = np.dot(w, x)
t = example[idx_t]
err.append(t - y)
Expand All @@ -536,7 +536,7 @@ def LinearLearner(dataset, learning_rate=0.01, epochs=100):
w[i] = w[i] + learning_rate * (np.dot(err, X_col[i]) / num_examples)

def predict(example):
x = [1] + example
x = [1] + [example[i] for i in idx_i]
return np.dot(w, x)

return predict
Expand All @@ -552,8 +552,8 @@ def LogisticLinearLeaner(dataset, learning_rate=0.01, epochs=100):
examples = dataset.examples
num_examples = len(examples)

# X transpose
X_col = [dataset.values[i] for i in idx_i] # vertical columns of X
# X transpose: the actual value of each input feature across the examples
X_col = [[example[i] for example in examples] for i in idx_i] # vertical columns of X

# add dummy
ones = [1 for _ in range(len(examples))]
Expand All @@ -568,7 +568,7 @@ def LogisticLinearLeaner(dataset, learning_rate=0.01, epochs=100):
h = []
# pass over all examples
for example in examples:
x = [1] + example
x = [1] + [example[i] for i in idx_i]
y = sigmoid(np.dot(w, x))
h.append(sigmoid_derivative(y))
t = example[idx_t]
Expand All @@ -580,7 +580,7 @@ def LogisticLinearLeaner(dataset, learning_rate=0.01, epochs=100):
w[i] = w[i] + learning_rate * (np.dot(buffer, X_col[i]) / num_examples)

def predict(example):
x = [1] + example
x = [1] + [example[i] for i in idx_i]
return sigmoid(np.dot(w, x))

return predict
Expand Down
8 changes: 8 additions & 0 deletions tests/test_learning.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ def test_neural_network_learner():
assert err_ratio(nnl, iris) < 0.21


def test_linear_learner():
iris = DataSet(name="iris")
iris.classes_to_numbers()
# both linear learners should train and produce a numeric prediction without shape errors
assert isinstance(float(LinearLearner(iris)(iris.examples[0][:-1])), float)
assert isinstance(float(LogisticLinearLeaner(iris)(iris.examples[0][:-1])), float)


def test_perceptron():
iris = DataSet(name='iris')
iris.classes_to_numbers()
Expand Down
Loading