使用支援向量機預測股票價格走勢


在這篇文章中,我們將學習如何使用支援向量機預測股票價格走勢。

機器學習是一種人工智慧應用,它正在改善世界在各個學科中的運作方式。從本質上講,它是一種演算法或模型,它識別特定資料集中的模式,然後在通用資料上預測學習到的模式。通俗地說,它是機器人學習模式並透過經驗進行調整以得出正確且可重複結論的概念。在這篇文章中,我們將探討使用支援向量機預測股票價格走勢。讓我們開始吧。

安裝庫並匯入它們

第一步,我們只需要安裝庫並匯入它們。

!pip install pandas !pip install numpy ! pip install scikit-learn import pandas as pd import numpy as np from sklearn.svm import SVC from sklearn.metrics import accuracy_score import matplotlib.pyplot as plt import warnings

下載和讀取股票資料集

讀取檔案中的資料集是下一個任務。您可以從這裡下載資料集,該檔案將儲存在外部儲存中。我們使用 pandas 讀取資料集。

示例

df = pd.read_csv('/content/sample_data/RELIANCE.csv') df.head()

輸出

Date	Symbol	Series	Prev Close	Open	High	Low	Last	Close	VWAP	Volume	Turnover	Trades	Deliverable Volume	%Deliverble
0	2000-01-03	RELIANCE	EQ	233.05	237.50	251.70	237.50	251.70	251.70	249.37	4456424	1.111319e+14	NaN	NaN	NaN
1	2000-01-04	RELIANCE	EQ	251.70	258.40	271.85	251.30	271.85	271.85	263.52	9487878	2.500222e+14	NaN	NaN	NaN
2	2000-01-05	RELIANCE	EQ	271.85	256.65	287.90	256.65	286.75	282.50	274.79	26833684	7.373697e+14	NaN	NaN	NaN
3	2000-01-06	RELIANCE	EQ	282.50	289.00	300.70	289.00	293.50	294.35	295.45	15682286	4.633254e+14	NaN	NaN	NaN
4	2000-01-07	RELIANCE	EQ	294.35	295.00	317.90	293.00	314.50	314.55	308.91	19870977	6.138388e+14	NaN	NaN	NaN

資料準備

在使用資料進行分析之前,日期列應充當索引。

示例

# Changes The Date column as index columns df.index = pd.to_datetime(df['Date']) df # drop The original date column df = df.drop(['Date'], axis='columns') df

輸出

	Symbol	Series	Prev Close	Open	High	Low	Last	Close	VWAP	Volume	Turnover	Trades	Deliverable Volume	%Deliverble
Date														
2000-01-03	RELIANCE	EQ	233.05	237.50	251.70	237.50	251.70	251.70	249.37	4456424	1.111319e+14	NaN	NaN	NaN
2000-01-04	RELIANCE	EQ	251.70	258.40	271.85	251.30	271.85	271.85	263.52	9487878	2.500222e+14	NaN	NaN	NaN
2000-01-05	RELIANCE	EQ	271.85	256.65	287.90	256.65	286.75	282.50	274.79	26833684	7.373697e+14	NaN	NaN	NaN
2000-01-06	RELIANCE	EQ	282.50	289.00	300.70	289.00	293.50	294.35	295.45	15682286	4.633254e+14	NaN	NaN	NaN
2000-01-07	RELIANCE	EQ	294.35	295.00	317.90	293.00	314.50	314.55	308.91	19870977	6.138388e+14	NaN	NaN	NaN
...	...	...	...	...	...	...	...	...	...	...	...	...	...	...
2020-05-22	RELIANCE	EQ	1441.25	1451.80	1458.00	1426.50	1433.00	1431.55	1442.31	17458503	2.518059e+15	388907.0	4083814.0	0.2339
2020-05-26	RELIANCE	EQ	1431.55	1448.15	1449.70	1416.30	1426.00	1424.05	1428.70	15330793	2.190317e+15	341795.0	7437964.0	0.4852
2020-05-27	RELIANCE	EQ	1424.05	1431.00	1454.00	1412.00	1449.85	1445.55	1430.20	16460764	2.354223e+15	348477.0	6524302.0	0.3964
2020-05-28	RELIANCE	EQ	1445.55	1455.00	1479.75	1449.00	1471.05	1472.25	1467.50	18519252	2.717698e+15	405603.0	8377100.0	0.4523
2020-05-29	RELIANCE	EQ	1472.25	1468.00	1472.00	1452.65	1470.00	1464.40	1462.79	18471770	2.702029e+15	300018.0	10292573.0	0.5572

解釋性因素

使用解釋性或獨立因素預測值響應變數。用於預測的變數儲存在 X 資料集中。變數如“開盤價-收盤價”和“最高價-最低價”是 X 的一部分。這些可以被視為演算法將用來預測未來一天趨勢的標記。隨意包含更多指標並評估結果。

示例

# Create predictor variables df['Open-Close'] = df.Open - df.Close df['High-Low'] = df.High - df.Low # Store all predictor variables in a variable X X = df[['Open-Close', 'High-Low']] X.head()

輸出

	Open-Close	High-Low
Date		
2000-01-03	-14.20	14.20
2000-01-04	-13.45	20.55
2000-01-05	-25.85	31.25
2000-01-06	-5.35	11.70
2000-01-07	-19.55	24.90

目標變數

目標資料集 y 包含相應的交易訊號,機器學習演算法將嘗試預測該訊號。

y = np.where(df['Close'].shift(-1) > df['Close'], 1, 0)

將資料拆分為訓練集和測試集

將有不同的資料集用於訓練和測試。

split_percentage = 0.8 split = int(split_percentage*len(df)) # Train data set X_train = X[:split] y_train = y[:split] # Test data set X_test = X[split:] y_test = y[split:]

支援向量分類器

現在是時候使用支援向量分類器了。

示例

cls = SVC().fit(X_train, y_train) df['prediction'] = cls.predict(X) print(df['prediction'])

輸出

Date
2000-01-03    1
2000-01-04    1
2000-01-05    1
2000-01-06    1
2000-01-07    1
             ..
2020-05-22    1
2020-05-26    1
2020-05-27    1
2020-05-28    1
2020-05-29    1
Name: prediction, Length: 5075, dtype: int64

結論

支援向量機是一種流行且節省空間的用於分類和迴歸應用的方法,它利用幾何概念來解決我們的問題。我們還使用 SVM 演算法來預測股票價格走勢的方向。在企業界,股票價格預測非常重要,當我們自動化此過程時,它會提高人們對該問題的認識。

更新於: 2022年12月1日

1K+ 閱讀量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.