In [1]:
import numpy as np
import scipy as sp
import pandas as pd
import warnings
warnings.filterwarnings("ignore")
In [2]:
np.random.seed(71)
x = np.arange(10)
y = x
# Plot the most naive matplotlib plot. :-)
import matplotlib.pyplot as plt
plt.plot(x, y)
plt.show()
In [3]:
np.random.seed(71)
x = np.random.random(50)
y = np.random.random(50)
# Plot the most naive matplotlib line plot. :-)
import matplotlib.pyplot as plt
plt.scatter(x, y)
plt.show()
In [4]:
def plot_line(x, y, title='Line Plot', xlab='xlab', ylab='lab',
label='label', lw=2, xlim=None, ylim=None):
"""Line.
Args:
x: NumPy array, x vector.
y: NumPy array, y vector.
title: string, title.
xlab: string, x-axis label.
ylab: string, y-axis label.
label: string, line label.
lw: scalar, line width.
xlim: list with [x_lower, x_upper]. Default to None.
ylim: list with [y_lower, y_upper]. Default to None.
"""
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib
matplotlib.style.use('ggplot')
%matplotlib inline
plt.figure()
plt.plot(x, y, color='darkorange', lw=lw, label=label)
plt.title(title)
plt.xlabel(xlab)
plt.ylabel(ylab)
plt.legend(loc='lower right')
if xlim is not None:
plt.xlim(xlim)
if ylim is not None:
plt.ylim(ylim)
plt.show()
In [5]:
np.random.seed(71)
x = np.arange(5)
y = x
plot_line(x, y)
In [6]:
def plot_scatter(x, y, title='Scatter Plot', xlab='xlab', ylab='lab',
color='b', marker='.', lw=2.5,
xlim=None, ylim=None):
"""Scatter plot.
Args:
x: NumPy array, x vector.
y: NumPy array, y vector.
title: string, title.
xlab: string, x-axis label.
ylab: string, y-axis label.
color: string, point color. Default to 'b'.
marker: string, point marker. Default to '.'.
lw: scalar, line width. Default to 2.5.
xlim: list with [x_lower, x_upper]. Default to None.
ylim: list with [y_lower, y_upper]. Default to None.
"""
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib
matplotlib.style.use('ggplot')
%matplotlib inline
plt.figure()
plt.scatter(x, y, color=color, marker=marker, lw=lw)
plt.title(title)
plt.xlabel(xlab)
plt.ylabel(ylab)
plt.legend(loc='lower right')
if xlim is not None:
plt.xlim(xlim)
if ylim is not None:
plt.ylim(ylim)
plt.show()
In [7]:
np.random.seed(71)
x = np.random.random(50)
y = np.random.random(50)
plot_scatter(x, y)
In [8]:
def plot_roc(y_true, prob_pred, title='ROC Curve',
xlab='False Positive (FP) Rate', ylab='True Positive (TP) Rate', lw=2):
"""Plot Receiver Operating Characteristics (ROC) Curve.
Args:
y_true: numpy array, labels in test set.
prob_pred: numpy array, predicted probability for test set.
title: string, title.
xlab: string, x-axis label.
ylab: string, y-axis label.
lw: scalar, line width.
"""
from sklearn.metrics import roc_curve, auc
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib
matplotlib.style.use('ggplot')
%matplotlib inline
fpr, tpr, thresholds = roc_curve(y_true, prob_pred)
roc_auc = auc(fpr, tpr)
plt.figure()
plt.plot(fpr, tpr, color='darkorange',
lw=lw, label='ROC Curve (AUC = %0.2f)' % roc_auc)
plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.title(title)
plt.xlabel(xlab)
plt.ylabel(ylab)
plt.legend(loc='lower right')
plt.show()