Regression Model

General Notation

Description

Python (if applicable)

๐‘Ž๐‘Ž

scalar, non bold

๐š๐š

vector, bold

Regression

๐ฑ๐ฑ

Training Example feature values (in this lab - Size (1000 sqft))

x_train

๐ฒ๐ฒ

Training Example targets (in this lab Price (1000s of dollars))

y_train

๐‘ฅ(๐‘–)๐‘ฅ(๐‘–), ๐‘ฆ(๐‘–)๐‘ฆ(๐‘–)

๐‘–๐‘–๐‘กโ„ŽTraining Example

x_i, y_i

m

Number of training examples

m

๐‘ค๐‘ค

parameter: weight

w

๐‘๐‘

parameter: bias

b

๐‘“๐‘ค,๐‘(๐‘ฅ(๐‘–))๐‘“๐‘ค,๐‘(๐‘ฅ(๐‘–))

The result of the model evaluation at ๐‘ฅ(๐‘–)๐‘ฅ(๐‘–) parameterized by ๐‘ค,๐‘๐‘ค,๐‘: ๐‘“๐‘ค,๐‘(๐‘ฅ(๐‘–))=๐‘ค๐‘ฅ(๐‘–)+๐‘๐‘“๐‘ค,๐‘(๐‘ฅ(๐‘–))=๐‘ค๐‘ฅ(๐‘–)+๐‘

f_wb

Tools - ๅทฅๅ…ท

  • NumPy๏ผŒไธ€ไธชๆต่กŒ็š„็ง‘ๅญฆ่ฎก็ฎ—ๅบ“

  • Matplotlib, ไธ€ไธช็”จไบŽ็ป˜ๅˆถๆ•ฐๆฎ็š„ๆต่กŒๅบ“

import numpy as np
import matplotlib.pyplot as plt
plt.style.use('./deeplearning.mplstyle')

Problem Statement - ้—ฎ้ข˜้™ˆ่ฟฐ

ไฝฟ็”จไฝๆˆฟไปทๆ ผ้ข„ๆต‹ ไฝฟ็”จไธ€ไธช็ฎ€ๅ•็š„ๆ•ฐๆฎ้›†๏ผŒๅ…ถไธญๅชๆœ‰ไธคไธชๆ•ฐๆฎ็‚น--ไธ€ไธช้ข็งฏไธบ 1000 ๅนณๆ–น่‹ฑๅฐบ๏ผˆsqft๏ผ‰็š„ๆˆฟๅญๅ”ฎไปทไธบ 30 ไธ‡็พŽๅ…ƒ๏ผŒๅฆไธ€ไธช้ข็งฏไธบ 2000 ๅนณๆ–น่‹ฑๅฐบ๏ผˆsqft๏ผ‰็š„ๆˆฟๅญๅ”ฎไปทไธบ 50 ไธ‡็พŽๅ…ƒใ€‚่ฟ™ไธคไธช็‚นๅฐ†ๆž„ๆˆๆˆ‘ไปฌ็š„ๆ•ฐๆฎ้›†ๆˆ–่ฎญ็ปƒ้›†ใ€‚ๅœจไพ‹ๅญไธญ๏ผŒ้ข็งฏๅ•ไฝๆ˜ฏ 1000 ๅนณๆ–น่‹ฑๅฐบ๏ผŒไปทๆ ผๅ•ไฝๆ˜ฏ 1000 ็พŽๅ…ƒใ€‚

Size (1000 sqft) ้ข็งฏ๏ผˆ1000 ๅนณๆ–น่‹ฑๅฐบ๏ผ‰

Price (1000s of dollars) ไปทๆ ผ๏ผˆๅƒ็พŽๅ…ƒ๏ผ‰

1.0

300

2.0

500

้€š่ฟ‡่ฟ™ไธค็‚นๆ‹Ÿๅˆไธ€ไธช็บฟๆ€งๅ›žๅฝ’ๆจกๅž‹๏ผˆๅฆ‚ไธŠๅ›พ่“่‰ฒ็›ด็บฟๆ‰€็คบ๏ผ‰๏ผŒ่ฟ™ๆ ทๅฐฑๅฏไปฅ้ข„ๆต‹ๅ…ถไป–ๆˆฟๅฑ‹็š„ไปทๆ ผ--ๆฏ”ๅฆ‚๏ผŒ1200 ๅนณๆ–น่‹ฑๅฐบ็š„ๆˆฟๅฑ‹ใ€‚

# x_train is the input variable (size in 1000 square feet)
# y_train is the target (price in 1000s of dollars)
x_train = np.array([1.0, 2.0])
y_train = np.array([300.0, 500.0])
print(f"x_train = {x_train}")
print(f"y_train = {y_train}")

่ฎญ็ปƒ็คบไพ‹ๆ•ฐ m

