gistfile1.txt
· 666 B · Text
Brut
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
a_simple_linear_regression_model.py: 透過使用 Python 的 sklearn 套件,訓練一個簡單的線性回歸模型,並使用該模型預測蘋果公司的股價變化。
Author: Timmy
Copyright: Copyright 2022, Timmy
License: MIT
Version: 1.0
"""
# 引入必要的模組
import numpy as np
from sklearn.linear_model import LinearRegression
# 載入蘋果公司的股價資料
apple = np.array([155, 156, 157])
# 計算資料筆數
n = len(apple)
# 建立線性迴歸模型
model = LinearRegression().fit(np.arange(n).reshape((n, 1)), apple)
# 預測第3、4天的股價
print(model.predict([[3], [4]]))
| 1 | #!/usr/bin/env python |
| 2 | # -*- coding: utf-8 -*- |
| 3 | |
| 4 | |
| 5 | """ |
| 6 | a_simple_linear_regression_model.py: 透過使用 Python 的 sklearn 套件,訓練一個簡單的線性回歸模型,並使用該模型預測蘋果公司的股價變化。 |
| 7 | |
| 8 | Author: Timmy |
| 9 | Copyright: Copyright 2022, Timmy |
| 10 | License: MIT |
| 11 | Version: 1.0 |
| 12 | """ |
| 13 | |
| 14 | # 引入必要的模組 |
| 15 | import numpy as np |
| 16 | from sklearn.linear_model import LinearRegression |
| 17 | |
| 18 | # 載入蘋果公司的股價資料 |
| 19 | apple = np.array([155, 156, 157]) |
| 20 | |
| 21 | # 計算資料筆數 |
| 22 | n = len(apple) |
| 23 | |
| 24 | # 建立線性迴歸模型 |
| 25 | model = LinearRegression().fit(np.arange(n).reshape((n, 1)), apple) |
| 26 | |
| 27 | # 預測第3、4天的股價 |
| 28 | print(model.predict([[3], [4]])) |
| 29 |