| |
|
| | import numpy as np
|
| |
|
| |
|
| | x = np.array([1,2,3,4,5])
|
| | y = np.array([3,5,7,9,11])
|
| |
|
| |
|
| | w = np.sum((x - x.mean()) * (y - y.mean())) / np.sum((x - x.mean())**2)
|
| | b = y.mean() - w * x.mean()
|
| |
|
| |
|
| | y_pred = w * x + b
|
| |
|
| | print(f"Phương trình: y= {w: .1f}x + {b: .1f}")
|
| | print(f"Dự đoán:", y_pred)
|
| |
|
| |
|
| |
|
| |
|
| | import numpy as np
|
| | import matplotlib.pyplot as plt
|
| |
|
| |
|
| | x = np.array([1,2,3,4,5])
|
| | y = np.array([3,5,7,9,11])
|
| |
|
| |
|
| | w = np.sum((x - x.mean()) * (y - y.mean())) / np.sum((x - x.mean()) ** 2)
|
| | b = y.mean() - w * x.mean()
|
| |
|
| |
|
| | y_pred = w * x + b
|
| |
|
| |
|
| | print(f"Phương trình hồi quy: y={w: .1f}x + {b: .1f}")
|
| |
|
| |
|
| | 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()
|
| |
|
| |
|
| |
|
| | import numpy as np
|
| |
|
| | x = np.array([1,2,3,4,5,6,7])
|
| | y = np.array([1,5,9,13,17,21,25])
|
| |
|
| | w = np.sum((x - x.mean()) * (y - y.mean())) / np.sum((x - x.mean())**2)
|
| | b = y.mean() - w * x.mean()
|
| |
|
| | y_predict = w * x + b
|
| | print(f"Phương trình y= {w: .0f}x + {b: .0f}")
|
| | print(f"Dự đoán:", y_predict)
|
| |
|
| | 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()
|
| |
|
| |
|
| |
|
| | import numpy as np
|
| | import matplotlib.pyplot as plt
|
| |
|
| | np.random.seed(0)
|
| | x = np.arange(1,11)
|
| | y = 3 * x + 2 + np.random.randn(10) * 2
|
| |
|
| | w = np.sum((x - x.mean()) * (y - y.mean())) / np.sum((x - x.mean())**2)
|
| | b = y.mean() - w * x.mean()
|
| |
|
| | 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)
|
| |
|
| | 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()
|
| |
|
| |
|
| |
|
| | import numpy as np
|
| | import matplotlib.pyplot as plt
|
| |
|
| | x = np.array([1,2,3,4,5,6])
|
| | y = np.array([8,6,4,2,0,-2])
|
| |
|
| | w = np.sum((x - x.mean()) * (y - y.mean())) / np.sum((x - x.mean())**2)
|
| | b = y.mean() - w * x.mean()
|
| |
|
| | 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)
|
| |
|
| | 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()
|
| |
|
| |
|
| |
|
| | import numpy as np
|
| | import matplotlib.pyplot as plt
|
| |
|
| | np.random.seed(1)
|
| | x = np.linspace(-5,5,20)
|
| | y = x**2 + np.random.randn(20)*3
|
| |
|
| | w = np.sum((x - x.mean()) * (y - y.mean())) / np.sum((x - x.mean())**2)
|
| | b = y.mean() - w * x.mean()
|
| |
|
| | 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)
|
| |
|
| | 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()
|
| |
|
| |
|
| | import numpy as np
|
| | import matplotlib.pyplot as plt
|
| |
|
| | np.random.seed(2)
|
| | x = np.random.randint(1,100,20)
|
| | y = np.random.randint(1,100,20)
|
| |
|
| |
|
| | w = np.sum((x - x.mean()) * (y - y.mean())) / np.sum((x - x.mean())**2)
|
| | b = y.mean() - w * x.mean()
|
| |
|
| | 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)
|
| |
|
| | 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() |