ไฝฟ็”จ m ่กจ็คบ่ฎญ็ปƒ็คบไพ‹็š„ๆ•ฐ้‡ใ€‚Numpy ๆ•ฐ็ป„ๆœ‰ไธ€ไธช .shape ๅ‚ๆ•ฐใ€‚ x_train.shape ่ฟ”ๅ›žไธ€ไธช python ๅ…ƒ็ป„๏ผŒๆฏไธช็ปด้ƒฝๆœ‰ไธ€ไธชๆก็›ฎใ€‚ x_train.shape[0] ๆ˜ฏๆ•ฐ็ป„็š„้•ฟๅบฆๅ’Œ็คบไพ‹ๆ•ฐ๏ผŒๅฆ‚ไธ‹ๆ‰€็คบใ€‚

# m is the number of training examples
print(f"x_train.shape: {x_train.shape}")
m = x_train.shape[0]
print(f"Number of training examples is: {m}")

ไนŸๅฏไปฅไฝฟ็”จ Python len() ๅ‡ฝๆ•ฐ๏ผŒๅฆ‚ไธ‹ๆ‰€็คบใ€‚

# m is the number of training examples
m = len(x_train)
print(f"Number of training examples is: {m}")

Training example x_i, y_i ไฝฟ็”จ (x (๐‘–)(๐‘–) , y (๐‘–)(๐‘–)) ่กจ็คบ ๐‘–๐‘กh๐‘–๐‘กโ„Ž ่ฎญ็ปƒ็คบไพ‹ใ€‚็”ฑไบŽ Python ๆ˜ฏ้›ถ็ดขๅผ•็š„๏ผŒๅ› ๆญค (x (0)(0) , y (0)(0)) ๆ˜ฏ (1.0, 300.0)๏ผŒ่€Œ (x (1)(1), y (1)(1)) ๆ˜ฏ (2.0, 500.0)ใ€‚ ่ฆ่ฎฟ้—ฎ Numpy ๆ•ฐ็ป„ไธญ็š„ๆŸไธชๅ€ผ๏ผŒ้œ€่ฆ็”จๆ‰€้œ€็š„ๅ็งป้‡ๅฏนๆ•ฐ็ป„่ฟ›่กŒ็ดขๅผ•ใ€‚ไพ‹ๅฆ‚๏ผŒ่ฎฟ้—ฎ x_train ไธญ็ฌฌ 0 ไธชไฝ็ฝฎ็š„่ฏญๆณ•ๆ˜ฏ x_train[0] ใ€‚่ฟ่กŒไธ‹้ข็š„ไปฃ็ ๅ—ๅฏ่Žทๅพ— ๐‘–๐‘กh๐‘–๐‘กโ„Ž ็š„่ฎญ็ปƒ็คบไพ‹ใ€‚

i = 0 # Change this to 1 to see (x^1, y^1)
x_i = x_train[i]
y_i = y_train[i]
print(f"(x^({i}), y^({i})) = ({x_i}, {y_i})")

Plotting the data ็ป˜ๅˆถๆ•ฐๆฎๅ›พ ๅฏไปฅไฝฟ็”จ matplotlib ๅบ“ไธญ็š„ scatter() ๅ‡ฝๆ•ฐ็ป˜ๅˆถ่ฟ™ไธค็‚น

  • ๅ‡ฝๆ•ฐๅ‚ๆ•ฐ marker ๅ’Œ c ๅฐ†็‚นๆ˜พ็คบไธบ็บข่‰ฒๅๅญ—๏ผˆ้ป˜่ฎคไธบ่“็‚น๏ผ‰ใ€‚

ๆ‚จๅฏไปฅไฝฟ็”จ matplotlib ๅบ“ไธญ็š„ๅ…ถไป–ๅ‡ฝๆ•ฐๆฅ่ฎพ็ฝฎ่ฆๆ˜พ็คบ็š„ๆ ‡้ข˜ๅ’Œๆ ‡็ญพ

# Plot the data points
plt.scatter(x_train, y_train, marker='x', c='r')
# Set the title
plt.title("Housing Prices")
# Set the y-axis label
plt.ylabel('Price (in 1000s of dollars)')
# Set the x-axis label
plt.xlabel('Size (1000 sqft)')
plt.show()

Model function - ๆจกๅž‹ๅ‡ฝๆ•ฐ

็บฟๆ€งๅ›žๅฝ’็š„ๆจกๅž‹ๅ‡ฝๆ•ฐ๏ผˆๅณไปŽ x ๆ˜ ๅฐ„ๅˆฐ y ็š„ๅ‡ฝๆ•ฐ๏ผ‰่กจ็คบไธบ

๐‘“๐‘ค,๐‘(๐‘ฅ(๐‘–))=๐‘ค๐‘ฅ(๐‘–)+๐‘๐‘“๐‘ค,๐‘(๐‘ฅ(๐‘–))=๐‘ค๐‘ฅ(๐‘–)+๐‘

