Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
2962 lines (2562 sloc) 75.1 KB

巨量資料教我們的: 一命二運三風水。

組員: 葉登元、洪志穎、徐上元。

指導教授: 高啟昌 老師。

前言

孔子曰:不知命,無以為君子也。

這裡的命,從大方面來講包括天命,指上天意和 民族、國家的命運,從小方面來講,就是指個人的命運。

命是車,運是路。命運如舟與水。

所謂的運,意旨所謂的環境周遭的變化所造成的影響,攸關於市場波動與眾人心理,因此我們納入VIX指數。

所謂的風水,意旨事與物之相對位置,我們納入cnn來預測隔日k線圖之收盤價。

願能藉由古人的智慧,預測股價走勢及報酬。

PART1

利用台股VIX指數估計台指期的報酬。

並探討VIX高低和台指期交易量。

PART2

使用cnn預測(2330台積電)之k線圖。

---------------------------------------------

PART1

載入模組

套件需求: pandas,sklearn中的RandomForest分類、回歸、交叉驗證。

from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
from sklearn.linear_model import LinearRegression

import pandas as pd

讀取檔案

資料來源於TEJ

台指期貨標的VIX指數 & 台指期

期間為:2017/6/19~2018/6/15 將資料切為測試與訓練資料,

測試資料為2017/6/19到2018/6/15。訓練資料為2018年6/15倒回去2017年6/19。

並修改訓練與測試資料欄位名稱為: VIX、VIX日期、VIX開盤價、VIX最高價、VIX最低價、VIX收盤價、台指期名稱、台指期日期、台指期報酬、台指期收盤、台指期成交量。

test = pd.read_csv("test.csv", error_bad_lines=False)
train = pd.read_csv("train.csv", error_bad_lines=False)
#submit = pd.read_csv('gender_submission.csv')

改欄位名

train.columns=['vix','vix_date','vix_open','vix_high','vix_low','vix_close','future','fu_date','fu_ret','fu_colse','fu_vol']

查看資料型態

train.head()
<style> .dataframe thead tr:only-child th { text-align: right; }
.dataframe thead th {
    text-align: left;
}

.dataframe tbody tr th {
    vertical-align: top;
}
</style>
vix vix_date vix_open vix_high vix_low vix_close future fu_date fu_ret fu_colse fu_vol
0 TXO_N5 2018/6/15 14.21 14.90 13.81 14.13 TX201806 ??? 2018/06 2018/6/15 0.6281 11055 145449
1 TXO_N5 2018/6/14 14.31 14.35 13.87 14.21 TX201807 ??? 2018/07 2018/6/15 0.6052 10808 24657
2 TXO_N5 2018/6/13 14.19 14.36 14.01 14.01 TX201806 ??? 2018/06 2018/6/14 -1.4445 10985 143124
3 TXO_N5 2018/6/12 14.49 14.55 14.15 14.42 TX201807 ??? 2018/07 2018/6/14 -1.3954 10742 11364
4 TXO_N5 2018/6/11 13.86 14.45 13.80 14.45 TX201806 ??? 2018/06 2018/6/13 0.0449 11146 114215
test.head()
<style> .dataframe thead tr:only-child th { text-align: right; }
.dataframe thead th {
    text-align: left;
}

.dataframe tbody tr th {
    vertical-align: top;
}
</style>
vix vix_date vix_open vix_high vix_low vix_close future fu_date fu_ret fu_close fu_volum
0 TXO_N5 20170619 10.32 11.02 9.96 11.02 TX201803 ??? 2018/03 20170619 1.0289 9819 168204
1 TXO_N5 20170620 9.97 10.04 9.71 9.86 TX201803 ??? 2018/03 20170620 1.0490 9922 184342
2 TXO_N5 20170621 9.88 10.13 9.82 9.93 TX201803 ??? 2018/03 20170621 0.1613 9938 180584
3 TXO_N5 20170622 10.30 10.38 9.78 10.02 TX201803 ??? 2018/03 20170622 0.6239 10002 121899
4 TXO_N5 20170623 9.90 10.40 9.80 10.40 TX201803 ??? 2018/03 20170623 -0.1200 9988 105120

查看資料型態,探段資料筆數以及型態

若有不同需要去遺失值等資料處理

train.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 936 entries, 0 to 935
Data columns (total 11 columns):
vix          455 non-null object
vix_date     455 non-null object
vix_open     455 non-null float64
vix_high     455 non-null float64
vix_low      455 non-null float64
vix_close    455 non-null float64
future       936 non-null object
fu_date      936 non-null object
fu_ret       927 non-null float64
fu_colse     936 non-null int64
fu_vol       936 non-null int64
dtypes: float64(5), int64(2), object(4)
memory usage: 80.5+ KB
test.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 247 entries, 0 to 246
Data columns (total 11 columns):
vix          247 non-null object
vix_date     247 non-null int64
vix_open     247 non-null float64
vix_high     247 non-null float64
vix_low      247 non-null float64
vix_close    247 non-null float64
future       247 non-null object
fu_date      247 non-null int64
fu_ret       247 non-null float64
fu_close     247 non-null int64
fu_volum     247 non-null int64
dtypes: float64(5), int64(4), object(2)
memory usage: 21.3+ KB

查看資料分佈,以方便之後過濾極值

train.describe()
<style> .dataframe thead tr:only-child th { text-align: right; }
.dataframe thead th {
    text-align: left;
}

.dataframe tbody tr th {
    vertical-align: top;
}
</style>
vix_open vix_high vix_low vix_close fu_ret fu_colse fu_vol
count 455.000000 455.000000 455.000000 455.000000 927.000000 936.000000 936.000000
mean 12.448879 12.922462 11.987055 12.365033 0.064679 10555.685897 37793.850427
std 3.300000 4.318183 2.950731 3.198237 0.729217 279.185047 59756.884565
min 8.080000 8.190000 7.730000 7.820000 -4.995400 9819.000000 17.000000
25% 10.250000 10.455000 9.945000 10.140000 -0.319150 10335.750000 279.750000
50% 11.480000 11.910000 11.160000 11.530000 0.117000 10529.500000 1749.500000
75% 13.715000 14.015000 13.270000 13.805000 0.497100 10761.500000 89802.000000
max 34.460000 57.360000 29.030000 30.070000 2.997600 11227.000000 409245.000000
test.describe()
<style> .dataframe thead tr:only-child th { text-align: right; }
.dataframe thead th {
    text-align: left;
}

.dataframe tbody tr th {
    vertical-align: top;
}
</style>
vix_date vix_open vix_high vix_low vix_close fu_date fu_ret fu_close fu_volum
count 2.470000e+02 247.000000 247.000000 247.000000 247.000000 2.470000e+02 247.000000 247.000000 247.000000
mean 2.017509e+07 13.104737 13.622834 12.649717 13.116842 2.017509e+07 0.057362 10548.178138 143502.190283
std 4.685796e+03 3.347928 4.255705 2.930674 3.245273 4.685796e+03 0.774394 286.091933 39598.891933
min 2.017062e+07 8.440000 8.860000 7.820000 7.820000 2.017062e+07 -4.995400 9819.000000 24665.000000
25% 2.017091e+07 10.915000 11.245000 10.615000 10.945000 2.017091e+07 -0.351200 10331.000000 119712.000000
50% 2.017121e+07 12.090000 12.530000 11.780000 12.150000 2.017121e+07 0.113100 10580.000000 139254.000000
75% 2.018032e+07 14.350000 14.780000 13.785000 14.165000 2.018032e+07 0.510700 10758.500000 166639.500000
max 2.018062e+07 34.460000 54.920000 25.300000 30.070000 2.018062e+07 2.982800 11161.000000 433346.000000

