用Python實現乙個SVM分類器策略

2021-09-11 12:57:37 字數 3326 閱讀 1674

正好最近自己學習機器學習,看到reddit上 please explain support vector machines (svm) like i am a 5 year old 的帖子,乙個字讚!於是整理一下和大家分享。(如有錯歡迎指教!)

支援向量機/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?

複製**

好吧,故事是這樣子的:

程式是 基於發明者量化平台的,標的物選擇為電子貨幣,因為電子貨幣適合回測。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):

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

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)

```[閱讀原文](複製**

用python建模 用Python建模乙個線性系統

我知道這有點舊,但搜尋引起了我的這個問題。當我找不到乙個好的模組時,我把它放在一起。這並不多,但如果有人發現他們在這裡,這是乙個好的開始。import matplotlib.pylab as plt import numpy as np import scipy.signal def bode g,...

python實現乙個二分查詢

二分查詢 二分查詢也稱折半查詢 binary search 它是一種效率較高的查詢方法。但是,折半查詢要求線性表必須採用順序儲存結構,而且表中元素按關鍵字有序排列 查詢過程 首先,假設表中元素是按公升序排列,將表中間位置記錄的關鍵字與查詢關鍵字比較,如果兩者相等,則查詢成功 否則利用中間位置記錄將表...

用python實現乙個簡單的cache系統

2013年01月02日 綜合 共 2016字 字型大小 小 中 大 原文檢視 本文章的 本篇文章將介紹python中的decorator,中文翻譯為 裝飾器 魔法。在這篇文章中我們將熟悉decorator使用的基本方式和基本使用例子,並利用decorator來實現乙個高階的例子 快取系統 cache...