python支持向量机分类器怎么用_用Python 实现一个 SVM 分类器 策略

本文介绍了支持向量机(SVM)的概念,并通过一个情人节故事生动解释其工作原理。接着,展示了如何利用Python的sklearn库构建一个SVM分类器,应用于数字货币的买卖策略预测。通过分析历史数据,预测价格走势,从而决定买入或卖出。
摘要由CSDN通过智能技术生成

支持向量机(SVM)是什么意思?

正好最近自己学习机器学习,看到reddit上 Please explain Support Vector Machines (SVM) like I am a 5 year old 的帖子,一个字赞!于是整理一下和大家分享。(如有错欢迎指教!)

什么是SVM?

支持向量机/support vector machine (SVM)。

当然首先看一下wiki.

Support Vector Machines are learning models used for classification: which individuals in a population belong where? So… how do SVM and the mysterious “kernel” work?

好吧,故事是这样子的:

在很久以前的情人节,大侠要去救他的爱人,但魔鬼和他玩了一个游戏。

魔鬼在桌子上似乎有规律放了两种颜色的球,说:“你用一根棍分开它们?要求:尽量在放更多球之后,仍然适用。”

….. 文章 详细内容 地址:https://www.botvs.com/bbs-topic/680

我们来用Python 实现一个 SVM 分类器 预测 买卖

程序是 基于 BotVS 平台的, 标的物选择为电子货币,因为 电子货币适合回测。

Python 机器学习之 SVM 预测买卖

Python入门简单策略 sklearn 机器学习库的使用

回测系统自带的库有

numpy pandas TA-Lib scipy statsmodels sklearn cvxopt hmmlearn pykalman arch matplotlib

实盘需要在托管者所在机器安装策略需要的库

from sklearn import svm

import numpy as np

def main():

preTime = 0

n = 0

success = 0

predict = None

pTime = None

marketPosition = 0

initAccount = exchange.GetAccount()

Log("Running...")

while True:

r = exchange.GetRecords()

if len(r) < 60:

continue

bar = r[len(r)-1]

if bar.Time > preTime:

preTime = bar.Time

if pTime is not None and r[len(r)-2].Time == pTime:

diff = r[len(r)-2].Close - r[len(r)-3].Close

if diff > SpreadVal:

success += 1 if predict == 0 else 0

elif diff < -SpreadVal:

success += 1 if predict == 1 else 0

else:

success += 1 if predict == 2 else 0

pTime = None

LogStatus("预测次数", n, "成功次数", success, "准确率:", '%.3f %%' % round(float(success) * 100 / n, 2))

else:

Sleep(1000)

continue

inputs_X, output_Y = [], []

sets = [None, None, None]

for i in xrange(1, len(r)-2, 1):

inputs_X.append([r[i].Open, r[i].Close])

Y = 0

diff = r[i+1].Close - r[i].Close

if diff > SpreadVal:

Y = 0

sets[0] = True

elif diff < -SpreadVal:

Y = 1

sets[1] = True

else:

Y = 2

sets[2] = True

output_Y.append(Y)

if None in sets:

Log("样本不足, 无法预测 ...")

continue

n += 1

clf = svm.LinearSVC()

clf.fit(inputs_X, output_Y)

predict = clf.predict(np.array([bar.Open, bar.Close]).reshape((1, -1)))

pTime = bar.Time

Log("预测当前Bar结束:", bar.Time, ['涨', '跌', '横'][predict])

if marketPosition == 0:

if predict == 0:

exchange.Buy(initAccount.Balance/2)

marketPosition = 1

elif predict == 1:

exchange.Sell(initAccount.Stocks/2)

marketPosition = -1

else:

nowAccount = exchange.GetAccount()

if marketPosition > 0 and predict != 0:

exchange.Sell(nowAccount.Stocks - initAccount.Stocks)

nowAccount = exchange.GetAccount()

marketPosition = 0

elif marketPosition < 0 and predict != 1:

while True:

dif = initAccount.Stocks - nowAccount.Stocks

if dif < 0.01:

break

ticker = exchange.GetTicker()

exchange.Buy(ticker.Sell + (ticker.Sell-ticker.Buy)*2, dif)

while True:

Sleep(1000)

orders = exchange.GetOrders()

for order in orders:

exchange.CancelOrder(order.Id)

if len(orders) == 0:

break

nowAccount = exchange.GetAccount()

marketPosition = 0

if marketPosition == 0:

LogProfit(_N(nowAccount.Balance - initAccount.Balance, 4), nowAccount)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值