計算技術指標

我們願使用KD、MACD、RSI技術指標的值當作分類依據,

因此需載numpy、talib,技術指標於talib運算後與之前欄位項目一同匯入t1,

並移除空值與遺漏值

其中利用技術指標是採用VIX指數來做為計算

想看看以往都是利用股價計算去做的技術指標在VIX終能否適用

import talib
import numpy
def talib2df(talib_output):
    if type(talib_output) == list:
        ret = pd.DataFrame(talib_output).transpose()
    else:
        ret = pd.Series(talib_output)
    ret.index = test['vix_close'].index
    return ret;
t1 = {
    'close':test.vix_close.dropna().astype(float),
    'open':test.vix_open.dropna().astype(float),
    'high':test.vix_high.dropna().astype(float),
    'low':test.vix_low.dropna().astype(float),
    'volume': test.fu_volum.dropna().astype(float)    
}

採用 KD/MACD/RSI指標

KD = talib2df(talib.abstract.STOCH(t1, fastk_period=9))
MACD = talib2df(talib.abstract.MACD(t1))
RSI = talib2df(talib.abstract.RSI(t1))
t1=pd.DataFrame(t1)
t1 = pd.concat([test,KD,MACD,RSI], axis=1)
t1.columns=['vix','vix_date','vix_open','vix_high','vix_low','vix_close','future','fu_date','fu_ret','fu_colse','fu_vol','k','d','dif12','dif26','macd','rsi']
t1.head(50)
<style> .dataframe thead tr:only-child th { text-align: right; }
.dataframe thead th {
    text-align: left;
}

