def tune_para(dataframe, i):
# To apply an classifier on this data, we need to flatten the image, to
# turn the data in a (samples, feature) matrix:
columns = ['SMA_10','Momentum','stoch_K','WMA_10','MACD','A/D','Volume']
X = dataframe[columns].as_matrix()
y = dataframe['Adj Close'].as_matrix()
X_train = X[i-200:i]
y_train = y[i-200:i]
X_test = X[i:i+1]
y_test = y[i:i+1]
### Train four kinds of SVM model
C = 1 # SVM regularization parameter
svc = svm.SVC(cache_size = 1000, kernel='linear', C=C).fit(X_train, y_train)
rbf_svc = svm.SVC(cache_size = 1000, kernel='rbf', gamma=0.7, C=C).fit(X_train, y_train)
poly_svc = svm.SVC(cache_size = 1000, kernel='poly', degree=3, C=C).fit(X_train, y_train)
lin_svc = svm.LinearSVC(loss='squared_hinge', penalty='l1', dual=False, C=C).fit(X_train, y_train)
Y_result = y_test
### Make the prediction
for i, clf in enumerate((svc, lin_svc, rbf_svc, poly_svc)):
pred = clf.predict(X_test)
Y_result = np.vstack((Y_result, np.array(pred))) # append prediction on Y_result
return Y_result.T
评论列表
文章目录