This repository has been archived by the owner on Oct 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxgboost訓練範例.py
84 lines (62 loc) · 2.79 KB
/
xgboost訓練範例.py
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
import pandas as pd
from sklearn.model_selection import train_test_split
from xgboost import XGBClassifier
df = pd.read_excel('data.xlsx', index_col = 'DATE')
#print(df.columns)
def split_stock_data(stock_data, label_column, delete_column, test_size = 0.3,
random_state = 42):
X = stock_data.drop(delete_column, axis = 1)
feature_names = X.columns.tolist()
y = stock_data[label_column].values
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size = test_size,
random_state =
random_state) # 資料分割
return X_train, X_test, y_train, y_test, feature_names
label_column = 'LABEL' # 標籤欄位
# 刪除的欄位,欄位 'Next_?Day_Return' 中,? 應視特徵處理檔案參數 pre_day 調整
delete_column = ['LABEL', 'Next_2Day_Return']
# 切分資料為訓練與測試集
trainX, testX, trainY, testY, feature_names = split_stock_data(df, label_column,
delete_column)
import time
Xgboost = XGBClassifier()
start_time = time.time()
Xgboost.fit(trainX, trainY)
training_time = time.time() - start_time
test_predic = Xgboost.predict(testX) # 取得預測的結果
test_acc = Xgboost.score(testX, testY)
print('Xgboost測試集準確率 %.2f' % test_acc)
# 繪製特徵重要性圖
import matplotlib.pyplot as plt
# 將特徵名稱和重要性配對
feature_importance_pairs = list(zip(feature_names,
Xgboost.feature_importances_))
sorted_pairs = sorted(feature_importance_pairs, key = lambda x: x[1],
reverse = True)
# 提取排序後的特徵
sorted_feature_names, sorted_importances = zip(*sorted_pairs[:15]) # [:數字] 取得前幾名的特徵和重要性
print("依特徵重要性排序")
print(sorted_feature_names)
print()
for i, j in zip(sorted_feature_names, sorted_importances):
print(f"{i} ({j * 100:.2f} %)")
print()
# 繪製特徵重要性橫條圖
plt.rcParams['font.family'] = 'Microsoft JhengHei' # 設置中文字體
plt.figure(figsize = (12, 8))
bars = plt.barh(sorted_feature_names, sorted_importances, color = 'skyblue')
# 顯示每個橫條的數值
for bar in bars:
width = bar.get_width()
plt.text(width + 0.002, bar.get_y() + bar.get_height() / 2,
f'{width * 100:.2f} %',
va = 'center', ha = 'left', fontsize = 10)
plt.xlabel('特徵重要性')
plt.ylabel('特徵')
plt.title('特徵重要性')
plt.tight_layout(pad = 0.5)
plt.gca().invert_yaxis() # 反轉 y 軸,使重要性高的特徵顯示在上面
plt.show()
print(f"測試時間: {training_time:.2f} 秒")
print(f'模型準確率為 {test_acc:.3f}')