Machine Learning algorithms in Python
$ pip install PyML
Feel free to raise issues or add new features. A lot of work can be done on this project!
The first column of the training set x must be all 1s.
The following code is assumed in all the examples.
import numpy as np
import pyml
list_x = [[1, 1, 100], [1, 2, 101], [1, 3, 102]]
list_theta = [[0], [1], [0]]
list_y = [[1], [2], [3]]
x = np.matrix(list_x)
theta = np.matrix(list_theta)
y = np.matrix(list_y)
cost = pyml.linear_regression_cost(x, y, theta)
print('Cost: ', cost)
Cost: 0.0
list_x = [[1, 1, 100], [1, 2, 101], [1, 3, 102]]
x = np.matrix(list_x)
(x, mu, sigma) = pyml.normalize_features(x)
print('Normalized x: ', x)
Normalized x: [[ 1. -1.22474487 -1.22474487]
[ 1. 0. 0. ]
[ 1. 1.22474487 1.22474487]]
# load(file, sep=',')
# save(file, x, mode='w', sep=',')
x = pyml.load('data1')
pyml.save('data2', x)
y = pyml.load('data2')
print((x == y).all())
True
list_x = [[1, 1], [1, 2], [1, 3]]
list_y = [[10], [20], [30]]
x = np.matrix(list_x)
y = np.matrix(list_y)
(x, mu, sigma) = pyml.normalize_features(x)
theta = pyml.get_theta(x, y)
print('Theta: ', theta)
Theta: [[ 20. ]
[ 8.16496581]]
list_x = [[1, 1], [1, 2], [1, 3]]
list_theta = [[0], [0]]
list_y = [[10], [20], [30]]
x = np.matrix(list_x)
theta = np.matrix(list_theta)
y = np.matrix(list_y)
(x, mu, sigma) = pyml.normalize_features(x)
alpha = 0.03
num_iters = 2000
theta = pyml.gradient_descent(x, y, theta, alpha, num_iters)
print('Theta: ', theta)
Theta: [[ 20. ]
[ 8.16496581]]
Note: gradient_descent_history(x, y, theta, alpha, num_iters)
returns a tuple in which the first element is theta and the second element is a list with cost history from all the iterations.
list_x = [[1, 1], [1, 2], [1, 3]]
list_theta = [[0], [0]]
list_y = [[10], [20], [30]]
x = np.matrix(list_x)
theta = np.matrix(list_theta)
y = np.matrix(list_y)
(x, mu, sigma) = pyml.normalize_features(x)
alpha = 0.03
num_iters = 2000
theta = pyml.gradient_descent(x, y, theta, alpha, num_iters)
list_vals = [[1, 4], [1, 5]]
vals = np.matrix(list_vals)
predictions = pyml.predict(vals, theta, mu, sigma)
print('Predictions: ', predictions)
Predictions: [[ 40.]
[ 50.]]
list_y = [[10], [20], [30]]
s = pyml.sigmoid(y)
print('Sigmoid: ', s)
print('Sigmoid: ', list(map(pyml.sigmoid, [float('-inf'), -1, 0, 1, float('inf')])))
Sigmoid: [[ 0.9999546 1. 1. ]] # Returns np.matrix for iterables
Sigmoid: [0.0, 0.2689414213699951, 0.5, 0.7310585786300049, 1.0] # Returns float for int/float