def fit(self, x_train, y_train):
		"""
		Fit the model according to the given training data.
		Args:
			x_train: a 1d or 2d numpy ndarray for the samples
			y_train: a scalar or a numpy ndarray for the correct labels
		Returns: 
			self : object
			None on any error.
		Raises:
			This method should not raise any Exception.
		"""
		# self.model.fit(x_train, y_train)
		# self.thetas = np.hstack((self.model.intercept_[:,None], self.model.coef_)).reshape(-1,1)
		# return self.thetas
		# Your code here
		thetas = np.zeros((x_train.shape[1] + 1,1))
		np.insert(x_train, 0, 1,axis=1)
		
		m = x_train.shape[0]
		for i in range(5):
		    # print("cost: {}".format(vec_log_loss_(y_train,y_pred,m)),end='\r')
			y_pred = sigmoid_(x_train.dot(thetas))
			grad = vec_log_gradient_(x_train,y_train,y_pred)
			thetas = thetas - self.alpha * 0.5 * (1./m) * grad
			if i % 150 == 0 and self.verbose == True:
			    print("epoch {}     : loss {}".format(i,vec_log_loss_(y_train,y_pred,m)))4
			self.thetas = thetas
		return self.thetas
Beispiel #2
0
def logistic_predict(x, theta):
	"""Computes the vector of prediction y_hat from two non-empty numpy.ndarray.
	Args:
	x: has to be an numpy.ndarray, a vector of dimension m * n.
	theta: has to be an numpy.ndarray, a vector of dimension (n + 1) * 1.
	Returns:
	y_hat as a numpy.ndarray, a vector of dimension m * 1.
	None if x or theta are empty numpy.ndarray.
	None if x or theta dimensions are not appropriate.
	Raises:
	This function should not raise any Exception.
	"""
	if len(x) < 1 or len(theta) < 1 or x is None or theta is None:
		return None
	return sigmoid_(np.matmul(add_intercept(x), theta))
	def predict(self, x_train):
		"""
		Predict class labels for samples in x_train.
		Arg:
			x_train: a 1d or 2d numpy ndarray for the samples
		Returns: 
			y_pred, the predicted class label per sample.
			None on any error.
		Raises:
			This method should not raise any Exception.
		"""
		# Your code here
		#without intercept column
		x_train = np.insert(x_train, 0, 1,axis=1)
		return sigmoid_((x_train.dot(self.thetas)))
Beispiel #4
0
def vec_reg_logistic_grad(y, x, theta, lambda_):
    """Computes the regularized linear gradient of three non-empty numpy.ndarray, without any for-loop. The three arrays must have compatible dimensions."""
    if isinstance(x, np.ndarray) == 1 and isinstance(
            y, np.ndarray) == 1 and isinstance(theta, np.ndarray) == 1:
        if isinstance(lambda_, (int, float)) == 1:
            if len(y) == len(x) and len(x[0]) == len(theta):
                y_pred = sigmoid_(np.dot(x, theta))
                temp = np.dot(x.T, (y_pred - y)) / len(x)
                res = np.dot(x.T, (y_pred - y)) / len(x) + np.dot(
                    lambda_ / len(x), theta)
                res[0] = temp[0]
                return (res)
            else:
                print("vec_reg_log_gradient: error in size of x, y or theta")
        else:
            print("vec_reg_log_gradient: lamda is not a scalar")
    else:
        print("vec_reg_log_gradient: error in type of x, y or theta")
Beispiel #5
0
def logistic_predict(x, theta):
    """Computes the vector of prediction y_hat from two non-empty numpy.ndarray.
    Args:
    x: has to be an numpy.ndarray, a vector of dimension m * n.
    theta: has to be an numpy.ndarray, a vector of dimension (n + 1) * 1.
    Returns:
    y_hat as a numpy.ndarray, a vector of dimension m * 1.
    None if x or theta are empty numpy.ndarray.
    None if x or theta dimensions are not appropriate.
    Raises:
    This function should not raise any Exception.
    """
    if x.ndim == 1:
        x = x.reshape(len(x), 1)
        print(x.shape, theta.shape)
    intercept = np.ones((x.shape[0], 1))
    x = np.append(intercept, x, axis=1)
    return sigmoid_(x.dot(theta))
