File size: 6,229 Bytes
662d38c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | # Hồi quy tuyến tính đơn giản
import numpy as np
# Dữ liệu y = 2x + 1
x = np.array([1,2,3,4,5])
y = np.array([3,5,7,9,11])
# Tính hệ số w và b theo công thức thống kê
w = np.sum((x - x.mean()) * (y - y.mean())) / np.sum((x - x.mean())**2)
b = y.mean() - w * x.mean()
# Dự đoán
y_pred = w * x + b
print(f"Phương trình: y= {w: .1f}x + {b: .1f}")
print(f"Dự đoán:", y_pred)
# Vẽ đồ thị Linear Regression để trực quan hóa
# Hồi quy tuyến tính đơn giản + vẽ biểu đồ
import numpy as np
import matplotlib.pyplot as plt
# Dữ liệu y = 2x + 1
x = np.array([1,2,3,4,5])
y = np.array([3,5,7,9,11])
# Tính hệ số hồi quy
w = np.sum((x - x.mean()) * (y - y.mean())) / np.sum((x - x.mean()) ** 2)
b = y.mean() - w * x.mean()
# Dự đoán
y_pred = w * x + b
# In ra kết quả
print(f"Phương trình hồi quy: y={w: .1f}x + {b: .1f}")
# Vẽ biểu đồ
plt.scatter(x,y, color='blue', label='Dữ liệu thật')
plt.plot(x,y_pred, color='red', label='Đường hồi quy')
plt.xlabel('x axis')
plt.ylabel('y axis')
plt.title('Hồi quy tuyến tính đơn giản + Trực quan hóa')
plt.legend()
plt.show()
# Luyện tập
# 1. Tuyến tính nhưng đổi hệ số khác
import numpy as np
# Dữ liệu y = 4x - 3
x = np.array([1,2,3,4,5,6,7])
y = np.array([1,5,9,13,17,21,25]) # y = 4x - 3
# Tính hệ số w và b theo công thức thống kê
w = np.sum((x - x.mean()) * (y - y.mean())) / np.sum((x - x.mean())**2)
b = y.mean() - w * x.mean()
# Dự đoán
y_predict = w * x + b
print(f"Phương trình y= {w: .0f}x + {b: .0f}")
print(f"Dự đoán:", y_predict)
# Vẽ đồ thị để trực quan hóa
import matplotlib.pyplot as plt
plt.scatter(x,y, color='black', label='Real Data')
plt.plot(x, y_predict, color='red', label='Predicted Data')
plt.xlabel('X - Axis')
plt.ylabel('Y - Axis')
plt.title('Linear Regression (4x - 3)')
plt.legend()
plt.show()
# 2. Tuyến tính có nhiễu(noise) thực tế hơn
# Công thức y = 3x + 2 + noise
import numpy as np
import matplotlib.pyplot as plt
# Dữ liệu 3x + 2 + noise
np.random.seed(0)
x = np.arange(1,11)
y = 3 * x + 2 + np.random.randn(10) * 2 # thêm nhiễu ngẫu nhiên
# Tính hệ số w và b theo công thức thống kê
w = np.sum((x - x.mean()) * (y - y.mean())) / np.sum((x - x.mean())**2)
b = y.mean() - w * x.mean()
# Dự đoán
y_predict = w * x + b
print(f"Phương trình + nhiễu: y = {w: .1f}x + {b: .1f}")
print(f"Dự đoán chính xác:", y_predict)
# Vẽ đồ thị trực quan hóa với nhiễu
plt.scatter(x,y, color='blue', label='Real Data + Noise')
plt.plot(x,y_predict, color='red', label='Predicted Data + Noise')
plt.xlabel('X - Axis')
plt.ylabel('Y - Axis')
plt.title('Linear Regression AND Noise')
plt.legend()
plt.show()
# 3. Tuyến tính nghịch (hệ số âm)
# Công thức y = -2x + 10
import numpy as np
import matplotlib.pyplot as plt
# Dữ liệu tuyến tính nghịch
x = np.array([1,2,3,4,5,6])
y = np.array([8,6,4,2,0,-2])
# Tính hệ số w và b theo công thức thống kê
w = np.sum((x - x.mean()) * (y - y.mean())) / np.sum((x - x.mean())**2)
b = y.mean() - w * x.mean()
# Dự đoán chính xác
y_pred = w * x + b
print(f"Phương trình tuyến tính nghịch: y = {w: .0f}x + {b: .0f}")
print(f"Dự đoán chính xác:", y_pred)
# Vẽ đồ thị trực quan hóa với tuyến tính nghịch
plt.scatter(x,y, color='red', label='Real Data')
plt.plot(x,y_pred, color='blue', label='Predicted Data')
plt.xlabel('X - Axis')
plt.ylabel('Y - Axis')
plt.title('Linear Regression AND Reversed')
plt.legend()
plt.show()
# 4. Không tuyến tính (để xem hồi quy tuyến tính kém thế nào)
# Công thức: y = x**2 + noise
import numpy as np
import matplotlib.pyplot as plt
# Dữ liệu không tuyến tính
np.random.seed(1)
x = np.linspace(-5,5,20) # Giải thích: .linspace(start, stop, num) với num=number là số giá trị cách đều nhau
y = x**2 + np.random.randn(20)*3 # Sinh ra 20 số ngẫu nhiên tuân theo phân phối chuẩn (Gaussian) → trung bình = 0, độ lệch chuẩn = 1; Nhân với 3 để phóng đại độ nhiễu (noise) → sai số ngẫu nhiên có độ lệch chuẩn ≈ 3 → càng nhân lớn, dữ liệu càng "rải" quanh đường parabol. y là giá trị theo hàm bậc hai, có thêm nhiễu ngẫu nhiên để giống dữ liệu thật. Đây là mối quan hệ phi tuyến tính giữa x và y.
# Tính hệ số w và b theo công thức thống kê
w = np.sum((x - x.mean()) * (y - y.mean())) / np.sum((x - x.mean())**2)
b = y.mean() - w * x.mean()
# Dự đoán chính xác
y_pred = w * x + b
print(f"Phương trình không tuyến tính: y={w: 1f}x + {b: .1f}")
print(f"Dự đoán chính xác:", y_pred)
# Vẽ đồ thị để trực quan hóa không tuyến tính
plt.scatter(x,y, color='red', label='Real Data with no linear')
plt.plot(x,y_pred, color='blue', label='Predicted Data with no linear')
plt.xlabel('X - Axis')
plt.ylabel('Y - Axis')
plt.title(' No Linear Regression')
plt.legend()
plt.show()
# 5. Dữ liệu ngẫu nhiên để luyện hiểu độ phù hợp (low R**2)
import numpy as np
import matplotlib.pyplot as plt
# Dữ liệu ngẫu nhiên
np.random.seed(2)
x = np.random.randint(1,100,20) # Giải thích: np.random.randint(low,high,size) với size là tổng số ptử muốn tạo ra
y = np.random.randint(1,100,20)
# Tính hệ số w và b theo công thức thống kê
w = np.sum((x - x.mean()) * (y - y.mean())) / np.sum((x - x.mean())**2)
b = y.mean() - w * x.mean()
# Dự đoán chính xác
y_pred = w * x + b
print(f"Phương trình tuyến tính + Random Data: y={w: 1f}x + {b: .1f}")
print(f"Dự đoán chính xác:", y_pred)
# Vẽ đồ thị để trực quan hóa không tuyến tính
plt.scatter(x,y, color='red', label='Real Data + Random')
plt.plot(x,y_pred, color='blue', label='Predicted Data + Random')
plt.xlabel('X - Axis')
plt.ylabel('Y - Axis')
plt.title('Linear Regression With Random Data')
plt.legend()
plt.show() |