.dataframe tbody tr th {
    vertical-align: top;
}
</style>
vix vix_date vix_open vix_high vix_low vix_close future fu_date fu_ret fu_colse fu_vol k d dif12 dif26 macd rsi
0 TXO_N5 20170619 10.32 11.02 9.96 11.02 TX201803 ??? 2018/03 20170619 1.0289 9819 168204 NaN NaN NaN NaN NaN NaN
1 TXO_N5 20170620 9.97 10.04 9.71 9.86 TX201803 ??? 2018/03 20170620 1.0490 9922 184342 NaN NaN NaN NaN NaN NaN
2 TXO_N5 20170621 9.88 10.13 9.82 9.93 TX201803 ??? 2018/03 20170621 0.1613 9938 180584 NaN NaN NaN NaN NaN NaN
3 TXO_N5 20170622 10.30 10.38 9.78 10.02 TX201803 ??? 2018/03 20170622 0.6239 10002 121899 NaN NaN NaN NaN NaN NaN
4 TXO_N5 20170623 9.90 10.40 9.80 10.40 TX201803 ??? 2018/03 20170623 -0.1200 9988 105120 NaN NaN NaN NaN NaN NaN
5 TXO_N5 20170626 10.39 12.17 10.32 12.17 TX201803 ??? 2018/03 20170626 2.1526 10203 195349 NaN NaN NaN NaN NaN NaN
6 TXO_N5 20170627 11.89 12.55 11.75 12.12 TX201803 ??? 2018/03 20170627 -0.1078 10192 121428 NaN NaN NaN NaN NaN NaN
7 TXO_N5 20170628 12.48 12.60 12.25 12.52 TX201803 ??? 2018/03 20170628 -1.0302 10087 165922 NaN NaN NaN NaN NaN NaN
8 TXO_N5 20170629 12.38 12.38 11.79 11.84 TX201803 ??? 2018/03 20170629 0.2677 10115 134893 NaN NaN NaN NaN NaN NaN
9 TXO_N5 20170630 12.76 12.76 12.11 12.33 TX201712 ??? 2017/12 20170630 -0.4141 10103 129676 NaN NaN NaN NaN NaN NaN
10 TXO_N5 20170703 12.62 13.01 12.05 12.07 TX201803 ??? 2018/03 20170703 0.3078 10101 109556 NaN NaN NaN NaN NaN NaN
11 TXO_N5 20170704 11.98 12.16 11.80 12.10 TX201803 ??? 2018/03 20170704 -0.7029 10030 143772 NaN NaN NaN NaN NaN NaN
12 TXO_N5 20170705 11.98 12.17 11.57 12.17 TX201803 ??? 2018/03 20170705 0.7278 10103 172653 72.185411 75.076025 NaN NaN NaN NaN
13 TXO_N5 20170706 11.96 12.42 11.94 12.23 TX201803 ??? 2018/03 20170706 -0.3563 10067 134041 72.220706 73.538272 NaN NaN NaN NaN
14 TXO_N5 20170707 12.51 12.69 12.40 12.40 TX201803 ??? 2018/03 20170707 -0.5761 10009 123496 67.491461 70.632526 NaN NaN NaN 62.147887
15 TXO_N5 20170710 12.82 12.83 12.31 12.31 TX201803 ??? 2018/03 20170710 0.0599 10015 114699 60.010498 66.574222 NaN NaN NaN 61.105193
16 TXO_N5 20170711 12.17 12.17 11.68 11.72 TX201803 ??? 2018/03 20170711 1.3480 10150 149796 39.814815 55.772258 NaN NaN NaN 54.633954
17 TXO_N5 20170712 11.81 11.92 11.68 11.80 TX201803 ??? 2018/03 20170712 -0.0788 10142 119813 25.925926 41.917080 NaN NaN NaN 55.324827
18 TXO_N5 20170713 11.86 11.91 11.43 11.53 TX201803 ??? 2018/03 20170713 0.7888 10222 155889 10.906001 25.548914 NaN NaN NaN 52.423152
19 TXO_N5 20170714 11.54 11.60 11.14 11.19 TX201803 ??? 2018/03 20170714 -0.1467 10207 89843 8.419972 15.083966 NaN NaN NaN 48.942094
20 TXO_N5 20170717 11.43 12.09 11.16 12.09 TX201803 ??? 2018/03 20170717 0.2253 10230 178952 21.833571 13.719848 NaN NaN NaN 57.068719
21 TXO_N5 20170718 11.12 12.10 10.94 12.10 TX201803 ??? 2018/03 20170718 0.2639 10257 132979 40.182420 23.478654 NaN NaN NaN 57.150327
22 TXO_N5 20170719 10.96 11.68 10.84 11.26 TX201803 ??? 2018/03 20170719 0.3900 10297 155196 46.231402 36.082464 NaN NaN NaN 48.764750
23 TXO_N5 20170720 10.76 10.76 10.48 10.48 TX201806 ??? 2018/06 20170720 0.0000 10275 95765 27.493730 37.969184 NaN NaN NaN 42.525107
24 TXO_N5 20170721 10.53 10.71 10.34 10.54 TX201806 ??? 2018/06 20170721 -0.6326 10210 99432 10.678163 28.134432 NaN NaN NaN 43.127936
25 TXO_N5 20170724 10.95 10.99 10.41 10.58 TX201806 ??? 2018/06 20170724 0.1763 10228 99477 8.188442 15.453445 NaN NaN NaN 43.552996
26 TXO_N5 20170725 10.58 10.70 10.32 10.70 TX201806 ??? 2018/06 20170725 0.0489 10233 93023 15.304547 11.390384 NaN NaN NaN 44.883867
27 TXO_N5 20170726 10.30 10.32 10.10 10.20 TX201806 ??? 2018/06 20170726 -0.4691 10185 143458 13.328226 12.273738 NaN NaN NaN 40.589652
28 TXO_N5 20170727 10.03 10.09 9.73 9.92 TX201806 ??? 2018/06 20170727 1.1095 10298 135687 11.455064 13.362612 NaN NaN NaN 38.375439
29 TXO_N5 20170728 10.02 10.50 10.02 10.50 TX201806 ??? 2018/06 20170728 -1.0293 10192 132484 15.168776 13.317356 NaN NaN NaN 45.061024
30 TXO_N5 20170731 10.56 10.90 10.24 10.90 TX201806 ??? 2018/06 20170731 0.1374 10206 111093 33.502110 20.041983 NaN NaN NaN 49.157663
31 TXO_N5 20170801 10.23 10.96 10.15 10.26 TX201806 ??? 2018/06 20170801 0.0784 10214 128371 44.850981 31.173956 NaN NaN NaN 43.560766
32 TXO_N5 20170802 10.25 14.83 10.16 14.83 TX201806 ??? 2018/06 20170802 0.7539 10291 123147 67.354497 48.569196 NaN NaN NaN 69.907775
33 TXO_N5 20170803 18.90 18.90 10.80 10.96 TX201806 ??? 2018/06 20170803 -0.4567 10244 139493 51.825599 54.677026 0.021543 -0.124580 0.146123 49.033124
34 TXO_N5 20170804 11.27 11.43 11.22 11.40 TX201806 ??? 2018/06 20170804 0.2831 10273 85938 43.874955 54.351684 0.022980 -0.095068 0.118048 50.830807
35 TXO_N5 20170807 11.44 11.51 11.10 11.33 TX201806 ??? 2018/06 20170807 0.8274 10358 95562 16.357688 37.352747 0.018261 -0.072402 0.090663 50.525480
36 TXO_N5 20170808 11.12 11.44 10.98 11.41 TX201806 ??? 2018/06 20170808 -0.1255 10345 121843 17.993457 26.075367 0.020736 -0.053774 0.074511 50.888556
37 TXO_N5 20170809 11.99 12.49 11.99 12.15 TX201806 ??? 2018/06 20170809 -0.6380 10279 145957 19.918433 18.089859 0.081471 -0.026725 0.108197 54.234230
38 TXO_N5 20170810 11.79 13.39 11.75 13.23 TX201806 ??? 2018/06 20170810 -1.1577 10160 202500 25.835699 21.249196 0.214281 0.021476 0.192805 58.660546
39 TXO_N5 20170811 14.74 15.23 14.18 14.38 TX201803 ??? 2018/03 20170811 -0.3133 10182 161301 35.843115 27.199082 0.407630 0.098707 0.308923 62.787668
40 TXO_N5 20170814 13.34 14.03 13.34 13.87 TX201806 ??? 2018/06 20170814 -0.7994 10052 216354 41.997123 34.558646 0.513785 0.181722 0.332063 59.930175
41 TXO_N5 20170815 13.50 13.51 13.05 13.05 TX201806 ??? 2018/06 20170815 0.7163 10124 191986 39.523049 39.121096 0.525687 0.250515 0.275172 55.552508
42 TXO_N5 20170816 13.09 15.16 12.76 12.77 TX201806 ??? 2018/06 20170816 -0.2864 10095 187408 37.447979 39.656051 0.506685 0.301749 0.204936 54.099332
43 TXO_N5 20170817 12.45 12.53 11.67 11.98 TX201806 ??? 2018/06 20170817 0.5547 10151 127147 31.141612 36.037547 0.423003 0.326000 0.097003 50.116018
44 TXO_N5 20170818 12.87 13.35 12.39 12.67 TX201806 ??? 2018/06 20170818 -0.3448 10116 145088 35.137255 34.575615 0.407663 0.342333 0.065330 53.347031
45 TXO_N5 20170821 13.03 13.26 12.72 13.03 TX201806 ??? 2018/06 20170821 0.0000 10116 112143 33.832122 33.370330 0.419716 0.357809 0.061907 54.985244
46 TXO_N5 20170822 12.85 12.85 11.84 12.08 TX201806 ??? 2018/06 20170822 0.7711 10194 114930 29.827936 32.932437 0.348593 0.355966 -0.007373 49.996026
47 TXO_N5 20170823 11.52 11.81 11.47 11.75 TX201806 ??? 2018/06 20170823 0.1275 10207 136761 19.055303 27.571787 0.262572 0.337287 -0.074715 48.354680
48 TXO_N5 20170824 11.52 11.55 11.18 11.35 TX201806 ??? 2018/06 20170824 0.7740 10286 121528 7.745006 18.876082 0.160276 0.301885 -0.141609 46.367625
49 TXO_N5 20170825 11.22 11.50 11.10 11.29 TX201806 ??? 2018/06 20170825 0.3889 10326 109142 5.465989 10.755433 0.073517 0.256211 -0.182695 46.061859

移除空值(由於技術指標計算後前幾天會有空值情況)

沒有移除會有錯誤產生

t1 = t1.query('vix_date > 20170803')

先載入sklearn的模型選擇、預先處理、指標與ensemble模型後我們開始進行分類。

我們將(交易量增加的)、(報酬率>0)、(波動率指數增加的)設為1; 因為想要用類別(不是連續)的變數,

因此需要載入虛擬變數(dummy variables),最後測試並建立volume的數據。

import numpy as np

from sklearn import model_selection, ensemble, preprocessing, metrics

移除遺漏值、與設交易量增加&報酬大於零&VIX指數上漲 為 1

df=t1
df['pre_vol']=(df.fu_vol - df.fu_vol.shift(1)) > 0
df['pre_ret']=(df.fu_ret -  0 ) > 0
df['pre_vix_close']=(df.vix_close - df.vix_close.shift(1)) > 0

df=df.dropna()

創造 dummy variables

label_encoder = preprocessing.LabelEncoder()
encoded_label = label_encoder.fit_transform(df["future"])
encoded_label2 = label_encoder.fit_transform(df["vix"])
df['future']= encoded_label
df['vix'] = encoded_label2

測試還有建立的數據_VOLUME

selected_features = ['vix_date','vix_open','vix_high','vix_low','vix_close','fu_vol','k','d','macd','rsi','fu_ret','future']
pre_vol_X = df[selected_features]
pre_vol_y = df['pre_vol']
train_X, test_X, train_y, test_y = model_selection.train_test_split(pre_vol_X, pre_vol_y, test_size = 0.3)

接下來我們載入 random forest 模型並進行預測

forest = ensemble.RandomForestClassifier(n_estimators = 300)
forest_fit = forest.fit(train_X, train_y)

預測

test_y_predicted = forest.predict(test_X)
#test_y_predicted

績效