Beispiel #6
0
def reg_logistic_grad(y, x, theta, lambda_):
    """Computes the regularized linear gradient of three non-empty numpy.ndarray, with two for-loop. The three arrays must have compatible dimensions."""

    if isinstance(y, np.ndarray) == 1 and isinstance(x, np.ndarray) == 1 and isinstance(theta, np.ndarray) == 1:
        if isinstance(lambda_, (int, float)) == 1:
            if len(x) == len(y):
                if len(x[0]) == len(theta):
                    y_pred = sigmoid_(np.dot(x, theta))
                    res = np.zeros(len(x[0]))
                    res[0] = sum((y_pred - y) * (x[:,0])) / len(x)
                    for j in range(1,len(theta)):
                        res[j] = sum((y_pred - y) * (x[:,j]) + lambda_ * theta[j]) / len(x)
                    return(res)
                else:
                    print("reg_log_grad: x's columns is not equel to theta's lines")
            else:
                print("reg_log_grad: lamda not a float / int")
        else:
            print("reg_log_grad: x and y do not have the same number of lines")
    else:
        print("reg_log_grad: At least one argument is not a np.ndaray")
Beispiel #7
0
    if y_true.size > 1:
        x = np.transpose(x)
        for i, row in enumerate(x):
            sigma.append(np.sum((y_pred - y_true).dot(x[i])))
        return sigma
    for i, row in enumerate(x):
        sigma.append(np.sum((y_pred - y_true) * x[i]))
    return np.array(sigma)


if __name__ == "__main__":
    x = [1, 4.2]  # 1 represent the intercept
    y_true = 1
    theta = [0.5, -0.5]
    x_dot_theta = sum([a * b for a, b in zip(x, theta)])
    y_pred = sigmoid_(x_dot_theta)
    print(log_gradient_(x, y_pred, y_true))
    x = [1, -0.5, 2.3, -1.5, 3.2]
    y_true = 0
    theta = [0.5, -0.5, 1.2, -1.2, 2.3]
    x_dot_theta = sum([a * b for a, b in zip(x, theta)])
    y_pred = sigmoid_(x_dot_theta)
    print(log_gradient_(x, y_true, y_pred))
    x_new = [[1, 2, 3, 4, 5], [1, 6, 7, 8, 9], [1, 10, 11, 12, 13]]
    # first column of x_new are intercept values initialized to 1
    y_true = [1, 0, 1]
    theta = [0.5, -0.5, 1.2, -1.2, 2.3]
    x_new_dot_theta = []
    for i in range(len(x_new)):
        my_sum = 0
        for j in range(len(x_new[i])):
import numpy as np
from sigmoid import sigmoid_
from vec_log_loss import vec_log_loss_
# x = 4
# y_true = 1
# theta = 0.5
# y_pred = sigmoid_(x * theta)
# m = 1
# # length of y_true is 1
# print(vec_log_loss_(y_true, y_pred, m))    # 0.1269280110429715

x = np.array([1, 2, 3, 4])
y_true = 0
theta = np.array([-1.5, 2.3, 1.4, 0.7])
y_pred = sigmoid_(np.dot(x, theta))
m = 1
print(vec_log_loss_(y_true, y_pred, m))

# x_new = np.arange(1, 13).reshape((3, 4))
# y_true = np.array([1, 0, 1])
# theta = np.array([-1.5, 2.3, 1.4, 0.7])
# y_pred = sigmoid_(np.dot(x_new, theta))
# m = len(y_true)
# print(vec_log_loss_(y_true, y_pred, m))
        This function should not raise any Exception.
