VIETNAM AVIATION ACADEMY
FACULTY OF AERONAUTICAL ENGINEERING
Assignment Report
HOMEWORK 3
COURSE : ARTIFICIAL INTELLIGENCE (AI)
Instructior : VO PHI SON
Student : DUONG THI PHUONG DUY
Student ID: 2433520061
HO CHI MINH– MAY, 2025
Falculty of Aeronautical Engineering
LAB 01
import numpy as np
import time
# Vector creation
print("=== Vector Creation ===")
a = np.zeros(4)
print(f"np.zeros(4): {a}")
a = np.random.random_sample(4)
print(f"np.random.random_sample(4): {a}")
a = np.arange(4.)
print(f"np.arange(4.): {a}")
a = np.random.rand(4)
print(f"np.random.rand(4): {a}")
a = np.array([5, 4, 3, 2])
print(f"np.array([5,4,3,2]): {a}")
# Indexing & Slicing
print("\n=== Indexing & Slicing ===")
a = np.arange(10)
print(f"a = {a}")
print(f"a[2] = {a[2]}")
print(f"a[-1] = {a[-1]}")
try:
print(a[10])
except Exception as e:
print("Shape mismatch error:", e)
print(f"5 * a = {5 * a}")
# Dot product (manual and NumPy)
def my_dot(a, b):
x=0
for i in range(a.shape[0]):
x += a[i] * b[i]
return x
a = np.array([1, 2, 3, 4])
b = np.array([-1, 4, 3, 2])
print(f"my_dot(a, b) = {my_dot(a, b)}")
print(f"np.dot(a, b) = {np.dot(a, b)}")
# Performance comparison
print("\n=== Performance Comparison ===")
Assignment Lab 1 Page 2/22
Falculty of Aeronautical Engineering
np.random.seed(1)
a = np.random.rand(10_000_000)
b = np.random.rand(10_000_000)
tic = time.time()
c = np.dot(a, b)
toc = time.time()
print(f"Vectorized np.dot(a, b) = {c:.4f}, duration = {1000*(toc - tic):.2f} ms")
tic = time.time()
c = my_dot(a, b)
toc = time.time()
print(f"Looped my_dot(a, b) = {c:.4f}, duration = {1000*(toc - tic):.2f} ms")
del a, b
# Matrix creation and operations
print("\n=== Matrix Creation and Indexing ===")
a = np.zeros((1, 5))
print(f"zeros(1,5): shape={a.shape}, \n{a}")
a = np.zeros((2, 1))
print(f"zeros(2,1): shape={a.shape}, \n{a}")
a = np.array([[5], [4], [3]])
print(f"Manual matrix: shape={a.shape}, \n{a}")
a = np.arange(6).reshape(3, 2)
print(f"Reshaped matrix: shape={a.shape}, \n{a}")
print(f"a[2,0] = {a[2,0]}")
print(f"a[2] = {a[2]}")
# Matrix slicing
print("\n=== Matrix Slicing ===")
a = np.arange(20).reshape(2, 10)
print(f"a = \n{a}")
print(f"a[0, 2:7:1] = {a[0, 2:7:1]}")
print(f"a[:, 2:7:1] = \n{a[:, 2:7:1]}")
print(f"a[:,:] = \n{a[:,:]}")
print(f"a[0] = {a[0]}")
print(f"a[1] = {a[1]}")
KET QUA
=== Vector Creation ===
np.zeros(4): [0. 0. 0. 0.]
np.random.random_sample(4): [0.17151519 0.54973983 0.91731098 0.8322861 ]
np.arange(4.): [0. 1. 2. 3.]
np.random.rand(4): [0.46808877 0.06304921 0.56839091 0.8603235 ]
np.array([5,4,3,2]): [5 4 3 2]
=== Indexing & Slicing ===
a = [0 1 2 3 4 5 6 7 8 9]
Assignment Lab 1 Page 3/22
Falculty of Aeronautical Engineering
a[2] = 2
a[-1] = 9
Shape mismatch error: index 10 is out of bounds for axis 0 with size 10
5 * a = [ 0 5 10 15 20 25 30 35 40 45]
my_dot(a, b) = 24
np.dot(a, b) = 24
=== Performance Comparison ===
Vectorized np.dot(a, b) = 2501072.5817, duration = 14.00 ms
Looped my_dot(a, b) = 2501072.5817, duration = 2978.00 ms
=== Matrix Creation and Indexing ===
zeros(1,5): shape=(1, 5),
[[0. 0. 0. 0. 0.]]
zeros(2,1): shape=(2, 1),
[[0.]
[0.]]
Manual matrix: shape=(3, 1),
[[5]
[4]
[3]]
Reshaped matrix: shape=(3, 2),
[[0 1]
[2 3]
[4 5]]
a[2,0] = 4
a[2] = [4 5]
=== Matrix Slicing ===
a=
[[ 0 1 2 3 4 5 6 7 8 9]
[10 11 12 13 14 15 16 17 18 19]]
a[0, 2:7:1] = [2 3 4 5 6]
a[:, 2:7:1] =
[[ 2 3 4 5 6]
[12 13 14 15 16]]
a[:,:] =
[[ 0 1 2 3 4 5 6 7 8 9]
[10 11 12 13 14 15 16 17 18 19]]
a[0] = [0 1 2 3 4 5 6 7 8 9]
a[1] = [10 11 12 13 14 15 16 17 18 19]
[ ]:
Assignment Lab 1 Page 4/22
Falculty of Aeronautical Engineering
LAB 02
def predict_single_example_pure_python(x_vector, w_vector, b_scalar):
"""
Computes the prediction for a single example in multiple variable linear regression
using pure Python (no NumPy).
Assignment Lab 1 Page 5/22
Falculty of Aeronautical Engineering
Args:
x_vector (list): A list representing the input features for one
example. w_vector (list): A list representing the weight parameters
(vector w). b_scalar (float or int): The bias parameter (scalar b).
Returns:
float: The predicted value.
Returns None if the feature vector and weight vector have different lengths.
"""
if len(x_vector) != len(w_vector):
print("Error: Feature vector and weight vector must have the same length.")
return None
prediction = 0.0
for i in range(len(x_vector)):
prediction += x_vector[i] * w_vector[i]
prediction += b_scalar
return prediction
# Test cases (similar to examples in the lab, but with pure Python lists)
# Example: house size, bedrooms, floors, age
# w = [0.2, 0.1, 0.3, -0.05] (weights for each feature)
# b = 10.0 (bias)
# x_example1 = [1200, 3, 1, 40] # Example 1: size=1200, bed=3, floor=1, age=40
# x_example2 = [2000, 4, 2, 10] # Example 2: size=2000, bed=4, floor=2, age=10
weights = [0.2, 0.1, 0.3, -0.05]
bias = 10.0
example_features_1 = [1200, 3, 1, 40]
example_features_2 = [2000, 4, 2, 10]
example_features_3_mismatch = [500, 2] # Mismatch length
print("--- Prediction for Example 1 ---")
pred1 = predict_single_example_pure_python(example_features_1, weights, bias)
if pred1 is not None:
print(f"Features: {example_features_1}")
print(f"Weights: {weights}, Bias: {bias}")
print(f"Predicted value: {pred1:.2f}")
print("\n--- Prediction for Example 2 ---")
pred2 = predict_single_example_pure_python(example_features_2, weights, bias)
if pred2 is not None:
print(f"Features: {example_features_2}")
print(f"Weights: {weights}, Bias: {bias}")
print(f"Predicted value: {pred2:.2f}")
print("\n--- Testing with mismatched lengths ---")
pred_mismatch = predict_single_example_pure_python(example_features_3_mismatch,
weights, bias)
Assignment Lab 1 Page 6/22
Falculty of Aeronautical Engineering
if pred_mismatch is not None:
print(f"Predicted value (mismatched): {pred_mismatch:.2f}")
KẾT QUẢ
Prediction for Example 1 ---
Features: [1200, 3, 1, 40]
Weights: [0.2, 0.1, 0.3, -0.05], Bias: 10.0
Predicted value: 248.60
--- Prediction for Example 2 ---
Features: [2000, 4, 2, 10]
Weights: [0.2, 0.1, 0.3, -0.05], Bias: 10.0
Predicted value: 410.50
--- Testing with mismatched lengths ---
Error: Feature vector and weight vector must have the same length
LAB 03
import numpy as np
import matplotlib.pyplot as plt
# --- Load sample housing data ---
def load_house_data():
X=np.array([ [952
, 2, 1, 65],
[1244, 3, 2, 64],
[1947, 3, 2, 17],
[2000, 4, 2, 5],
[1500, 3, 1, 45],
])
y = np.array([271.5, 232, 509.8, 612, 342])
return X, y
Assignment Lab 1 Page 7/22
Falculty of Aeronautical Engineering
# --- Gradient Descent core ---
def run_gradient_descent(X, y, iterations, alpha):
m, n = X.shape
w = np.zeros(n)
b=0
cost_history = []
for _ in range(iterations):
predictions = X @ w + b
errors = predictions - y
dj_dw = (1/m) * (X.T @ errors)
dj_db = (1/m) * np.sum(errors)
w -= alpha * dj_dw
b -= alpha * dj_db
cost = (1/(2*m)) * np.sum(errors**2)
cost_history.append((w.copy(), b, cost))
return w, b, cost_history
# --- Z-score normalization ---
def zscore_normalize_features(X):
mu = np.mean(X, axis=0)
sigma = np.std(X, axis=0)
X_norm = (X - mu) / sigma
return X_norm, mu, sigma
# --- Plot cost vs w[0] ---
def plot_cost_i_w(hist):
w_vals = [entry[0][0] for entry in hist]
costs = [entry[2] for entry in hist]
plt.plot(w_vals, costs, marker='o')
plt.xlabel('w[0]')
plt.ylabel('Cost')
plt.title('Cost vs w[0]')
plt.grid(True)
plt.show()
# --- Main execution ---
X_train, y_train = load_house_data()
X_features = ['size(sqft)', 'bedrooms', 'floors', 'age']
# Plot scatter plots
fig, ax = plt.subplots(1, 4, figsize=(12, 3), sharey=True)
for i in range(4):
ax[i].scatter(X_train[:, i], y_train)
ax[i].set_xlabel(X_features[i])
ax[0].set_ylabel("Price (1000's)")
plt.suptitle("Scatter plots of features vs. price")
plt.tight_layout()
Assignment Lab 1 Page 8/22
Falculty of Aeronautical Engineering
plt.show()
# Try several learning rates
for alpha in [9.9e-7, 9e-7, 1e-7]:
print(f"\n Alpha = {alpha}")
_, _, hist = run_gradient_descent(X_train, y_train, 10, alpha=alpha)
plot_cost_i_w(hist)
# Normalize features
X_norm, mu, sigma = zscore_normalize_features(X_train)
print("\n Đã chuẩn hóa Z-score:")
print("Mean:", mu)
print("Std Dev:", sigma)
# Train on normalized data
w_norm, b_norm, hist_norm = run_gradient_descent(X_norm, y_train, 1000, alpha=0.1)
p r i n t ( " \ n Huấn luyện thành công với alpha = 0.1")
# Predict new data
x_house = np.array([1200, 3, 1, 40])
x_house_norm = (x_house - mu) / sigma
predicted_price = np.dot(x_house_norm, w_norm) + b_norm
print(f"\n Dự đoán giá nhà 1200 sqft, 3BR, 1 tầng, 40 tuổi: {predicted_price:.2f} nghìn
đô")
# Plot actual vs predicted
m = X_norm.shape[0]
yp = np.dot(X_norm, w_norm) + b_norm
fig, ax = plt.subplots(1, 4, figsize=(12, 3), sharey=True)
for i in range(4):
ax[i].scatter(X_train[:, i], y_train, label='Thực tế')
ax[i].scatter(X_train[:, i], yp, color='orange', label='Dự đoán')
ax[i].set_xlabel(X_features[i])
ax[0].set_ylabel("Price")
ax[0].legend()
fig.suptitle("Thực tế vs Dự đoán sau chuẩn hóa")
plt.tight_layout()
plt.show()
kết quả
Alpha = 9.9e-07
Assignment Lab 1 Page 9/22
Falculty of Aeronautical Engineering
Alpha = 9e-07
Alpha = 1e-07
Assignment Lab 1 Page 10/22
Falculty of Aeronautical Engineering
Đã chuẩn hóa Z-score:
Mean: [1528.6 3. 1.6 39.2]
Std Dev: [402.87943606 0.63245553 0.48989795 24.4 ]
Huấn luyện thành công với alpha = 0.1
Dự đoán giá nhà 1200 sqft, 3BR, 1 tầng, 40 tuổi: 443.41 nghìn đô
LAB 04
import numpy as np
import matplotlib.pyplot as plt
# Cài đặt cấu hình hiển thị
np.set_printoptions(precision=2)
# Hàm chuẩn hóa Z-score
def zscore_normalize_features(X):
mu = np.mean(X, axis=0)
sigma = np.std(X, axis=0)
Assignment Lab 1 Page 1/22
Falculty of Aeronautical Engineering
X_norm = (X - mu) / sigma
return X_norm, mu, sigma
# Hàm tính cost
def compute_cost(X, y, w, b):
m = X.shape[0]
f_wb = X @ w + b
return (1 / (2 * m)) * np.sum((f_wb - y) ** 2)
# Hàm tính gradient
def compute_gradient(X, y, w, b):
m = X.shape[0]
f_wb = X @ w + b
dj_dw = (1 / m) * (X.T @ (f_wb - y))
dj_db = (1 / m) * np.sum(f_wb - y)
return dj_dw, dj_db
# Hàm chạy Gradient Descent
def run_gradient_descent(X, y, iterations, alpha):
w = np.zeros(X.shape[1])
b=0
hist = []
for i in range(iterations):
dj_dw, dj_db = compute_gradient(X, y, w,
b) w -= alpha * dj_dw
b -= alpha * dj_db
cost = compute_cost(X, y, w, b)
hist.append(cost)
if i % (iterations // 10) == 0:
print(f"Iteration {i:4}: Cost {cost:.2e}")
return w, b, hist
# Tạo dữ liệu: y = 1 +
x^2 x = np.arange(0, 20,
1)
y = 1 + x**2
X = x.reshape(-1, 1)
# Trường hợp 1: dùng đặc trưng x
print("--- Fit with original feature x
---")
w, b, _ = run_gradient_descent(X, y, iterations=1000, alpha=0.01)
plt.scatter(x, y, label="Actual")
Assignment Lab 1 Page 2/22
Falculty of Aeronautical Engineering
plt.plot(x, X @ w + b, label="Predicted")
plt.title("Model using x")
plt.legend()
plt.show()
# Trường hợp 2: dùng đặc trưng x^2
print("--- Fit with engineered feature x^2 ---")
X_sq = (x**2).reshape(-1, 1)
w2, b2, _ = run_gradient_descent(X_sq, y, iterations=10000, alpha=0.01)
plt.scatter(x, y, label="Actual")
plt.plot(x, X_sq @ w2 + b2, label="Predicted")
plt.title("Model using x^2")
plt.legend()
plt.show()
kết quả
--- Fit with original feature x ---
Iteration 0: Cost 1.66e+03
Iteration 100: Cost 6.95e+02
Iteration 200: Cost 5.88e+02
Iteration 300: Cost 5.26e+02
Iteration 400: Cost 4.90e+02
Iteration 500: Cost 4.69e+02
Iteration 600: Cost 4.56e+02
Iteration 700: Cost 4.49e+02
Iteration 800: Cost 4.45e+02
Iteration 900: Cost 4.42e+02
--- Fit with engineered feature x^2 ---
Iteration 0: Cost 1.12e+09
Assignment Lab 1 Page 3/22
Falculty of Aeronautical Engineering
Iteration 1000: Cost nan
Iteration 2000: Cost nan
Iteration 3000: Cost nan
Iteration 4000: Cost nan
Iteration 5000: Cost nan
Iteration 6000: Cost nan
Iteration 7000: Cost nan
Iteration 8000: Cost nan
Iteration 9000: Cost nan
LAB 05
import numpy as np
import matplotlib.pyplot as plt
# --- 0. Helper Functions (Custom Implementations) ---
def zscore_normalize_features(X):
"""
Normalizes the features in X using z-score normalization.
Assignment Lab 1 Page 4/22
Falculty of Aeronautical Engineering
Args:
X (ndarray (m,n)): Data, m examples, n features
Returns:
X_norm (ndarray (m,n)): Normalized data
mu (ndarray (n,)): The mean for each feature
sigma (ndarray (n,)): The standard deviation for each feature
"""
mu = np.mean(X, axis=0)
sigma = np.std(X, axis=0)
X_norm = (X - mu) / sigma
return X_norm, mu, sigma
def compute_cost(X, y, w, b):
"""
Computes the cost function for linear regression.
Args:
X (ndarray (m,n)): Data, m examples, n features
y (ndarray (m,)): Target values
w (ndarray (n,)): Parameters for weights
b (scalar): Parameter for bias
Returns:
cost (scalar): The value of the cost function
"""
m = X.shape[0]
f_wb = X @ w + b
cost = (1/(2*m)) * np.sum((f_wb - y)**2)
return cost
def compute_gradient(X, y, w, b):
"""
Computes the gradient for linear regression.
Args:
X (ndarray (m,n)): Data, m examples, n features
y (ndarray (m,)): Target values
w (ndarray (n,)): Parameters for weights
b (scalar): Parameter for bias
Returns:
dj_dw (ndarray (n,)): The gradient of the cost with respect to the weights
w dj_db (scalar): The gradient of the cost with respect to the bias b
"""
m = X.shape[0]
f_wb = X @ w + b
error = f_wb - y
dj_dw = (1/m) * (X.T @ error)
dj_db = (1/m) * np.sum(error)
return dj_dw, dj_db
Assignment Lab 1 Page 5/22
Falculty of Aeronautical Engineering
def run_gradient_descent(X, y, w_in, b_in, alpha, num_iters):
"""
Performs batch gradient descent to learn w and b.
Args:
X (ndarray (m,n)): Data, m examples, n features
y (ndarray (m,)): Target values
w_in (ndarray (n,)): Initial weights
b_in (scalar): Initial bias
alpha (scalar): Learning rate
num_iters (scalar): Number of iterations to run gradient descent
Returns:
w (ndarray (n,)): Optimized weights
b (scalar): Optimized bias
J_history (list): History of cost values
"""
w = w_in
b = b_in
J_history = [] # To store cost at each iteration
for i in range(num_iters):
dj_dw, dj_db = compute_gradient(X, y, w,
b) w = w - alpha * dj_dw
b = b - alpha * dj_db
cost = compute_cost(X, y, w, b)
J_history.append(cost)
# Optional: print cost every 10% of iterations for monitoring
if i % (num_iters // 10) == 0 or i == num_iters - 1:
print(f"Iteration {i:5}: Cost {cost:0.2e}")
return w, b, J_history
# --- 1. Data Definition ---
X_train = np.array([[2104, 5, 1, 45],
[1416, 3, 2, 40],
[852, 2, 1, 35],
[1600, 3, 2, 30],
[1900, 4, 1, 42]])
y_train = np.array([460, 232, 178, 320, 399])
X_features = ['size(sqft)', 'bedrooms', 'floors', 'age']
print("Original X_train:\n", X_train)
print("Original y_train:\n", y_train)
# --- 2. Feature Scaling (Z-score Normalization) ---
X_norm, X_mu, X_sigma = zscore_normalize_features(X_train)
Assignment Lab 1 Page 6/22
Falculty of Aeronautical Engineering
print(f"\nMean of X_train features: {X_mu}")
print(f"Standard Deviation of X_train features: {X_sigma}")
print(f"Normalized X_train:\n {X_norm}")
# --- 3. Initialize Parameters and Hyperparameters for Gradient Descent ---
m, n = X_norm.shape # m = number of examples, n = number of features
w_init = np.zeros(n)
b_init = 0.0
iterations = 10000 # Increased iterations for better convergence without sklearn's tol
alpha = 1.0e-1 # Learning rate, often requires tuning
print(f"\nInitial w: {w_init}, Initial b: {b_init}")
print(f"Iterations: {iterations}, Learning Rate (alpha): {alpha}")
# --- 4. Run Gradient Descent ---
w_final, b_final, J_hist = run_gradient_descent(X_norm, y_train, w_init, b_init, alpha,
iterations)
print(f"\nOptimized w: {w_final}")
print(f"Optimized b: {b_final:.2f}")
# --- 5. Plot Cost vs Iterations ---
plt.figure(figsize=(8, 4))
plt.plot(J_hist)
plt.title("Cost vs. Iterations")
plt.xlabel("Iteration")
plt.ylabel("Cost (J)")
plt.grid(True)
plt.show()
# --- 6. Plot Predictions vs Actual Values for each Feature ---
y_pred = X_norm @ w_final + b_final
fig, ax = plt.subplots(1, n, figsize=(12, 3), sharey=True)
for i in range(n):
ax[i].scatter(X_train[:, i], y_train, label='Target', marker='o', c='blue')
ax[i].scatter(X_train[:, i], y_pred, label='Prediction', marker='x', c='red')
ax[i].set_xlabel(X_features[i])
ax[i].grid(True)
ax[0].set_ylabel("Price")
ax[0].legend()
fig.suptitle("Target vs. Prediction for each Feature (Custom GD)")
plt.tight_layout(rect=[0, 0.03, 1, 0.95]) # Adjust layout to prevent suptitle overlap
plt.show()
# --- 7. Example Prediction with New Data ---
# Remember to normalize new data using the same mu and sigma from training
x_new_house = np.array([1200, 3, 1, 40])
# Normalizing new data
x_new_house_norm = (x_new_house - X_mu) / X_sigma
Assignment Lab 1 Page 7/22
Falculty of Aeronautical Engineering
predicted_price_new = x_new_house_norm @ w_final + b_final
print(f"\nNew house features (original): {x_new_house}")
print(f"New house features (normalized): {x_new_house_norm}")
print(f"Predicted price for the new house: {predicted_price_new:.2f} USD")
kết quả
Original X_train:
[[2104 5 1 45]
[1416 3 2 40]
[ 852 2 1 35]
[1600 3 2 30]
[1900 4 1 42]]
Original y_train:
[460 232 178 320 399]
Mean of X_train features: [1.5744e+03 3.4000e+00 1.4000e+00 3.8400e+01]
Standard Deviation of X_train features: [432.22660723 1.0198039 0.48989795
5.3141321 ]
Normalized X_train:
[[ 1.2252832 1.56892908 -0.81649658 1.24197138]
[-0.36647443 -0.39223227 1.22474487 0.30108397]
[-1.67134551 -1.37281295 -0.81649658 -0.63980344]
[ 0.05922819 -0.39223227 1.22474487 -1.58069085]
[ 0.75330855 0.58834841 -0.81649658 0.67743894]]
Initial w: [0. 0. 0. 0.], Initial b: 0.0
Iterations: 10000, Learning Rate (alpha):
0.1 Iteration 0: Cost 4.42e+04
Iteration 1000: Cost 2.33e-02
Iteration 2000: Cost 1.59e-03
Iteration 3000: Cost 1.08e-04
Iteration 4000: Cost 7.33e-06
Iteration 5000: Cost 4.98e-07
Iteration 6000: Cost 3.39e-08
Iteration 7000: Cost 2.30e-09
Iteration 8000: Cost 1.56e-10
Iteration 9000: Cost 1.06e-11
Iteration 9999: Cost 7.25e-13
Optimized w: [ 70.76856615 45.85153132 -26.63485178 -30.75481477]
Optimized b: 317.80
Assignment Lab 1 Page 8/22
Falculty of Aeronautical Engineering
New house features (original): [1200 3 1 40]
New house features (normalized): [-0.86621229 -0.39223227 -0.81649658 0.30108397]
Predicted price for the new house: 251.00 USD
LAB 06
import numpy as np
import matplotlib.pyplot as plt
# --- 0. Helper Functions (Custom Implementations) ---
def normal_equation_solve(X, y):
"""
Solves for linear regression parameters (w, b) using the Normal Equation.
Args:
X (ndarray (m,n)): Data, m examples, n features
y (ndarray (m,)): Target values
Returns:
w (ndarray (n,)): Optimized
weights b (scalar): Optimized
bias
"""
# Add a column of ones to X for the bias term (b)
# This transforms the problem from f_wb = X @ w + b to f_wb = X_b @ w_b
Assignment Lab 1 Page 9/22
Falculty of Aeronautical Engineering
# where X_b has a column of ones and w_b includes b
ones = np.ones((X.shape[0], 1))
X_b = np.concatenate((ones, X), axis=1) # X_b now is (m, n+1)
# Normal Equation formula: w_b = (X_b^T @ X_b)^(-1) @ X_b^T @ y
# Using np.linalg.inv for inverse and @ for matrix multiplication
try:
w_b = np.linalg.inv(X_b.T @ X_b) @ X_b.T @ y
except np.linalg.LinAlgError:
print("Warning: Matrix is singular or ill-conditioned. Normal Equation might be
unstable.")
# Fallback to least squares if inverse fails, or use pseudo-inverse
w_b = np.linalg.pinv(X_b.T @ X_b) @ X_b.T @ y
b = w_b[0] # The first element is the bias
w = w_b[1:] # The rest are the weights for features
return w, b
# --- 1. Data Definition ---
X_train = np.array([[2104, 5, 1, 45],
[1416, 3, 2, 40],
[852, 2, 1, 35],
[1600, 3, 2, 30],
[1900, 4, 1, 42]])
y_train = np.array([460, 232, 178, 320, 399])
X_features = ['size(sqft)', 'bedrooms', 'floors', 'age']
print("Original X_train:\n", X_train)
print("Original y_train:\n", y_train)
# --- 2. Solve using Normal Equation ---
w_final, b_final = normal_equation_solve(X_train, y_train)
print(f"\nOptimized w (from Normal Equation): {w_final}")
print(f"Optimized b (from Normal Equation): {b_final:.2f}")
# --- 3. Evaluate and Plot Results ---
# Predictions on the training set
y_pred = X_train @ w_final + b_final
print(f"\nPrediction on training set (first 4 samples):\n {y_pred[:4]}")
print(f"Target values (first 4 samples):\n {y_train[:4]}")
# Plotting
fig, ax = plt.subplots(1, X_train.shape[1], figsize=(12, 3), sharey=True)
for i in range(X_train.shape[1]):
ax[i].scatter(X_train[:, i], y_train, label='Target', marker='o', c='blue')
ax[i].scatter(X_train[:, i], y_pred, label='Prediction', marker='x', c='red')
ax[i].set_xlabel(X_features[i])
Assignment Lab 1 Page 10/22
Falculty of Aeronautical Engineering
ax[i].grid(True)
ax[0].set_ylabel("Price")
ax[0].legend()
fig.suptitle("Target vs. Prediction for each Feature (Normal Equation)")
plt.tight_layout(rect=[0, 0.03, 1, 0.95]) # Adjust layout to prevent suptitle overlap
plt.show()
# --- 4. Example Prediction with New Data ---
x_new_house = np.array([1200, 3, 1, 40])
# With Normal Equation, explicit feature scaling is often not strictly required for
convergence,
# but can help with numerical stability if feature scales vary wildly or matrix is ill-
conditioned.
# For simplicity, we directly use the new data without scaling here.
# If you observed numerical issues, you might implement and apply z-score normalization.
predicted_price_new = x_new_house @ w_final + b_final
print(f"\nNew house features: {x_new_house}")
print(f"Predicted price for the new house: {predicted_price_new:.2f} USD")
kết quả
Original X_train:
[[2104 5 1 45]
[1416 3 2 40]
[ 852 2 1 35]
[1600 3 2 30]
[1900 4 1 42]]
Original y_train:
[460 232 178 320 399]
Optimized w (from Normal Equation): [ 0.16373026 44.96111786 -54.36816525 -
5.7873633 ]
Optimized b (from Normal Equation): 205.51
Prediction on training set (first 4 samples):
[460. 232. 178. 320.]
Target values (first 4 samples):
[460 232 178 320]
New house features: [1200 3 1 40]
Assignment Lab 1 Page 11/22
Falculty of Aeronautical Engineering
Predicted price for the new house: 251.00 USD
Assignment Lab 1 Page 12/22