%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import time
def export_plot(plt,filename,format='pdf'):
"""
Exports the figure into the project folder
suported formats : eps, jpeg, jpg, pdf, pgf, png, ps, raw, rgba, svg, svgz, tif, tiff.
"""
formated_filename = "{0}_{1}.{2}".format(time.strftime("%Y%m%d_%H%M%S"), filename, format)
plt.savefig(formated_filename, format=format)
X = np.linspace(-1,1,150)*(np.pi + 0.5)
def threshold(x):
y = np.zeros(x.size)
y[x>0] = 1
return y
plt.plot([X.min(), X.max()], [0.5, 0.5], 'r-')
plt.plot([0, 0], [-0.2,1.2], 'r-')
plt.ylim([-0.2,1.2])
plt.plot(X, threshold(X), 'b-', linewidth=3)
export_plot(plt, "threshold")
def identity(x):
return x
plt.plot([X.min(), X.max()], [0, 0], 'r-')
plt.plot([0, 0], [-4,4], 'r-')
plt.ylim([-4,4])
plt.plot(X, identity(X), 'b-', linewidth=3)
export_plot(plt, "identity")
def linear(x,a):
return x*a
plt.plot([X.min(), X.max()], [0, 0], 'r-')
plt.plot([0, 0], [-4,4], 'r-')
plt.ylim([-4,4])
plt.plot(X, linear(X,2), 'y-', linewidth=3)
plt.plot(X, linear(X,1), 'g-.', linewidth=3)
plt.plot(X, linear(X,0.5), 'b-', linewidth=3)
plt.annotate(r'Increasing $\alpha$', xy=(0.5, 3), xycoords='data',
xytext=(45, -120), textcoords='offset points',
arrowprops=dict(arrowstyle="->",
connectionstyle="arc3,rad=-.3")
)
export_plot(plt, "linear")
def linear_piecewise(x,a):
y = np.copy(x)*a
idx = y >= 1
y[idx] = 1
idx = y <= 0
y[idx] = 0
return y
plt.plot([X.min(), X.max()], [0.5, 0.5], 'r-')
plt.plot([0.25, 0.25], [-0.5,1.5], 'r-')
plt.plot([0.5, 0.5], [-0.5,1.5], 'r-')
plt.plot([1, 1], [-0.5,1.5], 'r-')
plt.ylim([-0.5,1.5])
plt.plot(X, linear_piecewise(X,2), 'y-', linewidth=3)
plt.plot(X, linear_piecewise(X,1), 'g-.', linewidth=3)
plt.plot(X, linear_piecewise(X,0.5), 'b-', linewidth=3)
plt.annotate(r'Increasing $\alpha$', xy=(0.5, 1.2), xycoords='data',
xytext=(45, -120), textcoords='offset points',
arrowprops=dict(arrowstyle="->",
connectionstyle="arc3,rad=-.3")
)
export_plot(plt, "linear_piecewise")
def linear_piecewise_symmetric(x,a):
y = np.copy(x*a)
idx = y >= 1
y[idx] = 1
idx = y <= -1
y[idx] = -1
return y
plt.plot([X.min(), X.max()], [0, 0], 'r-')
plt.plot([0, 0], [-1.5,1.5], 'r-')
plt.ylim([-1.5,1.5])
plt.plot(X, linear_piecewise_symmetric(X,2), 'y-', linewidth=3)
plt.plot(X, linear_piecewise_symmetric(X,1), 'g-.', linewidth=3)
plt.plot(X, linear_piecewise_symmetric(X,0.5), 'b-', linewidth=3)
plt.annotate(r'Increasing $\alpha$', xy=(0.5, 1.2), xycoords='data',
xytext=(45, -120), textcoords='offset points',
arrowprops=dict(arrowstyle="->",
connectionstyle="arc3,rad=-.3")
)
export_plot(plt, "linear_piecewise_symmetric")
def tanh(x):
return np.tanh(x)
plt.plot([X.min(), X.max()], [0, 0], 'r-')
plt.plot([0, 0], [-1.2,1.2], 'r-')
plt.ylim([-1.2,1.2])
plt.plot(X, tanh(X), 'b-', linewidth=3)
export_plot(plt, "tanh")
def logistic(x,a=1):
return 1/(1 + np.exp(-x*a))
plt.plot([X.min(), X.max()], [0.5, 0.5], 'r-')
plt.plot([0, 0], [-0.2,1.2], 'r-')
plt.ylim([-0.2,1.2])
plt.plot(X, logistic(X,4), 'y-', linewidth=3)
plt.plot(X, logistic(X,1), 'g-.', linewidth=3)
plt.plot(X, logistic(X,0.25), 'b-', linewidth=3)
plt.annotate(r'Increasing $\alpha$', xy=(0.5, 1), xycoords='data',
xytext=(45, -120), textcoords='offset points',
arrowprops=dict(arrowstyle="->",
connectionstyle="arc3,rad=-.3")
)
export_plot(plt, "logistic")
def gaussian(x,a=1):
return np.exp(-x*a*x*a)
plt.plot([X.min(), X.max()], [1, 1], 'r-')
plt.plot([0, 0], [-0.2,1.2], 'r-')
plt.ylim([-0.2,1.2])
plt.plot(X, gaussian(X,4), 'y-', linewidth=3)
plt.plot(X, gaussian(X,1), 'g-.', linewidth=3)
plt.plot(X, gaussian(X,0.25), 'b-', linewidth=3)
plt.annotate(r'Increasing $\alpha$', xy=(1,0.2), xycoords='data',
xytext=(45, 120), textcoords='offset points',
arrowprops=dict(arrowstyle="->",
connectionstyle="arc3,rad=.3")
)
export_plot(plt, "gaussian")
def gaussian_symmetric(x,a=1):
return np.exp(-x*a*x*a)*2-1
plt.plot([X.min(), X.max()], [1, 1], 'r-')
plt.plot([0, 0], [-0.2,1.2], 'r-')
plt.ylim([-0.2,1.2])
plt.plot(X, gaussian_symmetric(X,4), 'y-', linewidth=3)
plt.plot(X, gaussian_symmetric(X,1), 'g-.', linewidth=3)
plt.plot(X, gaussian_symmetric(X,0.25), 'b-', linewidth=3)
plt.annotate(r'Increasing $\alpha$', xy=(1,0.2), xycoords='data',
xytext=(45, 120), textcoords='offset points',
arrowprops=dict(arrowstyle="->",
connectionstyle="arc3,rad=.3")
)
export_plot(plt, "gaussian_symetric")
def elliot(x,a=1):
return ((x*a)/2)/(1+np.abs(x*a))+0.5
plt.plot([X.min(), X.max()], [0.5, 0.5], 'r-')
plt.plot([0, 0], [-0.2,1.2], 'r-')
plt.ylim([-0.2,1.2])
plt.plot(X, elliot(X,4), 'y-', linewidth=3)
plt.plot(X, elliot(X,1), 'g-.', linewidth=3)
plt.plot(X, elliot(X,0.25), 'b-', linewidth=3)
plt.annotate(r'Increasing $\alpha$', xy=(0.5, 1), xycoords='data',
xytext=(45, -120), textcoords='offset points',
arrowprops=dict(arrowstyle="->",
connectionstyle="arc3,rad=-.3")
)
export_plot(plt, "elliot")
def elliot_symmetric(x,a=1):
return ((x*a))/(1+np.abs(x*a))
plt.plot([X.min(), X.max()], [0, 0], 'r-')
plt.plot([0, 0], [-1.2,1.2], 'r-')
plt.ylim([-1.2,1.2])
plt.plot(X, elliot_symmetric(X,4), 'y-', linewidth=3)
plt.plot(X, elliot_symmetric(X,1), 'g-.', linewidth=3)
plt.plot(X, elliot_symmetric(X,0.25), 'b-', linewidth=3)
plt.annotate(r'Increasing $\alpha$', xy=(0.5, 1), xycoords='data',
xytext=(45, -120), textcoords='offset points',
arrowprops=dict(arrowstyle="->",
connectionstyle="arc3,rad=-.3")
)
export_plot(plt, "elliot_symmetric")
def sin(x):
return np.sin(x)
plt.plot([X.min(), X.max()], [0, 0], 'r-')
plt.plot([0, 0], [-1.2,1.2], 'r-')
plt.plot([X.min(), X.max()], [1, 1], 'r-.')
plt.plot([np.pi/2, np.pi/2], [-1.2,1.2], 'r-.')
plt.ylim([-1.2,1.2])
plt.plot(X, sin(X), 'b-', linewidth=3)
export_plot(plt, "sin")
def cos(x):
return np.cos(x)
plt.plot([X.min(), X.max()], [1, 1], 'r-')
plt.plot([0, 0], [-1.2,1.2], 'r-')
plt.plot([X.min(), X.max()], [0, 0], 'r-.')
plt.plot([np.pi/2, np.pi/2], [-1.2,1.2], 'r-.')
plt.ylim([-1.2,1.2])
plt.plot(X, cos(X), 'b-', linewidth=3)
export_plot(plt, "cos")