"""
    if m > 1:
        if y_true.size != m or y_pred.size != y_true.size:
            return (None)
        return (-1 / m * (np.dot(y_true, np.log(y_pred)) + np.dot(
            (1 - y_true), np.log(1 - y_pred))))
    return (y_true * np.log(y_pred + eps) + (1 - y_true) * np.log(1 - y_pred))


if __name__ == "__main__":
    # Test n.1
    x = 4
    y_true = 1
    theta = 0.5
    y_pred = sigmoid_(x * theta)
    m = 1  # length of y_true is 1
    print(vec_log_loss_(y_true, y_pred, m))
    # 0.12692801104297152

    # Test n.2
    x = np.array([1, 2, 3, 4])
    y_true = 0
    theta = np.array([-1.5, 2.3, 1.4, 0.7])
    y_pred = sigmoid_(np.dot(x, theta))
    m = 1
    print(vec_log_loss_(y_true, y_pred, m))
    # 10.100041078687479

    # Test n.3
    x_new = np.arange(1, 13).reshape((3, 4))
def vec_log_gradient(x, y, theta):
    x_pr = add_intercept(x)
    m = y.shape[0]
    sig = sigmoid_(np.dot(x_pr, theta))
    return (1 / m) * (np.dot(x_pr.T, (sig - y)))
Beispiel #11
0
import numpy as np
from sigmoid import sigmoid_

# Example 1:
x = np.array([-4])
print(sigmoid_(x))
# Output: array([0.01798620996209156])

# Example 2:
x = np.array([2])
print(sigmoid_(x))
# Output: array([0.8807970779778823])

# Example 3:
x = np.array([[-4], [2], [0]])
print(sigmoid_(x))
# Output:
# array([[0.01798620996209156], [0.8807970779778823], [0.5]])
# theta = 0.5
# y_pred = sigmoid_(x * theta)
# m = 1
# length of y_true is 1
# print(log_loss_(y_true, y_pred, m))
# 0.12692801104297152
# # Test n.2
# x = [1, 2, 3, 4]
# y_true = 0
# theta = [-1.5, 2.3, 1.4, 0.7]
# x_dot_theta = sum([a*b for a, b in zip(x, theta)])
# y_pred = sigmoid_(x_dot_theta)
# m = 1
# print(log_loss_(y_true, y_pred, m))
# # 10.100041078687479
# # # Test n.3
x_new = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
y_true = [1, 0, 1]
theta = [-1.5, 2.3, 1.4, 0.7]
x_dot_theta = []
for i in range(len(x_new)):
    my_sum = 0
    # for j in range(len(x_new[i])):
    #     my_sum += x_new[i][j] * theta[j]
    my_sum = sum([a * b for a, b in zip(x_new[i], theta)])
    x_dot_theta.append(my_sum)
y_pred = sigmoid_(x_dot_theta)
m = len(y_true)
print(log_loss_(y_true, y_pred, m))
#         # 7.233346147374828
Beispiel #13
0
import numpy as np
from sigmoid import sigmoid_
from vec_log_loss import vec_log_loss_

# Test n.1
x = 4
y_true = 1
theta = 0.5
y_pred = sigmoid_(x * theta)
m = 1  # length of y_true is 1
print(vec_log_loss_(y_true, y_pred, m))
# 0.12692801104297152

# Test n.2
x = np.array([1, 2, 3, 4])
y_true = 0
theta = np.array([-1.5, 2.3, 1.4, 0.7])
y_pred = sigmoid_(np.dot(x, theta))
m = 1
print(vec_log_loss_(y_true, y_pred, m))
# 10.100041078687479

# Test n.3
x_new = np.arange(1, 13).reshape((3, 4))
y_true = np.array([1, 0, 1])
theta = np.array([-1.5, 2.3, 1.4, 0.7])
y_pred = sigmoid_(np.dot(x_new, theta))  # convert en list forcement !
m = len(y_true)
print(vec_log_loss_(y_true, y_pred, m))
# 7.233346147374828
                somme += (y_true[i] * math.log(y_pred[i] + eps) + (1 - y_true[i]) * math.log(1 - y_pred[i] + eps))
            J = - (1 / m) * somme
            return (J)
        else:
            print("log_loss: y_true and y_red are not the same size")
    elif isinstance (y_true, (int, float)) == 1 and isinstance(y_pred, (int, float)) == 1:
        J = - (1 / m) * (y_true * math.log(y_pred + eps) + (1 - y_true) * math.log(1 - y_pred + eps))
        return (J)
    else:
        print("log_loss: error with var type")

# Test n.1
x = 4
y_true = 1
theta = 0.5
y_pred = sigmoid_(x * theta)
m = 1 # length of y_true is 1
print(log_loss_(y_true, y_pred, m))

# Test n.2
x = [1, 2, 3, 4]
y_true = 0
theta = [-1.5, 2.3, 1.4, 0.7]
x_dot_theta = sum([a*b for a, b in zip(x, theta)])
y_pred = sigmoid_(x_dot_theta)
m = 1
print(log_loss_(y_true, y_pred, m))

# Test n.3
x_new = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
y_true = [1, 0, 1]
        lambda_: a float for the regularization parameter
        eps: epsilon (default=1e-15)
    Returns:
        The logistic loss value as a float.
        None on any error.
    Raises:
        This function should not raise any Exception.
    """
    y = y_true
    h = y_pred
    j = (1 / m) * np.sum((-y.T) @ np.log(h + eps) -
                         (1 - y).T @ (1 - np.log(h + eps)) +
                         (np.dot(lambda_, theta) * theta))
    return j


if __name__ == "__main__":
    x_new = np.arange(1, 13).reshape((3, 4))
    y_true = np.array([1, 0, 1])
    theta = np.array([-1.5, 2.3, 1.4, 0.7])
    h = x_new.dot(theta)
    y_pred = sigmoid_(h)
    m = len(y_true)
    print(reg_log_loss_(y_true, y_pred, m, theta, 0.0))

    x_new = np.arange(1, 13).reshape((3, 4))
    y_true = np.array([1, 0, 1])
    theta = np.array([-1.5, 2.3, 1.4, 0.7])
    y_pred = sigmoid_(np.dot(x_new, theta))
    m = len(y_true)
    print(reg_log_loss_(y_true, y_pred, m, theta, 0.5))