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 ไฝฟ็จ 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 ) ่กจ็คบ ่ฎญ็ป็คบไพใ็ฑไบ Python ๆฏ้ถ็ดขๅผ็๏ผๅ ๆญค (x , y ) ๆฏ (1.0, 300.0)๏ผ่ (x , y ) ๆฏ (2.0, 500.0)ใ
่ฆ่ฎฟ้ฎ Numpy ๆฐ็ปไธญ็ๆไธชๅผ๏ผ้่ฆ็จๆ้็ๅ็งป้ๅฏนๆฐ็ป่ฟ่ก็ดขๅผใไพๅฆ๏ผ่ฎฟ้ฎ x_train ไธญ็ฌฌ 0 ไธชไฝ็ฝฎ็่ฏญๆณๆฏ x_train[0] ใ่ฟ่กไธ้ข็ไปฃ็ ๅๅฏ่ทๅพ ็่ฎญ็ป็คบไพใ
x_i, y_i
ไฝฟ็จ (x , y ) ่กจ็คบ ่ฎญ็ป็คบไพใ็ฑไบ Python ๆฏ้ถ็ดขๅผ็๏ผๅ ๆญค (x , y ) ๆฏ (1.0, 300.0)๏ผ่ (x , y ) ๆฏ (2.0, 500.0)ใ
่ฆ่ฎฟ้ฎ Numpy ๆฐ็ปไธญ็ๆไธชๅผ๏ผ้่ฆ็จๆ้็ๅ็งป้ๅฏนๆฐ็ป่ฟ่ก็ดขๅผใไพๅฆ๏ผ่ฎฟ้ฎ x_train ไธญ็ฌฌ 0 ไธชไฝ็ฝฎ็่ฏญๆณๆฏ x_train[0] ใ่ฟ่กไธ้ข็ไปฃ็ ๅๅฏ่ทๅพ ็่ฎญ็ป็คบไพใ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() ๅฝๆฐ็ปๅถ่ฟไธค็น
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 ็ๅฝๆฐ๏ผ่กจ็คบไธบ
ไธ้ข็ๅ ฌๅผๅฐฑๆฏ่กจ็คบ็ด็บฟ็ๆนๆณ-- ๅ ็ๅผไธๅ๏ผๆฒ็บฟๅพไธ็็ด็บฟไนไธๅใ
่ฏ็้่ฟไธ้ข็ไปฃ็ ๅๆฅๆดๅฅฝๅฐ็่งฃ่ฟไธ็นใไป ๅ ๅผๅงใ
ๆณจ๏ผๅฏไปฅ่ฟๅ่ฏฅๅๅ ๆ ผ่ฐๆดๆจกๅ็ w ๅ b ๅๆฐ
w = 100
b = 100
print(f"w: {w}")
print(f"b: {b}")่ฎก็ฎไธคไธชๆฐๆฎ็น็ ๅผใๅฏไปฅๅฐๆฏไธชๆฐๆฎ็น็่ฎก็ฎ็ปๆๆ็กฎๅไธบ
ๅฏนไบ , f_wb = w * x[0] + b
ๅฏนไบ , 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()่ฎพ็ฝฎ ๅ ๅนถไธ่ฝๅพๅฐ็ฌฆๅๆฐๆฎ็็ด็บฟใ
ๆญฃ็กฎ็ญๆกๆฏ w = 200 ๏ผb = 100
Last updated
Was this helpful?