DAY 27
0
AI & Data

## 27.稍微認真做一個股市預測模型

'成交股數', '成交金額', '開盤價', '最高價', '最低價', '收盤價', '漲跌價差', '成交筆數', '三大法人買賣超','外資自營商買賣超', '外資自營商買進', '外資自營商賣出', '外陸資買賣超', '外陸資買進', '外陸資賣出', '投信買賣超','投信買進', '投信賣出', '自營商買賣超', '自營商買賣超避險', '自營商買進', '自營商買進避險', '自營商賣出','自營商賣出避險', '融券今日餘額', '融券前日餘額', '融券現金償還', '融券買進', '融券賣出', '融券限額','融資今日餘額', '融資前日餘額', '融資現金償還', '融資買進', '融資賣出', '融資限額', '資券互抵', '借券今日可限額','借券前日餘額', '借券當日調整', '借券當日賣出', '借券當日還券', '借券當日餘額'

``````data['三大法人買賣超'].rolling(window=4).agg(['sum','std','mean','max','min','median','kurt','skew'])
``````

``````for col in data.columns:
data=pd.merge(data, data[col].rolling(window=4).agg(['sum','std','mean','max','min','median','kurt','skew']),on='date',suffixes=('','_'+col) )
``````

``````from sklearn import ensemble
params = {'n_estimators': [100,200,300], 'max_depth': [5,6,7], 'min_samples_split': [2],
'learning_rate': [0.01], 'loss': ['ls']}

from sklearn.model_selection import TimeSeriesSplit, GridSearchCV
my_cv = TimeSeriesSplit(n_splits=3).split(X)
clf = GridSearchCV(ensemble.GradientBoostingRegressor(), params, cv=my_cv, n_jobs=1)
clf.fit(X_train, y_train)

clf.best_estimator_.get_params()
``````

{'alpha': 0.9,
'criterion': 'friedman_mse',
'init': None,
'learning_rate': 0.01,
'loss': 'ls',
'max_depth': 7,
'max_features': None,
'max_leaf_nodes': None,
'min_impurity_decrease': 0.0,
'min_impurity_split': None,
'min_samples_leaf': 1,
'min_samples_split': 2,
'min_weight_fraction_leaf': 0.0,
'n_estimators': 200,
'presort': 'auto',
'random_state': None,
'subsample': 1.0,
'verbose': 0,
'warm_start': False}

Mean squared error: 343.01
Mean absolute error: 15.65