同花顺Supermind量化交易 机器学习算法对比-九大-机器学习算法运用 附源代码

76 阅读2分钟

这一节学习机器学习算法的对比,第二章学习如何将这些九大机器学习算法运用在预测数据上。

label = ['money rate %','net up rate % ','mean of updown %']
label1 = 'money rate %'
label2 = 'net up rate % '
label3 = 'mean of updown %'

#保留近一年的数据,用于测试,之前数据用于训练
train = dt[:-250]
test = dt[-250:]
X=train[label]
Y=train['label']
X_test=test[label]
Y_test=test['label']

from sklearn.neighbors import KNeighborsClassifier
model=KNeighborsClassifier(n_neighbors=30)

model.fit(X, Y)
print('训练时,预测成功率 {}'.format(round(np.mean(model.predict(X)==Y),2)))
print('测试时,预测成功率 {}'.format(round(np.mean(model.predict(X_test)==Y_test),2)))
训练时,预测成功率 0.56
测试时,预测成功率 0.55

In [207]:

#净值
test['Forecast'] = list(model.predict(X_test))
test['ref'] = test['next up'].loc[test['Forecast']==1]
test = test.fillna(0)
test['ref'] = test['ref'].apply(lambda x:1+x/100)
from operator import mul
from functools import reduce
test['date'] = test.index
test['net value'] = test['date'].apply(lambda x:reduce(mul,list(test['ref'])[:list(test['date']).index(x)+1]))

#基准净值
test['benchmark'] = test['now up'].apply(lambda x:1+x/100)
test['benchmark value'] = test['date'].apply(lambda x:reduce(mul,list(test['benchmark'])[:list(test['date']).index(x)+1]))

#风控净值
# model.predict_proba(X_test)[3][1]
test['risk ref'] = test['next up'].loc[test['Forecast']==1]
test = test.fillna(0)
test['rate'] = [model.predict_proba(X_test)[s][1] for s in range(0,len(model.predict_proba(X_test)))]
test['risk ref'] = (test['risk ref']/100)*test['rate']+1
test['net value (risk)'] = test['date'].apply(lambda x:reduce(mul,list(test['risk ref'])[:list(test['date']).index(x)+1]))

fig = plt.figure()
axes = fig.add_axes([0.1, 0.1, 1, 0.618]) #插入面板
color = ['tomato','green','darkorchid','b','y']
x1_list=list(test['net value'])
y=np.array(x1_list)
x=np.array(range(0,len(x1_list)))
axes.plot(x, y, 'tomato')

x1_list=list(test['benchmark value'])
y1=np.array(x1_list)
x1=np.array(range(0,len(x1_list)))
axes.plot(x1, y1, 'darkorchid')

axes.set_xlabel('Time',fontsize=15)
axes.set_ylabel('net value',fontsize=15)
axes.set_title('KNN return',fontsize=20)
axes.legend(['net value','benchmark'])
#设置X轴
mtradelist = list(test['date'])
numlist=[]
for s in list(range(0,len(mtradelist),60)):
    numlist.append(mtradelist[s])
axes.set_xticks(list(range(0,len(mtradelist),60)))
axes.set_xticklabels(numlist, fontsize=10)




#风控
fig = plt.figure()
axes = fig.add_axes([0.1, 0.1, 1, 0.618]) #插入面板
color = ['tomato','green','darkorchid','b','y']
x1_list=list(test['net value'])
y=np.array(x1_list)
x=np.array(range(0,len(x1_list)))
axes.plot(x, y, 'tomato')

x1_list=list(test['benchmark value'])
y1=np.array(x1_list)
x1=np.array(range(0,len(x1_list)))
axes.plot(x1, y1, 'darkorchid')

x1_list=list(test['net value (risk)'])
y2=np.array(x1_list)
x2=np.array(range(0,len(x1_list)))
axes.plot(x2, y2, 'b')

axes.set_xlabel('Time',fontsize=15)
axes.set_ylabel('net value',fontsize=15)
axes.set_title('KNN return (risk)',fontsize=20)
axes.legend(['net value','benchmark','net value (risk)'])
#设置X轴
mtradelist = list(test['date'])
numlist=[]
for s in list(range(0,len(mtradelist),60)):
    numlist.append(mtradelist[s])
axes.set_xticks(list(range(0,len(mtradelist),60)))
axes.set_xticklabels(numlist, fontsize=10)

Out[207]:

[<matplotlib.text.Text at 0x7f52b225dd30>,
 <matplotlib.text.Text at 0x7f52b225dda0>,
 <matplotlib.text.Text at 0x7f52b221cac8>,
 <matplotlib.text.Text at 0x7f52b2220048>,
 <matplotlib.text.Text at 0x7f52b2220b00>]

查看以上策略详情请到supermind量化交易官网查看:同花顺Supermind量化交易 机器学习算法对比-九大-机器学习算法运用 附源代码