accuracy = metrics.accuracy_score(test_y, test_y_predicted)
#print(accuracy)
fpr, tpr, thresholds = metrics.roc_curve(test_y, test_y_predicted)
auc = metrics.auc(fpr, tpr)
#print(auc)
print('準確率: {}'.format(auc))
print('AUC值: {}'.format(accuracy))
準確率: 0.6533333333333334
AUC值: 0.65625

預測期貨交易量

import numpy as np
today_X = df[selected_features]
today_y_predicted = forest.predict(today_X)
proba = forest.predict_proba(today_X)
print('隔日交易量: ' + format(np.where(today_y_predicted==True,'','')[0]))
print( '明日增加的機率: {}'.format(proba[0][1]))
隔日交易量: 減
明日增加的機率: 0.15666666666666668

量預測vix ,成功率65%。

建立預測報酬ret

selected_features_ret = ['vix_date','fu_date','vix_high','vix_low','fu_vol','k','d','macd','rsi','future']
pre_ret_X = df[selected_features_ret]
pre_ret_y = df['pre_ret']
train_X2, test_X2, train_y2, test_y2 = model_selection.train_test_split(pre_ret_X, pre_ret_y, test_size = 0.3)
forest = ensemble.RandomForestClassifier(n_estimators = 100)
forest_fit = forest.fit(train_X2, train_y2)
test_y2_predicted = forest.predict(test_X2)
accuracy = metrics.accuracy_score(test_y2, test_y2_predicted)
#print(accuracy)
fpr, tpr, thresholds = metrics.roc_curve(test_y2, test_y2_predicted)
auc = metrics.auc(fpr, tpr)
#print(auc)
print('準確率: {}'.format(auc))
print('AUC值: {}'.format(accuracy))
準確率: 0.5757085020242915
AUC值: 0.56

Vix預測報酬,成功率56%。

today_X2 = df[selected_features_ret]
today_y2_predicted = forest.predict(today_X2)
proba = forest.predict_proba(today_X2)
print('預期隔日報酬: ' + format(np.where(today_y_predicted==True,'','')[0]))
print( '明日賺的機率: {}'.format(proba[0][1]))
預期隔日報酬: 負
明日賺的機率: 0.43

建立預測VIX

#虛擬變數
label_encoder = preprocessing.LabelEncoder()
encoded_label = label_encoder.fit_transform(df["vix_close"])
selected_features_vix = ['vix_date','vix_open','vix_high','vix_low','vix_close','fu_vol','k','d','macd','rsi','fu_ret']
pre_vix_close_X = df[selected_features_vix]
pre_vix_close_y = df['pre_vix_close']
train_X3, test_X3, train_y3, test_y3 = model_selection.train_test_split(pre_vix_close_X, pre_vix_close_y, test_size = 0.3)
forest = ensemble.RandomForestClassifier(n_estimators = 300)
forest_fit = forest.fit(train_X3, train_y3)
test_y3_predicted = forest.predict(test_X3)
#test_y3_predicted
accuracy = metrics.accuracy_score(test_y3, test_y3_predicted)
#print(accuracy)
fpr, tpr, thresholds = metrics.roc_curve(test_y3, test_y3_predicted)
auc = metrics.auc(fpr, tpr)
#print(auc)
print('準確率: {}'.format(auc))
print('AUC值: {}'.format(accuracy))
準確率: 0.7598039215686274
AUC值: 0.765625

用vix技術指標來預測vix,成功率76%。

today_X3 = df[selected_features_vix]
today_y3_predicted = forest.predict(today_X3)
proba = forest.predict_proba(today_X3)
print('預期隔日VIX指數: ' + format(np.where(today_y_predicted==True,'','')[0]))
print( '震盪更大的機率: {}'.format(proba[0][1]))
預期隔日VIX指數: 減
震盪更大的機率: 0.12333333333333334

用上述分類來預測台指期的交易量增加or減少,及預測成功機率。

最後再進行台指期報酬率的預測(賺錢or賠錢),並顯示準確度與AUC值。

linreg = LinearRegression()
linreg_fit = linreg.fit(train_X3, train_y3)
cv = cross_val_score(linreg, train_X3, train_y3, cv=10)
print('Train Accuracy:', cv.mean())

cv = cross_val_score(linreg, test_X3, test_y3, cv=10)
print('Test Accuracy:', cv.mean())

test_y3_predicted = linreg.predict(test_X3)
#print('Predicted Closing Price: %.2f\n' % make_prediction(quotes_df, linreg))
Train Accuracy: 0.13445313024694755
Test Accuracy: 0.12334176365838365
test_y_predicted = linreg.predict(today_X3)
test_y_predicted
array([ 2.89628071e-01,  1.27662419e-01,  5.62733611e-01,  7.77800548e-01,
        1.09024574e+00,  7.22281644e-01,  8.10539896e-01,  1.82151803e-01,
        3.46176832e-01,  2.58240378e-01,  5.79615434e-01,  5.39513161e-01,
        2.10841539e-01,  3.76674350e-01,  1.79240831e-01,  3.70654381e-01,
        6.13448826e-01,  7.26384636e-01,  4.55616717e-01,  5.17373492e-01,
        3.12296301e-01,  5.50974043e-01,  4.88796596e-01,  6.79548999e-01,
        4.48624930e-01,  1.13234169e-01,  1.37994503e-01,  4.46843986e-05,
        3.13314691e-01,  1.44016660e-01,  1.70130033e-01,  3.45267114e-01,
        8.69491776e-01,  6.48574048e-01,  1.02533440e-01,  8.17274069e-01,
        8.03671724e-01,  7.66259351e-01,  1.43589242e-01,  2.20332542e-01,
        1.25290918e-01, -4.12230912e-03,  5.19765653e-02,  3.22743609e-01,
        3.31424929e-01,  4.31809089e-01,  1.86751210e-01,  2.67262133e-01,
        3.45252813e-01,  5.29444650e-01,  7.69654656e-01,  4.35289130e-01,
        5.25124868e-02,  2.80790428e-01,  3.90653259e-01,  3.84272889e-01,
        5.05091882e-01,  4.29292786e-01,  5.16433078e-01,  2.43469363e-01,
        2.51647700e-01,  1.90566424e-01,  4.21416950e-01,  3.98656092e-01,
        4.03439013e-01,  8.79701006e-02,  2.76860178e-01,  5.78688165e-01,
        2.71754458e-01,  4.87991020e-01,  4.21508124e-01,  8.02286306e-01,
        4.53066956e-01,  2.82574973e-02,  5.77362454e-01,  6.92935437e-02,
        3.59099336e-01,  2.95617883e-01,  3.33014557e-01,  8.08639826e-01,
        7.08643241e-01,  5.96381047e-01,  1.10249400e+00,  3.66219164e-01,
        3.47538092e-01,  6.76737066e-01,  1.43346228e+00,  5.00412317e-01,
        9.61458844e-02, -2.42454389e-02,  3.91745589e-01,  3.20455432e-01,
        1.39991273e-01,  5.63417018e-01,  4.80327056e-01,  7.41399606e-01,
        3.97788343e-01,  4.71901912e-01,  3.62760045e-01,  5.52327009e-01,
        1.07260963e+00,  4.35558142e-01,  2.33461196e-01,  2.20633994e-01,
        5.73136469e-01,  4.89026760e-01,  5.16878140e-01,  5.41420065e-01,
        7.74811184e-01,  7.29898259e-01,  1.02056606e+00,  5.94375813e-01,
        2.52279668e-01,  1.82219487e-01,  7.09664052e-01,  7.29835938e-01,
        5.88670141e-01,  5.59275297e-01,  5.27559108e-01,  6.96476355e-01,
        8.63336410e-01,  5.59454679e-01,  5.53371316e-01,  4.35062036e-01,
        1.05896641e+00,  5.72246699e-01,  5.97038710e-01,  6.30545976e-01,
        1.06851469e+00,  1.04362225e+00,  1.62046547e-01,  3.83264115e-01,
        7.85144554e-01,  1.31558678e-01, -5.47054614e-01,  6.18619064e-01,
       -3.32712284e-02,  4.09349308e-01,  7.30439576e-01,  7.63652915e-01,
        6.71329690e-01,  7.52089001e-01,  7.72918404e-03,  8.78739159e-01,
        7.89442895e-02,  2.86170379e-01, -9.46012342e-02,  2.37397060e-01,
        7.71855830e-01,  6.24163998e-01,  5.97966856e-01,  4.75088333e-01,
        6.26521601e-01,  4.39828031e-01,  5.31471329e-01,  1.36376697e+00,
       -1.01300453e-01, -2.25385175e-01,  8.35309353e-01,  2.18019927e-01,
        1.06840989e-01,  3.12738334e-01,  4.90451953e-01,  6.08928364e-01,
       -8.06688299e-02,  1.78346270e-01,  2.23405298e-01,  6.10305801e-01,
        4.90443170e-01,  6.82302848e-01,  9.97674518e-01,  3.56736075e-01,
       -3.31643901e-03,  9.96251189e-01,  7.75280126e-01,  8.26820217e-01,
        6.33863605e-01,  6.36803013e-01, -7.84618040e-03, -1.29846086e-01,
        3.36503251e-01,  6.32399280e-01,  2.88834487e-01,  1.07104238e-01,
        1.55144111e-01,  3.36420725e-01,  2.56309975e-01,  1.56651145e-01,
        1.56033862e-01,  7.20914079e-01,  3.87852687e-01,  5.78441961e-01,
        6.35982090e-01,  1.15283437e-01,  5.41657565e-01,  7.61363903e-01,
        2.27343427e-01,  3.10162333e-01,  1.22313730e-01,  4.88666092e-01,
        1.17436359e+00,  2.82129808e-01,  4.21579120e-01, -9.33773915e-02,
        5.05433833e-01,  1.83102450e-02,  2.65626676e-01,  8.70932182e-01,
        7.43502573e-01,  6.39892941e-01,  5.03298431e-01,  9.10068728e-01,
        1.37067529e-01])
