# -*- coding: utf-8 -*-
"""l1-l2-combined.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/17Xr03SrQnf94XZN_eWeVtGFzvBiugO4O
"""

# Importing Python packages from Python Libraries
import matplotlib.pyplot as plt
import numpy as np


"""
    Set up the 2-D space for graph plotting.
    Can be extended to 3 dimensions, or "n" dimensions.
"""
w_1 = np.linspace(-1.0, 1.0, 100)
w_2 = np.linspace(-1.0, 1.0, 100)
W_1, W_2 = np.meshgrid(w_1,w_2)
# Constant representing the radius of the (w_1, w_2) circle.
t = 1
# Equation representing L2 regularization.
L2 = W_1**2 + W_2**2 - t
print("Plotting L2 regularization")
plt.contour(W_1,W_2,L2,[0])
plt.show()

# Equation representing L1 regularization.
L1 = abs(W_1) + abs(W_2) - t
print("Plotting L1 regularization")
plt.contour(W_1,W_2,L1,[0])
plt.show()

"""
    Equation for elastic net, which combines L1 regularization
        with L2 regularization as a linear combination of these
        regularization methods.
"""
alpha = [0.1*x for x in range(10)]
beta = [1 - y for y in alpha]
print("alpha is:",alpha,"=")
print("beta is:",beta,"=")
"""
    Determine the current combination of weights,
        or ratio of alpha and beta, for the elastic net to
        combine L1 and L2 norm.
"""
for index, (alpha_i,beta_i) in enumerate(zip(alpha,beta)):
    elastic_net = alpha_i*L1 + beta_i*L2
    print("Plotting elastic net combination of L1 & L2 regularization.")
    plt.contour(W_1,W_2,elastic_net,[0])
    plt.show()
