AMATH Quiz 8

Regression

  • Recognize the difference between interpolation and extrapolation for a model.

    • Interpolation is for data within a model

    • Extrapolation is for data outside of a model


  • Recognize error functions for regression.

    • Max Error

      • E=max⁡k=1,…,n∣y^k−yk∣E=\max_{k=1,\ldots,n}\left|ŷ_{k}-y_{k}\right|

    • Average, or L1L^1 error'

      • E=1n∑k=1n∣y^k−yk∣E=\frac{1}{n}\sum_{k=1}^{n}\left|ŷ_{k}-y_{k}\right|

    • Root mean square/Sum of squared/L2L^2 error

      • E=1n∑k=1n(y^k−yk)2E=\sqrt{\frac{1}{n}\sum_{k=1}^{n}\left(ŷ_{k}-y_{k}\right)^2}

    • Sum of Squares Error

      • E=1n∑k=1n(y^k−yk)2E=\frac{1}{n}\sum_{k=1}^{n}\left(ŷ_{k}-y_{k}\right)^2


  • Create and minimize error functions for least-squares data fitting to find the line of best fit.

    • scipy.optimize.fmin

      • Change function to accept only one input

    • argmin of error will be the m and b values of best fit


  • Compare and contrast the effect of different error functions (Or norms) on regression.



  • Use regression to analyze and solve applied problems.



  • Plot fitted models against data to visually assess agreement.



  • Create linear and other polynomial fits using np.polyfit.

    • Format

      • np.polyfit(x, y, degree)

    • Example

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# Create the dataframe from data
df = pd.DataFrame({
    "Speed (mph)": [55, 57, 58, 60, 63, 65, 67, 68, 70, 71, 72, 78, 79, 80],
    "Efficiency (mpg)": [38.4, 37.2, 37.9, 35.8,  33.9, 34.7, 33.8, 32.4, 32.8, 32.1, 31.9, 28.1, 27.8, 27.1]
})

# Extract the data
speed = df["Speed (mph)"].to_numpy()
eff = df["Efficiency (mpg)"].to_numpy()

# 🚨 Find the polyfit
p = np.polyfit(speed, eff, 1) # Find y = mx+b


  • Plot linear and other polynomial fits from np.polyfit using np.polyval.

    • Format

      • np.polyval(coefficients(polyfit), x_eval)

    • Example

p = np.polyfit(speed, eff, 1) # Find y = mx+b

# 🚨 Use polyval
xplot = np.linspace(speed[0], speed[-1], 100) # Create x values for plotting
yplot = np.polyval(p, xplot)
ax.plot(xplot, yplot, label='Model')
ax.legend()
plt.show()


  • Recognize signs of overfitting in a regression model, including low training error paired with poor extrapolation behavior.



  • Recognize that overfitting reduces a model’s predictive power for new or unseen data.



  • Create and minimize error functions for nonlinear or non-polynomial regression.




Interpolating Functions

  • Determine whether regression or interpolation has been used to fit a dataset.

    • Regression vs Interpolation

      • Estimation vs Exact

      • No sensitive to noise vs Assumes there is no noise


  • Compare and contrast advantages and disadvantages of regression fitting versus interpolation.

    • Treats data points as exact

      • Interpolation

    • Handles noisy data well

      • Regression

    • Preserves all information in the data

      • Interpolation

    • Useful outside data range

      • Regression

    • Good for finding trends in datasets

      • Regression

    • Useful for creating smooth curves through all known points

      • Interpolation


  • Create and use linear interpolants using np.interp.

    • Format

      • np.interp(xvalue, xdata, ydata)

    • Example

import numpy as np
import matplotlib.pyplot as plt

x_data = np.linspace(0, 5, 6) # 6 points between 0 and 5
y_data = np.sin(x_data) # y values

# 🚨 Find the interpolant at x=2.5
yval = np.interp(2.5, x_data, y_data)


  • Create and use interpolating cubic splines using scipy.interpolate.CubicSpline.

    • Format

      • scipy.interpolate.CubicSpline(x, y)

    • Example

import numpy as np
import matplotlib.pyplot as plt

x_data = np.linspace(0, 5, 6) # 6 points between 0 and 5
y_data = np.sin(x_data) # y values

fig, ax = plt.subplots()
ax.plot(x_data, y_data, marker='o', label='data and linear interpolation')

# 🚨 Use cubic splines
import scipy.interpolate
spline = scipy.interpolate.CubicSpline(x_data, y_data)
# This is an anonymous function, we need to call it to plot
# We will plot it at various x values
x_plot = np.linspace(0, 5, 30)
ax.plot(x_plot, spline(x_plot), label='cubic splines interpolation')
ax.legend()


plt.show()



NumPy

  • sum

    • Returns the sum of an array of elements over a specified axis.

    • Format

      • np.sum(InputArray)

Scipy

  • optimize.fmin

    • Format

      • scipy.optimize.fmin(function, x0)