Predict Accuracy: 1.0

PART2: 用CNN預測隔日K線圖之收盤價

說明:

使用 2017/06/20 到 2018/06/18 間 ,2330之 K線圖、一日兩日三日MA、日量。

來預測 Next day 收盤價 漲/跌。

每5個交易日,畫出一張K線圖,用來預測第六天之漲跌。

load in model

from keras.layers import Dense, Dropout, Flatten,Conv2D, MaxPooling2D
from keras.utils import to_categorical
from keras.models import Sequential
from sklearn import model_selection
from keras.utils import np_utils
from keras.datasets import mnist
import matplotlib.pyplot as plt
from scipy import ndimage
from PIL import Image
import pandas as pd
import numpy as np
np.random.seed(10)

plot function

import matplotlib.pyplot as plt
def show_train_history(train_history, train, validation):
    plt.plot(train_history.history[train])
    plt.plot(train_history.history[validation])
    plt.title('train_histroy')
    plt.ylabel(train)
    plt.xlabel('Epoch')
    plt.legend(['train','validation'], loc='upper left')
    plt.show()

load data

import glob
filelist = sorted(glob.glob('/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/*.jpg'))
filelist
['/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20170620-20170710 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20170627-20170717 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20170704-20170724 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20170711-20170731 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20170718-20170807 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20170725-20170814 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20170801-20170821 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20170808-20170828 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20170815-20170904 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20170822-20170911 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20170829-20170918 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20170905-20170925 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20170912-20171002 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20170919-20171009 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20170926-20171016 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20171003-20171023 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20171010-20171030 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20171017-20171106 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20171024-20171113 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20171031-20171120 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20171107-20171127 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20171114-20171204 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20171121-20171211 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20171128-20171218 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20171205-20171225 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20171212-20180101 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20171219-20180108 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20171226-20180115 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20180102-20180122 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20180109-20180129 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20180116-20180205 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20180123-20180212 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20180130-20180219 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20180206-20180226 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20180213-20180305 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20180220-20180312 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20180227-20180319 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20180306-20180326 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20180313-20180402 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20180320-20180409 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20180327-20180416 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20180403-20180423 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20180410-20180430 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20180417-20180507 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20180424-20180514 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20180501-20180521 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20180508-20180528 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20180515-20180604 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20180522-20180611 2330.jpg',
 '/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20180529-20180618 2330.jpg']
x = np.array([np.array(Image.open(fname)) for fname in filelist])
x.shape
(50, 615, 1432, 3)
plt.imshow(x[20], cmap='gray')
<matplotlib.image.AxesImage at 0x103bbd320>

png

Y = pd.read_csv("/Users/tyyeh/Documents/Documents - TengYuanYEH MacBook Air/bigdata/final project/20170620-201806180-2330-chinese-v7.csv")
Y.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 50 entries, 0 to 49
Data columns (total 1 columns):
close    50 non-null int64
dtypes: int64(1)
memory usage: 480.0 bytes
train_X, test_X, train_y, test_y = model_selection.train_test_split(x, Y, test_size = 0.3)
train_X.shape
(35, 615, 1432, 3)

x_train normalize & 4D matrix

