最終更新 10 months ago

這段 Python 程式碼 使用 scikit-learn (sklearn) 建立簡單的線性回歸模型,根據 過去幾天蘋果公司的股價 訓練模型,並 預測未來股價變化。

timmy revised this gist 10 months ago. Go to revision

No changes

timmy revised this gist 10 months ago. Go to revision

1 file changed, 0 insertions, 0 deletions

gistfile1.txt renamed to a_simple_linear_regression_model.py

File renamed without changes

timmy revised this gist 10 months ago. Go to revision

1 file changed, 28 insertions

gistfile1.txt(file created)

@@ -0,0 +1,28 @@
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]]))
Newer Older