美文网首页
决策树预测 stock

决策树预测 stock

作者: NazgulSun | 来源:发表于2020-03-27 11:31 被阅读0次

    场景切合程度

    平时我们看一个股票,通常会找出特别的指标。有财务的,有短期技术类的,有中期动量类的等等。
    也就是非常经典的多因子模型,比如找出50个因子,然后由于人工决策,我们看会不知觉的选择一些因子去预测
    明天股票的涨跌。 整个过程非常像一颗决策树。

    使用决策树建模

    通过各大平台的 python 金融包,可以非常方便的获得各类因子。可能需要专业版权限。
    比如我人为的选取了50个因子,作为特征值。然后未来一天股票的涨跌作为 标签。构建数据集

    使用python 库。

    # encoding=utf-8
    
    import pandas as pd
    import time
    
    from sklearn.model_selection import train_test_split
    from sklearn.metrics import accuracy_score
    
    from sklearn.tree import DecisionTreeClassifier
    from sklearn import metrics
    
    
    if __name__ == '__main__':
        print("Start read data...")
        time_1 = time.time()
        raw_data = pd.read_csv('../file/mfactors.csv', header=0)  # 读取csv数据
        data = raw_data.values
        labels = data[::, -1][1:].astype(np.int32)
        #features = data[:-1,::][::, 3:-2]
        features = data[:-1,::][::, 3:-20]
        # 随机选取33%数据作为测试集,剩余为训练集
        train_features, test_features, train_labels, test_labels = train_test_split(features, labels, test_size=0.2, random_state=0)
        print('Start training...')
        # criterion可选‘gini’, ‘entropy’,默认为gini(对应CART算法),entropy为信息增益(对应ID3算法)
        clf = DecisionTreeClassifier(criterion='gini',splitter='best',max_depth=5)
        clf.fit(train_features, train_labels.astype('int'))
        print('Start predicting...')
        test_predict = clf.predict(test_features)
        score = accuracy_score(test_labels, test_predict)
        print("The accruacy score is %f" % score)
        print("cha zhun lv:",metrics.precision_score(test_labels, test_predict))
        print("zhao hui lv",metrics.recall_score(test_labels, tliest_predict))
        print("F1_score:",metrics.f1_score(test_labels, test_predict))
    
    
        # 下面这个参数对这个股票的查准率,18/24=68%. 意思是只要预测涨,就可以买一买
        # 参数调整 【https://www.jianshu.com/p/d1d17499365c】【https://www.cnblogs.com/chenyaling/p/7236435.html】
        # https://blog.csdn.net/qq_41577045/article/details/79844709
        #clf = DecisionTreeClassifier(criterion='gini',splitter='best',max_depth=5)
    

    模型解释

    经过参数调优, f1值 不到0.5. 对于一个预测来讲,的确很难。
    但是我们看到查准率在0.68. 这个就可以利用起来。
    查准率的意思。 我预测10个股票上涨,6.8个真的上涨。 使用的时候。不要关注预测下跌的情况。
    就关注 他精准的一面。 对应的策略。是如果模型预测上涨了。就安装凯利公式。给与一定的仓位。
    当然这个是个人想法。还没有完成回测程序。

    未来的优化

    从自选池中,为各个股票建立模型。挑出结果比较好的,构建动态的组合仓位。
    另外,尝试新的预测算法,不同算法之间做比较,看适合哪些场景。

    性能优化点

    1) checkVertex , 原版本是默认 一个一个点的查询,最好做成 batch 功能。 这个修改 verteAPi即可
    2) gremlin within 方法,根据 属性查询源节点和目标节点的时候,依旧是 一条一条查询。而不是 in List 方法。
    可以修改 condition flatten 逻辑,使用 inlist 方法。 但是查询 g_si 表,用 inList 方法,貌似也要2s。每500 as a batch
    gremlin 的原句是 : g.V().hasLabel().has(eid, within(ids))
    查询数据库的时候, select * from g_si where field_values 速度会远远快于
    select * from g_si where field_values and label= , 这个是什么原因?

    相关文章

      网友评论

          本文标题:决策树预测 stock

          本文链接:https://www.haomeiwen.com/subject/dzqluhtx.html