Plotting functions in Python

Here I’m going to plot several machine learning-related functions in Python.

First, import necessary modules:

import numpy as np
import matplotlib.pyplot as plt

Sigmoid function

x = np.arange(-10, 10, 0.1)
z = 1 / (1 + np.exp(-x))
plt.plot(x, z)
plt.xlabel('x')
plt.ylabel('$\sigma(x)$')
plt.show()
sigmoid

Natural log

x = np.arange(0.1, 10, 0.1)
z = np.log(x)
plt.plot(x, z)
plt.xlabel('x')
plt.ylabel('$\ln x$')
plt.show()

Rectified Linear Units (ReLU)

x = np.arange(-10, 10, 0.1)
zeros = np.zeros(len(x))
y = np.max([zeros,x], axis=0)
plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('y')
plt.show()

tanh

x = np.arange(-10, 10, 0.1)
y = (2 / (1 + np.exp(-2*x))) - 1
plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('y')
plt.show()

Comments

Copied title and URL