x_train4D = train_X.reshape(train_X.shape[0], 615, 1432, 3).astype('float64')
x_test4D = test_X.reshape(test_X.shape[0], 615, 1432, 3).astype('float64')
x_train4D
array([[[[  0.,   0.,  12.],
         [  9.,  12.,  27.],
         [  0.,   0.,  14.],
         ...,
         [  0.,   0.,  23.],
         [  6.,  13.,  29.],
         [  0.,   0.,   9.]],

        [[ 17.,  20.,  35.],
         [165., 171., 185.],
         [189., 195., 211.],
         ...,
         [187., 195., 218.],
         [164., 171., 189.],
         [ 17.,  21.,  30.]],

        [[  0.,   0.,  14.],
         [192., 198., 214.],
         [203., 210., 226.],
         ...,
         [202., 210., 233.],
         [191., 198., 216.],
         [  0.,   0.,   9.]],

        ...,

        [[  0.,   2.,  19.],
         [180., 193., 210.],
         [193., 206., 223.],
         ...,
         [194., 206., 218.],
         [183., 193., 203.],
         [  0.,   1.,   9.]],

        [[  4.,  14.,  26.],
         [160., 170., 182.],
         [197., 207., 219.],
         ...,
         [199., 206., 214.],
         [162., 169., 175.],
         [  8.,  13.,  19.]],

        [[  0.,   2.,   9.],
         [ 12.,  21.,  28.],
         [  0.,   2.,   9.],
         ...,
         [  0.,   0.,   5.],
         [ 16.,  20.,  23.],
         [  0.,   1.,   4.]]],


       [[[  0.,   0.,  12.],
         [  9.,  12.,  27.],
         [  0.,   0.,  14.],
         ...,
         [  0.,   0.,  23.],
         [  6.,  13.,  29.],
         [  0.,   0.,   9.]],

        [[ 17.,  20.,  35.],
         [165., 171., 185.],
         [189., 195., 211.],
         ...,
         [187., 195., 218.],
         [164., 171., 189.],
         [ 17.,  21.,  30.]],

        [[  0.,   0.,  14.],
         [192., 198., 214.],
         [203., 210., 226.],
         ...,
         [202., 210., 233.],
         [191., 198., 216.],
         [  0.,   0.,   9.]],

        ...,

        [[  0.,   2.,  19.],
         [180., 193., 210.],
         [193., 206., 223.],
         ...,
         [194., 206., 218.],
         [183., 193., 203.],
         [  0.,   1.,   9.]],

        [[  4.,  14.,  26.],
         [160., 170., 182.],
         [197., 207., 219.],
         ...,
         [199., 206., 214.],
         [162., 169., 175.],
         [  8.,  13.,  19.]],

        [[  0.,   2.,   9.],
         [ 12.,  21.,  28.],
         [  0.,   2.,   9.],
         ...,
         [  0.,   0.,   5.],
         [ 16.,  20.,  23.],
         [  0.,   1.,   4.]]],


       [[[  0.,   0.,  12.],
         [  9.,  12.,  27.],
         [  0.,   0.,  14.],
         ...,
         [  0.,   0.,  23.],
         [  6.,  13.,  29.],
         [  0.,   0.,   9.]],

        [[ 17.,  20.,  35.],
         [165., 171., 185.],
         [189., 195., 211.],
         ...,
         [187., 195., 218.],
         [164., 171., 189.],
         [ 17.,  21.,  30.]],

        [[  0.,   0.,  14.],
         [192., 198., 214.],
         [203., 210., 226.],
         ...,
         [202., 210., 233.],
         [191., 198., 216.],
         [  0.,   0.,   9.]],

        ...,

        [[  0.,   2.,  19.],
         [180., 193., 210.],
         [193., 206., 223.],
         ...,
         [194., 206., 218.],
         [183., 193., 203.],
         [  0.,   1.,   9.]],

        [[  4.,  14.,  26.],
         [160., 170., 182.],
         [197., 207., 219.],
         ...,
         [199., 206., 214.],
         [162., 169., 175.],
         [  8.,  13.,  19.]],

        [[  0.,   2.,   9.],
         [ 12.,  21.,  28.],
         [  0.,   2.,   9.],
         ...,
         [  0.,   0.,   5.],
         [ 16.,  20.,  23.],
         [  0.,   1.,   4.]]],


       ...,


       [[[  0.,   0.,  12.],
         [  9.,  12.,  27.],
         [  0.,   0.,  14.],
         ...,
         [  0.,   0.,  23.],
         [  6.,  13.,  29.],
         [  0.,   0.,   9.]],

        [[ 17.,  20.,  35.],
         [165., 171., 185.],
         [189., 195., 211.],
         ...,
         [187., 195., 218.],
         [164., 171., 189.],
         [ 17.,  21.,  30.]],

        [[  0.,   0.,  14.],
         [192., 198., 214.],
         [203., 210., 226.],
         ...,
         [202., 210., 233.],
         [191., 198., 216.],
         [  0.,   0.,   9.]],

        ...,

        [[  0.,   2.,  19.],
         [180., 193., 210.],
         [193., 206., 223.],
         ...,
         [194., 206., 218.],
         [183., 193., 203.],
         [  0.,   1.,   9.]],

        [[  4.,  14.,  26.],
         [160., 170., 182.],
         [197., 207., 219.],
         ...,
         [199., 206., 214.],
         [162., 169., 175.],
         [  8.,  13.,  19.]],

        [[  0.,   2.,   9.],
         [ 12.,  21.,  28.],
         [  0.,   2.,   9.],
         ...,
         [  0.,   0.,   5.],
         [ 16.,  20.,  23.],
         [  0.,   1.,   4.]]],


       [[[  0.,   0.,  12.],
         [  9.,  12.,  27.],
         [  0.,   0.,  14.],
         ...,
         [  0.,   0.,  23.],
         [  6.,  13.,  29.],
         [  0.,   0.,   9.]],

        [[ 17.,  20.,  35.],
         [165., 171., 185.],
         [189., 195., 211.],
         ...,
         [187., 195., 218.],
         [164., 171., 189.],
         [ 17.,  21.,  30.]],

        [[  0.,   0.,  14.],
         [192., 198., 214.],
         [203., 210., 226.],
         ...,
         [202., 210., 233.],
         [191., 198., 216.],
         [  0.,   0.,   9.]],

        ...,

        [[  0.,   2.,  19.],
         [180., 193., 210.],
         [193., 206., 223.],
         ...,
         [194., 206., 218.],
         [183., 193., 203.],
         [  0.,   1.,   9.]],

        [[  4.,  14.,  26.],
         [160., 170., 182.],
         [197., 207., 219.],
         ...,
         [199., 206., 214.],
         [162., 169., 175.],
         [  8.,  13.,  19.]],

        [[  0.,   2.,   9.],
         [ 12.,  21.,  28.],
         [  0.,   2.,   9.],
         ...,
         [  0.,   0.,   5.],
         [ 16.,  20.,  23.],
         [  0.,   1.,   4.]]],


       [[[  0.,   0.,  12.],
         [  9.,  12.,  27.],
         [  0.,   0.,  14.],
         ...,
         [  0.,   0.,  23.],
         [  6.,  13.,  29.],
         [  0.,   0.,   9.]],

        [[ 17.,  20.,  35.],
         [165., 171., 185.],
         [189., 195., 211.],
         ...,
         [187., 195., 218.],
         [164., 171., 189.],
         [ 17.,  21.,  30.]],

        [[  0.,   0.,  14.],
         [192., 198., 214.],
         [203., 210., 226.],
         ...,
         [202., 210., 233.],
         [191., 198., 216.],
         [  0.,   0.,   9.]],

        ...,

        [[  0.,   2.,  19.],
         [180., 193., 210.],
         [193., 206., 223.],
         ...,
         [194., 206., 218.],
         [183., 193., 203.],
         [  0.,   1.,   9.]],

        [[  4.,  14.,  26.],
         [160., 170., 182.],
         [197., 207., 219.],
         ...,
         [199., 206., 214.],
         [162., 169., 175.],
         [  8.,  13.,  19.]],

        [[  0.,   2.,   9.],
         [ 12.,  21.,  28.],
         [  0.,   2.,   9.],
         ...,
         [  0.,   0.,   5.],
         [ 16.,  20.,  23.],
         [  0.,   1.,   4.]]]])