ไธŠ้ข็š„ๅ…ฌๅผๅฐฑๆ˜ฏ่กจ็คบ็›ด็บฟ็š„ๆ–นๆณ•-- ๐‘ค๐‘คๅ’Œ ๐‘๐‘็š„ๅ€ผไธๅŒ๏ผŒๆ›ฒ็บฟๅ›พไธŠ็š„็›ด็บฟไนŸไธๅŒใ€‚

่ฏ•็€้€š่ฟ‡ไธ‹้ข็š„ไปฃ็ ๅ—ๆฅๆ›ดๅฅฝๅœฐ็†่งฃ่ฟ™ไธ€็‚นใ€‚ไปŽ ๐‘ค=100๐‘ค=100 ๅ’Œ ๐‘=100๐‘=100 ๅผ€ๅง‹ใ€‚

ๆณจ๏ผšๅฏไปฅ่ฟ”ๅ›ž่ฏฅๅ•ๅ…ƒๆ ผ่ฐƒๆ•ดๆจกๅž‹็š„ w ๅ’Œ b ๅ‚ๆ•ฐ

w = 100
b = 100
print(f"w: {w}")
print(f"b: {b}")

่ฎก็ฎ—ไธคไธชๆ•ฐๆฎ็‚น็š„ ๐‘“๐‘ค,๐‘(๐‘ฅ(๐‘–))๐‘“๐‘ค,๐‘(๐‘ฅ(๐‘–)) ๅ€ผใ€‚ๅฏไปฅๅฐ†ๆฏไธชๆ•ฐๆฎ็‚น็š„่ฎก็ฎ—็ป“ๆžœๆ˜Ž็กฎๅ†™ไธบ ๅฏนไบŽ ๐‘ฅ(0)๐‘ฅ(0), f_wb = w * x[0] + b ๅฏนไบŽ ๐‘ฅ(1)๐‘ฅ(1) , f_wb = w * x[1] + b ๅฏนไบŽๅคง้‡ๆ•ฐๆฎ็‚นๆฅ่ฏด๏ผŒ่ฟ™ๅฏ่ƒฝไผšๅ˜ๅพ—่‡ƒ่‚ฟๅ’Œ้‡ๅคใ€‚ๅ› ๆญค๏ผŒๆ‚จๅฏไปฅๅœจ for ๅพช็Žฏไธญ่ฎก็ฎ—ๅ‡ฝๆ•ฐ่พ“ๅ‡บ๏ผŒๅฆ‚ไธ‹้ข็š„ compute_model_output ๅ‡ฝๆ•ฐๆ‰€็คบใ€‚

ๆณจๆ„๏ผšๅ‚ๆ•ฐๆ่ฟฐ (ndarray (m,)) ๆ่ฟฐไบ†ไธ€ไธช Numpy n ็ปดๆ•ฐ็ป„็š„ๅฝข็Šถ (m,)ใ€‚ (scalar) ๆ่ฟฐ็š„ๅ‚ๆ•ฐๆฒกๆœ‰็ปดๆ•ฐ๏ผŒๅชๆœ‰ๅคงๅฐใ€‚ ๆณจๆ„๏ผš np.zero(n) ๅฐ†่ฟ”ๅ›žไธ€ไธชๅŒ…ๅซ ๐‘›๐‘› ๆก็›ฎ็š„ไธ€็ปด numpy ๆ•ฐ็ป„

def compute_model_output(x, w, b):
    """
    Computes the prediction of a linear model
    Args:
      x (ndarray (m,)): Data, m examples 
      w,b (scalar)    : model parameters  
    Returns
          f_wb (ndarray (m,)): model prediction
    """
    m = x.shape[0]
    f_wb = np.zeros(m)
    for i in range(m):
        f_wb[i] = w * x[i] + b
        
    return f_wb

่ฐƒ็”จ compute_model_output ๅ‡ฝๆ•ฐๅนถ็ป˜ๅˆถ่พ“ๅ‡บ็ป“ๆžœใ€‚

tmp_f_wb = compute_model_output(x_train, w, b,)

# Plot our model prediction
plt.plot(x_train, tmp_f_wb, c='b',label='Our Prediction')

# Plot the data points
plt.scatter(x_train, y_train, marker='x', c='r',label='Actual Values')

# Set the title
plt.title("Housing Prices")
# Set the y-axis label
plt.ylabel('Price (in 1000s of dollars)')
# Set the x-axis label
plt.xlabel('Size (1000 sqft)')
plt.legend()
plt.show()

่ฎพ็ฝฎ ๐‘ค=100๐‘ค=100 ๅ’Œ ๐‘=100๐‘=100 ๅนถไธ่ƒฝๅพ—ๅˆฐ็ฌฆๅˆๆ•ฐๆฎ็š„็›ด็บฟใ€‚

ๆญฃ็กฎ็ญ”ๆกˆๆ˜ฏ w = 200 ๏ผŒb = 100

Last updated

Was this helpful?