Week 11 (4/18-4/24)

Notebook

Weekly digest

Linear regression

  • Linear regression and cost functions

  • Gradient descent

  • Linear regression with sklearn

Processig text

Project

Resources

1. Tips predictions

[1]:
from ipywidgets import interact, fixed
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

plt.style.use('ggplot')
sns.set_context("notebook")


df = sns.load_dataset('tips')

def tip_plot(a, b):

    predict = a*df['total_bill'] + b
    cost = ((predict - df['tip'])**2).sum()

    plt.figure(figsize=(12,7))
    sns.scatterplot(data=df, x="total_bill", y="tip", marker='o')
    x = np.arange(0, 55)
    plt.plot(x, a*x + b, c='b', lw=5, alpha=0.5)
    plt.ylim(0, 11)
    plt.title(f"a={a:.2f}   b={b:.2f}   C(a, b)={cost:.2f}", fontdict={'fontsize':20})
    plt.show()

interact(tip_plot, a=(0.1, 0.2, 0.01), b=(0.0, 1.0, 0.1));

2. Gradient descent

[3]:
import plotly.graph_objects as go
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns


def descent(Df, x0, l_rate=0.1, nsteps=1000):
    '''
    Performs gradient descent of a given function f.

    Df:
        Differential of f
    x0:
        The xtarrting point.
    l_rate:
        The learning rate.
    nsteps:
        Number of iterations to run.

    Returns:
        A list of points computed during steps of the gradient descent.
    '''

    x = np.array(x0, dtype='float')
    path = [x]
    for i in range(nsteps):
        Dfx = np.array(Df(x))
        x = x - l_rate*Dfx
        path.append(x)
    return path


def plot_descent(f, xlim, ylim, path=None, levels=20):
    '''
    Creates contour plot of a functions and the path
    computed by gradient descent applied to the function.

    f:
        Function to be plotted
    path:
        List of coordinates of points computed by the
        gradient descent algorithm.
    xlim, ylim:
        Tuples with limits of x- and y-values for the contour
        plot of the function.
    levels:
        Specifies levels of the contour plot.
    '''

    plt.figure(figsize=(8, 8))
    x, y = np.meshgrid(np.linspace(*xlim, 1000), np.linspace(*ylim, 1000))
    Z = f(np.vstack([x.ravel(), y.ravel()])).reshape(x.shape)
    plt.contourf(x, y, Z, levels=levels, cmap='bone')
    plt.contour(x, y, Z, levels=levels, colors='gray')
    if path is not None:
        plt.plot([x[0] for x in path], [x[1] for x in path], 'ro-', ms=4)
    plt.show()


def plot_descent_step(f, xlim, ylim, path=None, levels=20, last=None, step=1):
    plot_descent(f=f,
                 xlim=xlim,
                 ylim=ylim,
                 path=path[:last:step],
                 levels=levels)


def plot3d(f, xlim, ylim):
    x = np.linspace(xlim[0], xlim[1], 400)
    y = np.linspace(ylim[0], ylim[1], 400)
    X, Y = np.meshgrid(x, y)
    Z = f(np.array([X, Y]))
    fig = go.Figure(go.Surface(x=X, y=Y, z=Z, colorscale="picnic"))
    fig.update_layout(autosize=False, width=800, height=600)
    fig.show()

3. Gradient descent test functions

[4]:
def h(x):
    '''
    Himmelblau's function
    h(x, y) = (x^2 + y - 11)^2 + (x + y^2 - 7)^2
    '''
    return (x[0]**2 + x[1] - 11)**2 + (x[0] + x[1]**2 - 7)**2

def Dh(x):
    return np.array([
        2 * (x[0]**2 + x[1] - 11) * 2 * x[0] + 2 * (x[0] + x[1]**2 - 7),
        2 * (x[0]**2 + x[1] - 11) + 2 * (x[0] + x[1]**2 - 7) * 2 * x[1]
    ])


def r(x):
    '''
    Rosenbrock function
    r(x, y) = (1-x)^2 + 100(y-x^2)^2
    '''
    return (1-x[0])**2 + 100*(x[1]-x[0]**2)**2

def Dr(x):
    return np.array([-2*(1-x[0]) - 400*(x[1]-x[0]**2)*x[0], 200*(x[1]-x[0]**2)])

4. Text sample

[ ]:
sample = """I am writing in reference to the our conversation on Tuesday, March 2, 2021.
Your order of 100,000 units of GC-1344 microcontrollers has been confirmed.
You will be billed $19.50 per unit for the total of $1,950,000.00.
The expected delivery date is Friday, April 30, 2021. In case of any further
questions please contact our sales department by email support@simonis.biz,
phone (294) 934-0923 or fax (294) 934-0202.

Delfina Fischer
Manager, Simonis LLC
delfina.fisher@simonis.biz
(294) 934 0937
"""

5. re_show

[10]:
import html
import re
from IPython.core.display import display, HTML


def re_show(regex, text="", flags=0):
    """
    Displays text with the regex match highlighted.
    """
    text_css = '''"border-style: none;
                   border-width: 0px;
                   padding: 0px;
                   font-size: 14px;
                   color: darkslategray;
                   background-color: white;
                   white-space: pre;
                   line-height: 20px;"
                   '''
    match_css = '''"padding: 0px 1px 0px 1px;
                    margin: 0px 0.5px 0px 0.5px;
                    border-style: solid;
                    border-width: 0.5px;
                    border-color: darkred;
                    background-color: cornsilk;
                    color: red;"
                    '''

    r = re.compile(f"({regex})", flags=flags)
    t = r.sub(fr'###START###\1###END###', text)
    t = html.escape(t)
    t = t.replace("###START###", f"<span style={match_css}>")
    t = t.replace("###END###", f"</span>")
    display(HTML(f'<code style={text_css}>{t}</code>'))

Exercises

Exercise 1.

Complete regex exercises posted here. You don’t need to do them all. The first few are easier, then they get more tricky.