x_train4D_normalize = x_train4D / 255 #normailize
x_test4D_normalize = x_test4D/ 255
x_train4D_normalize
array([[[[0.        , 0.        , 0.04705882],
         [0.03529412, 0.04705882, 0.10588235],
         [0.        , 0.        , 0.05490196],
         ...,
         [0.        , 0.        , 0.09019608],
         [0.02352941, 0.05098039, 0.11372549],
         [0.        , 0.        , 0.03529412]],

        [[0.06666667, 0.07843137, 0.1372549 ],
         [0.64705882, 0.67058824, 0.7254902 ],
         [0.74117647, 0.76470588, 0.82745098],
         ...,
         [0.73333333, 0.76470588, 0.85490196],
         [0.64313725, 0.67058824, 0.74117647],
         [0.06666667, 0.08235294, 0.11764706]],

        [[0.        , 0.        , 0.05490196],
         [0.75294118, 0.77647059, 0.83921569],
         [0.79607843, 0.82352941, 0.88627451],
         ...,
         [0.79215686, 0.82352941, 0.91372549],
         [0.74901961, 0.77647059, 0.84705882],
         [0.        , 0.        , 0.03529412]],

        ...,

        [[0.        , 0.00784314, 0.0745098 ],
         [0.70588235, 0.75686275, 0.82352941],
         [0.75686275, 0.80784314, 0.8745098 ],
         ...,
         [0.76078431, 0.80784314, 0.85490196],
         [0.71764706, 0.75686275, 0.79607843],
         [0.        , 0.00392157, 0.03529412]],

        [[0.01568627, 0.05490196, 0.10196078],
         [0.62745098, 0.66666667, 0.71372549],
         [0.77254902, 0.81176471, 0.85882353],
         ...,
         [0.78039216, 0.80784314, 0.83921569],
         [0.63529412, 0.6627451 , 0.68627451],
         [0.03137255, 0.05098039, 0.0745098 ]],

        [[0.        , 0.00784314, 0.03529412],
         [0.04705882, 0.08235294, 0.10980392],
         [0.        , 0.00784314, 0.03529412],
         ...,
         [0.        , 0.        , 0.01960784],
         [0.0627451 , 0.07843137, 0.09019608],
         [0.        , 0.00392157, 0.01568627]]],


       [[[0.        , 0.        , 0.04705882],
         [0.03529412, 0.04705882, 0.10588235],
         [0.        , 0.        , 0.05490196],
         ...,
         [0.        , 0.        , 0.09019608],
         [0.02352941, 0.05098039, 0.11372549],
         [0.        , 0.        , 0.03529412]],

        [[0.06666667, 0.07843137, 0.1372549 ],
         [0.64705882, 0.67058824, 0.7254902 ],
         [0.74117647, 0.76470588, 0.82745098],
         ...,
         [0.73333333, 0.76470588, 0.85490196],
         [0.64313725, 0.67058824, 0.74117647],
         [0.06666667, 0.08235294, 0.11764706]],

        [[0.        , 0.        , 0.05490196],
         [0.75294118, 0.77647059, 0.83921569],
         [0.79607843, 0.82352941, 0.88627451],
         ...,
         [0.79215686, 0.82352941, 0.91372549],
         [0.74901961, 0.77647059, 0.84705882],
         [0.        , 0.        , 0.03529412]],

        ...,

        [[0.        , 0.00784314, 0.0745098 ],
         [0.70588235, 0.75686275, 0.82352941],
         [0.75686275, 0.80784314, 0.8745098 ],
         ...,
         [0.76078431, 0.80784314, 0.85490196],
         [0.71764706, 0.75686275, 0.79607843],
         [0.        , 0.00392157, 0.03529412]],

        [[0.01568627, 0.05490196, 0.10196078],
         [0.62745098, 0.66666667, 0.71372549],
         [0.77254902, 0.81176471, 0.85882353],
         ...,
         [0.78039216, 0.80784314, 0.83921569],
         [0.63529412, 0.6627451 , 0.68627451],
         [0.03137255, 0.05098039, 0.0745098 ]],

        [[0.        , 0.00784314, 0.03529412],
         [0.04705882, 0.08235294, 0.10980392],
         [0.        , 0.00784314, 0.03529412],
         ...,
         [0.        , 0.        , 0.01960784],
         [0.0627451 , 0.07843137, 0.09019608],
         [0.        , 0.00392157, 0.01568627]]],


       [[[0.        , 0.        , 0.04705882],
         [0.03529412, 0.04705882, 0.10588235],
         [0.        , 0.        , 0.05490196],
         ...,
         [0.        , 0.        , 0.09019608],
         [0.02352941, 0.05098039, 0.11372549],
         [0.        , 0.        , 0.03529412]],

        [[0.06666667, 0.07843137, 0.1372549 ],
         [0.64705882, 0.67058824, 0.7254902 ],
         [0.74117647, 0.76470588, 0.82745098],
         ...,
         [0.73333333, 0.76470588, 0.85490196],
         [0.64313725, 0.67058824, 0.74117647],
         [0.06666667, 0.08235294, 0.11764706]],

        [[0.        , 0.        , 0.05490196],
         [0.75294118, 0.77647059, 0.83921569],
         [0.79607843, 0.82352941, 0.88627451],
         ...,
         [0.79215686, 0.82352941, 0.91372549],
         [0.74901961, 0.77647059, 0.84705882],
         [0.        , 0.        , 0.03529412]],

        ...,

        [[0.        , 0.00784314, 0.0745098 ],
         [0.70588235, 0.75686275, 0.82352941],
         [0.75686275, 0.80784314, 0.8745098 ],
         ...,
         [0.76078431, 0.80784314, 0.85490196],
         [0.71764706, 0.75686275, 0.79607843],
         [0.        , 0.00392157, 0.03529412]],

        [[0.01568627, 0.05490196, 0.10196078],
         [0.62745098, 0.66666667, 0.71372549],
         [0.77254902, 0.81176471, 0.85882353],
         ...,
         [0.78039216, 0.80784314, 0.83921569],
         [0.63529412, 0.6627451 , 0.68627451],
         [0.03137255, 0.05098039, 0.0745098 ]],

        [[0.        , 0.00784314, 0.03529412],
         [0.04705882, 0.08235294, 0.10980392],
         [0.        , 0.00784314, 0.03529412],
         ...,
         [0.        , 0.        , 0.01960784],
         [0.0627451 , 0.07843137, 0.09019608],
         [0.        , 0.00392157, 0.01568627]]],


       ...,


       [[[0.        , 0.        , 0.04705882],
         [0.03529412, 0.04705882, 0.10588235],
         [0.        , 0.        , 0.05490196],
         ...,
         [0.        , 0.        , 0.09019608],
         [0.02352941, 0.05098039, 0.11372549],
         [0.        , 0.        , 0.03529412]],

        [[0.06666667, 0.07843137, 0.1372549 ],
         [0.64705882, 0.67058824, 0.7254902 ],
         [0.74117647, 0.76470588, 0.82745098],
         ...,
         [0.73333333, 0.76470588, 0.85490196],
         [0.64313725, 0.67058824, 0.74117647],
         [0.06666667, 0.08235294, 0.11764706]],

        [[0.        , 0.        , 0.05490196],
         [0.75294118, 0.77647059, 0.83921569],
         [0.79607843, 0.82352941, 0.88627451],
         ...,
         [0.79215686, 0.82352941, 0.91372549],
         [0.74901961, 0.77647059, 0.84705882],
         [0.        , 0.        , 0.03529412]],

        ...,

        [[0.        , 0.00784314, 0.0745098 ],
         [0.70588235, 0.75686275, 0.82352941],
         [0.75686275, 0.80784314, 0.8745098 ],
         ...,
         [0.76078431, 0.80784314, 0.85490196],
         [0.71764706, 0.75686275, 0.79607843],
         [0.        , 0.00392157, 0.03529412]],

        [[0.01568627, 0.05490196, 0.10196078],
         [0.62745098, 0.66666667, 0.71372549],
         [0.77254902, 0.81176471, 0.85882353],
         ...,
         [0.78039216, 0.80784314, 0.83921569],
         [0.63529412, 0.6627451 , 0.68627451],
         [0.03137255, 0.05098039, 0.0745098 ]],

        [[0.        , 0.00784314, 0.03529412],
         [0.04705882, 0.08235294, 0.10980392],
         [0.        , 0.00784314, 0.03529412],
         ...,
         [0.        , 0.        , 0.01960784],
         [0.0627451 , 0.07843137, 0.09019608],
         [0.        , 0.00392157, 0.01568627]]],


       [[[0.        , 0.        , 0.04705882],
         [0.03529412, 0.04705882, 0.10588235],
         [0.        , 0.        , 0.05490196],
         ...,
         [0.        , 0.        , 0.09019608],
         [0.02352941, 0.05098039, 0.11372549],
         [0.        , 0.        , 0.03529412]],

        [[0.06666667, 0.07843137, 0.1372549 ],
         [0.64705882, 0.67058824, 0.7254902 ],
         [0.74117647, 0.76470588, 0.82745098],
         ...,
         [0.73333333, 0.76470588, 0.85490196],
         [0.64313725, 0.67058824, 0.74117647],
         [0.06666667, 0.08235294, 0.11764706]],

        [[0.        , 0.        , 0.05490196],
         [0.75294118, 0.77647059, 0.83921569],
         [0.79607843, 0.82352941, 0.88627451],
         ...,
         [0.79215686, 0.82352941, 0.91372549],
         [0.74901961, 0.77647059, 0.84705882],
         [0.        , 0.        , 0.03529412]],

        ...,

        [[0.        , 0.00784314, 0.0745098 ],
         [0.70588235, 0.75686275, 0.82352941],
         [0.75686275, 0.80784314, 0.8745098 ],
         ...,
         [0.76078431, 0.80784314, 0.85490196],
         [0.71764706, 0.75686275, 0.79607843],
         [0.        , 0.00392157, 0.03529412]],

        [[0.01568627, 0.05490196, 0.10196078],
         [0.62745098, 0.66666667, 0.71372549],
         [0.77254902, 0.81176471, 0.85882353],
         ...,
         [0.78039216, 0.80784314, 0.83921569],
         [0.63529412, 0.6627451 , 0.68627451],
         [0.03137255, 0.05098039, 0.0745098 ]],

        [[0.        , 0.00784314, 0.03529412],
         [0.04705882, 0.08235294, 0.10980392],
         [0.        , 0.00784314, 0.03529412],
         ...,
         [0.        , 0.        , 0.01960784],
         [0.0627451 , 0.07843137, 0.09019608],
         [0.        , 0.00392157, 0.01568627]]],


       [[[0.        , 0.        , 0.04705882],
         [0.03529412, 0.04705882, 0.10588235],
         [0.        , 0.        , 0.05490196],
         ...,
         [0.        , 0.        , 0.09019608],
         [0.02352941, 0.05098039, 0.11372549],
         [0.        , 0.        , 0.03529412]],

        [[0.06666667, 0.07843137, 0.1372549 ],
         [0.64705882, 0.67058824, 0.7254902 ],
         [0.74117647, 0.76470588, 0.82745098],
         ...,
         [0.73333333, 0.76470588, 0.85490196],
         [0.64313725, 0.67058824, 0.74117647],
         [0.06666667, 0.08235294, 0.11764706]],

        [[0.        , 0.        , 0.05490196],
         [0.75294118, 0.77647059, 0.83921569],
         [0.79607843, 0.82352941, 0.88627451],
         ...,
         [0.79215686, 0.82352941, 0.91372549],
         [0.74901961, 0.77647059, 0.84705882],
         [0.        , 0.        , 0.03529412]],

        ...,

        [[0.        , 0.00784314, 0.0745098 ],
         [0.70588235, 0.75686275, 0.82352941],
         [0.75686275, 0.80784314, 0.8745098 ],
         ...,
         [0.76078431, 0.80784314, 0.85490196],
         [0.71764706, 0.75686275, 0.79607843],
         [0.        , 0.00392157, 0.03529412]],

        [[0.01568627, 0.05490196, 0.10196078],
         [0.62745098, 0.66666667, 0.71372549],
         [0.77254902, 0.81176471, 0.85882353],
         ...,
         [0.78039216, 0.80784314, 0.83921569],
         [0.63529412, 0.6627451 , 0.68627451],
         [0.03137255, 0.05098039, 0.0745098 ]],

        [[0.        , 0.00784314, 0.03529412],
         [0.04705882, 0.08235294, 0.10980392],
         [0.        , 0.00784314, 0.03529412],
         ...,
         [0.        , 0.        , 0.01960784],
         [0.0627451 , 0.07843137, 0.09019608],
         [0.        , 0.00392157, 0.01568627]]]])
