scikit-learn示例
from joblib import parallel_backend, dump, load
import data.selectData as selectData
import time
import util_job.util as util
# from sklearn import svm
from sklearn.neural_network import MLPClassifier
# from sklearn.tree import DecisionTreeClassifier
# from sklearn.linear_model import LogisticRegression
from sklearn.svm import LinearSVC
import numpy as np
import data.insertData as insertData
from sklearn.metrics import accuracy_score
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import cross_val_predict
from sklearn.preprocessing import StandardScaler
# 预测
def run_sklearn(start_time, end_time, x, y):
开始时间 = time.time()
barData = selectData.show_10s_between_time_new(start_time, end_time)
barData = util.sort_dataFrame(barData)
barData.replace([np.inf, -np.inf, "", np.nan], 0, inplace=True)
# x_train = np.array(barData[[x]])
x_train = np.array(barData[x])
# scaler = StandardScaler() 标准化数据,不准
# scaler.fit(x_train)
# x_train = scaler.transform(x_train)
y_train = np.array(barData[y])
print('开始')
加载数据时间 = time.time()
print('加载数据时间:', round((加载数据时间 - 开始时间) / 60, 2), '分钟')
# 实例化 模型
# clf = svm.SVC(C=0.6, kernel='rbf', gamma=0.001)
# clf = svm.SVC() # 相似度:71.75
# clf = DecisionTreeClassifier() # 相似度:61.48
# clf = LogisticRegression() # 相似度: 71.91
clf = LinearSVC(max_iter=100000) # 相似度: max_iter=1000000
# clf = MLPClassifier() # 相似度:正 71.73 71.83 负 72.0129 max_iter=10000
with parallel_backend('threading', n_jobs=-1):
# 放入 数据学习
clf.fit(x_train, y_train)
print('训练完成!====', clf)
print("LinearSVC 。 决策函数中的常数 intercept : ", clf.intercept_, " 。 唯一的类标签 classes_ : ", clf.classes_,
" 。 所有类的最大迭代次数:n_iter_ : ", clf.n_iter_, " 。 系数:", clf.coef_)
训练完成时间 = time.time()
print('训练时间:', round((训练完成时间 - 加载数据时间) / 60, 2), '分钟')
# 下载模型,持久化模型
dump(clf, 'scikit_model.joblib')
print('训练总时间:', round((训练完成时间 - 开始时间) / 60, 2), '分钟')
return clf, x_train, y_train
# 交叉准确率
def scores(clf, x_train, y_train):
开始时间 = time.time()
with parallel_backend(backend='threading', n_jobs=-1):
scores = cross_val_score(clf, x_train, y_train, cv=10) # .astype('int')
print('scores准确率:', scores)
print("Accuracy: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() * 2))
y_pred = cross_val_predict(clf, x_train, y_train, cv=10)
print('y_pred准确率:', y_pred)
结束时间 = time.time()
print('交叉准确率时间:', round((开始时间 - 结束时间) / 60, 2), '分钟')
# 测试
def run_test(features_start_time, features_end_time, x, y):
开始时间 = time.time()
# 加载模型
clf = load('scikit_model.joblib')
# 加载测试数据
features_barData = selectData.show_10s_between_time_new(features_start_time, features_end_time)
features_barData = util.sort_dataFrame(features_barData)
features = np.array(features_barData[x])
加载数据时间 = time.time()
print('加载测试数据时间:', round((加载数据时间 - 开始时间) / 60, 2), '分钟')
# features = scaler.transform(features) 标准化数据
with parallel_backend('threading', n_jobs=-1):
# 预测 数据
features_data = clf.predict(features)
features_barData['未来价格走势'] = features_data
insertData.run_features(features_barData)
预测时间 = time.time()
print('预测时间:', round((预测时间 - 加载数据时间) / 60, 2), '分钟')
# 准确率
acc = accuracy_score(np.array(features_barData[y]), features_data)
time_end = time.time()
print('计算准确率时间:', round((time_end - 预测时间) / 60, 2), '分钟')
print('准确率:', acc)
def run_corr():
barData = selectData.show_features_bar()
corrr = barData['未来价格走势'].corr(barData['正价格走势'])
print('相关性:', corrr)
def run():
sk_start_time = "2018-01-01 09:00"
sk_end_time = "2022-03-01 24:00"
features_start_time = "2022-03-03 09:00"
features_end_time = "2029-12-01 24:00"
# todo 加上 价格趋势
# x="买多_1r", "卖空_1r", "平多_1r", "平空_1r", "买多_5r", "卖空_5r", "平多_5r", "平空_5r", "买多_10r", "卖空_10r", "平多_10r", "平空_10r", "买多_30r", "卖空_30r", "平多_30r", "平空_30r", "ask_1min", "bid_1min", "vr_1", "kdj_k_list", "kdj_d_list", "kdj_k-d", "diff", "dea","macd"
x = ["买多_1r", "卖空_1r", "平多_1r", "平空_1r", "ask_1min", "bid_1min", "vr_1", "kdj_k_list", "kdj_d_list", "kdj_k-d",
"diff",
"dea", "macd"]
# x = ["买多成交量", "卖空成交量", "平多成交量", "平空成交量", "ask_v_sum", "bid_v_sum", "volume_sum", "kdj_k_list", "kdj_d_list", "kdj_k-d", "diff", "dea", "macd"] ,"hours","分钟"
# x = ["ask_v_sum", "bid_v_sum", "volume_sum", "kdj_k_list", "kdj_d_list", "kdj_k-d", "diff", "dea", "macd"]
y = "正价格走势"
# 预测
clf, x_train, y_train = run_sklearn(sk_start_time, sk_end_time, x, y)
# 测试
run_test(features_start_time, features_end_time, x, y)
# 准确率
scores(clf, x_train, y_train)
# 相关性
run_corr()
网友评论