y_trainOneHot = np_utils.to_categorical(train_y)
y_testOneHot = np_utils.to_categorical(test_y)

establish CNN model

model = Sequential()
model.add(Conv2D(filters=36,
                kernel_size = (5,5),
                padding = 'same',
                input_shape=(615, 1432, 3),
                activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(filters=36,
                kernel_size=(5,5),
                padding= 'same',
                activation='relu'))
model.add(MaxPooling2D(pool_size =(2,2)))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(128,activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(,activation='softmax'))
model.compile(loss='categorical_crossentropy',
             optimizer = 'adam', metrics=['accuracy'])

input train data in CNN

train_history = model.fit(x= x_train4D,y= y_trainOneHot,
                          validation_split=0.3,epochs=5,
                          batch_size=300, verbose=2)
Train on 24 samples, validate on 11 samples
Epoch 1/5
 - 212s - loss: 7.0771 - acc: 0.5000 - val_loss: 1.1921e-07 - val_acc: 1.0000
Epoch 2/5
 - 206s - loss: 2.6863 - acc: 0.8333 - val_loss: 1.1921e-07 - val_acc: 1.0000
Epoch 3/5
 - 183s - loss: 2.6863 - acc: 0.8333 - val_loss: 1.1921e-07 - val_acc: 1.0000
Epoch 4/5
 - 180s - loss: 2.6863 - acc: 0.8333 - val_loss: 1.1921e-07 - val_acc: 1.0000
Epoch 5/5
 - 208s - loss: 2.6863 - acc: 0.8333 - val_loss: 1.1921e-07 - val_acc: 1.0000

show train history

show_train_history(train_history,'acc','val_acc')

png

show_train_history(train_history,'loss','val_loss')

png

predict score

scores = model.evaluate(x_test4D_normalize, y_testOneHot)
15/15 [==============================] - 37s 2s/step
scores[1]
0.8666666746139526
You can’t perform that action at this time.