科学经得起实践检验-python3.6通过决策树实战精准准确预测今日大盘走势(含代码)...

本文主要是介绍科学经得起实践检验-python3.6通过决策树实战精准准确预测今日大盘走势(含代码)...,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

科学经得起实践检验-python3.6通过决策树实战精准准确预测今日大盘走势(含代码)




春有百花秋有月,夏有凉风冬有雪;

若无闲事挂心头,便是人间好时节。

  

  --宋.无门慧开


不废话了,以下训练模型数据,采用本人发明的极致800实时指数近期的一些实际数据,

预测采用今日的真实数据

#coding=utf-8
__author__ = 'huangzhi'

import math
import operatordef calcShannonEnt(dataset):numEntries = len(dataset)labelCounts = {}for featVec in dataset:currentLabel = featVec[-1]if currentLabel not in labelCounts.keys():labelCounts[currentLabel] = 0
        labelCounts[currentLabel] += 1

    shannonEnt = 0.0
    for key in labelCounts:prob = float(labelCounts[key]) / numEntriesshannonEnt -= prob * math.log(prob, 2)return shannonEntdef CreateDataSet():# dataset = [[1, 1, 'yes'],
    #            [1, 1, 'yes'],
    #            [1, 0, 'no'],
    #            [0, 1, 'no'],
    #            [0, 1, 'no']]

    dataset = [[3, 4, 100, 85, 4, 6, 110, 120, 4, 6, 151, 122, 8, 12, 110, 185, ''],[5, 7, 88, 85, 6, 8, 100, 130, 6, 9, 131, 132, 8, 14, 100, 195, ''],[6, 2, 60, 20, 9, 3, 80, 22, 16, 4, 131, 32, 33, 5, 160, 45, ''],[3, 4, 100, 105, 4, 6, 110, 120, 4, 6, 151, 122, 8, 12, 110, 185, ''],[5, 3, 50, 30, 8, 4, 70, 28, 12, 6, 101, 42, 28, 7, 120, 35, ''],[2, 6, 60, 95, 4, 8, 90, 130, 6, 11, 101, 142, 9, 15, 99, 145, ''],[5, 3, 70, 30, 8, 4, 90, 32, 22, 6, 141, 42, 43, 8, 150, 65, ''],[2, 8, 30, 60, 9, 8, 80, 90, 9, 20, 140, 160, 12, 32, 101, 205, '']]labels = ['l1', 'l2', 'l3', 'l4', 'l5', 'l6', 'l7', 'l8', 'l9', 'l11', 'l12', 'l13', 'l14', 'l15', 'l16', 'l17']return dataset, labelsdef splitDataSet(dataSet, axis, value):retDataSet = []for featVec in dataSet:if featVec[axis] == value:reducedFeatVec = featVec[:axis]reducedFeatVec.extend(featVec[axis + 1:])retDataSet.append(reducedFeatVec)return retDataSetdef chooseBestFeatureToSplit(dataSet):numberFeatures = len(dataSet[0]) - 1
    baseEntropy = calcShannonEnt(dataSet)bestInfoGain = 0.0;bestFeature = -1;for i in range(numberFeatures):featList = [example[i] for example in dataSet]# print(featList)
        uniqueVals = set(featList)# print(uniqueVals)
        newEntropy = 0.0
        for value in uniqueVals:subDataSet = splitDataSet(dataSet, i, value)prob = len(subDataSet) / float(len(dataSet))newEntropy += prob * calcShannonEnt(subDataSet)infoGain = baseEntropy - newEntropyif (infoGain > bestInfoGain):bestInfoGain = infoGainbestFeature = ireturn bestFeaturedef majorityCnt(classList):classCount = {}for vote in classList:if vote not in classCount.keys():classCount[vote] = 0
        classCount[vote] = 1
    sortedClassCount = sorted(classCount.iteritems(), key=operator.itemgetter(1), reverse=True)return sortedClassCount[0][0]def createTree(dataSet, inputlabels):labels = inputlabels[:]classList = [example[-1] for example in dataSet]if classList.count(classList[0]) == len(classList):return classList[0]if len(dataSet[0]) == 1:return majorityCnt(classList)bestFeat = chooseBestFeatureToSplit(dataSet)bestFeatLabel = labels[bestFeat]myTree = {bestFeatLabel: {}}del (labels[bestFeat])featValues = [example[bestFeat] for example in dataSet]uniqueVals = set(featValues)for value in uniqueVals:subLabels = labels[:]myTree[bestFeatLabel][value] = createTree(splitDataSet(dataSet, bestFeat, value), subLabels)return myTreedef classify(inputTree, featLabels, testVec):firstStr = list(inputTree.keys())[0]secondDict = inputTree[firstStr]featIndex = featLabels.index(firstStr)for key in secondDict.keys():if testVec[featIndex] == key:if type(secondDict[key]).__name__ == 'dict':classLabel = classify(secondDict[key], featLabels, testVec)else:classLabel = secondDict[key]return classLabelmyDat, labels = CreateDataSet()
# print(calcShannonEnt(myDat))

# print(splitDataSet(myDat, 1, 1))

# print(chooseBestFeatureToSplit(myDat))

myTree = createTree(myDat, labels)#通过早上9:41分的实际数据进行预测
print(classify(myTree, labels, [1, 6, 156, 169, 1, 6, 156, 169, 1, 6, 156, 169, 1, 6, 156, 169]))
#通过早上10:41分的实际数据进行预测
print(classify(myTree, labels, [1, 6, 156, 169, 4, 9, 129, 263, 4, 9, 129, 263, 4, 9, 129, 263]))
#通过下午13:41分的实际数据进行预测
print(classify(myTree, labels, [1, 6, 156, 169, 4, 9, 129, 263, 5, 12, 123, 306, 5, 12, 123, 306]))
#通过下午14:41分的实际数据进行预测
print(classify(myTree, labels, [1, 6, 156, 169, 4, 9, 129, 263, 5, 12, 123, 306, 6, 13, 99, 397]))
 
 
运行结果如下:
D:\Programs\Python\Python36-64\python.exe D:/pyfenlei/决策树/jcs4.py
跌
跌
跌
跌

转载于:https://www.cnblogs.com/bdccloudy/p/7665206.html

这篇关于科学经得起实践检验-python3.6通过决策树实战精准准确预测今日大盘走势(含代码)...的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:https://blog.csdn.net/weixin_30418341/article/details/95120421
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/554324

相关文章

在Java中使用OpenCV实践

《在Java中使用OpenCV实践》用户分享了在Java项目中集成OpenCV4.10.0的实践经验,涵盖库简介、Windows安装、依赖配置及灰度图测试,强调其在图像处理领域的多功能性,并计划后续探... 目录前言一 、OpenCV1.简介2.下载与安装3.目录说明二、在Java项目中使用三 、测试1.测

PyTorch中的词嵌入层(nn.Embedding)详解与实战应用示例

《PyTorch中的词嵌入层(nn.Embedding)详解与实战应用示例》词嵌入解决NLP维度灾难,捕捉语义关系,PyTorch的nn.Embedding模块提供灵活实现,支持参数配置、预训练及变长... 目录一、词嵌入(Word Embedding)简介为什么需要词嵌入?二、PyTorch中的nn.Em

MyBatis-Plus 自动赋值实体字段最佳实践指南

《MyBatis-Plus自动赋值实体字段最佳实践指南》MyBatis-Plus通过@TableField注解与填充策略,实现时间戳、用户信息、逻辑删除等字段的自动填充,减少手动赋值,提升开发效率与... 目录1. MyBATis-Plus 自动赋值概述1.1 适用场景1.2 自动填充的原理1.3 填充策略

在IntelliJ IDEA中高效运行与调试Spring Boot项目的实战步骤

《在IntelliJIDEA中高效运行与调试SpringBoot项目的实战步骤》本章详解SpringBoot项目导入IntelliJIDEA的流程,教授运行与调试技巧,包括断点设置与变量查看,奠定... 目录引言:为良驹配上好鞍一、为何选择IntelliJ IDEA?二、实战:导入并运行你的第一个项目步骤1

Olingo分析和实践之EDM 辅助序列化器详解(最佳实践)

《Olingo分析和实践之EDM辅助序列化器详解(最佳实践)》EDM辅助序列化器是ApacheOlingoOData框架中无需完整EDM模型的智能序列化工具,通过运行时类型推断实现灵活数据转换,适用... 目录概念与定义什么是 EDM 辅助序列化器?核心概念设计目标核心特点1. EDM 信息可选2. 智能类

Spring Boot3.0新特性全面解析与应用实战

《SpringBoot3.0新特性全面解析与应用实战》SpringBoot3.0作为Spring生态系统的一个重要里程碑,带来了众多令人兴奋的新特性和改进,本文将深入解析SpringBoot3.0的... 目录核心变化概览Java版本要求提升迁移至Jakarta EE重要新特性详解1. Native Ima

Olingo分析和实践之OData框架核心组件初始化(关键步骤)

《Olingo分析和实践之OData框架核心组件初始化(关键步骤)》ODataSpringBootService通过初始化OData实例和服务元数据,构建框架核心能力与数据模型结构,实现序列化、URI... 目录概述第一步:OData实例创建1.1 OData.newInstance() 详细分析1.1.1

Spring Boot 与微服务入门实战详细总结

《SpringBoot与微服务入门实战详细总结》本文讲解SpringBoot框架的核心特性如快速构建、自动配置、零XML与微服务架构的定义、演进及优缺点,涵盖开发环境准备和HelloWorld实战... 目录一、Spring Boot 核心概述二、微服务架构详解1. 微服务的定义与演进2. 微服务的优缺点三

Olingo分析和实践之ODataImpl详细分析(重要方法详解)

《Olingo分析和实践之ODataImpl详细分析(重要方法详解)》ODataImpl.java是ApacheOlingoOData框架的核心工厂类,负责创建序列化器、反序列化器和处理器等组件,... 目录概述主要职责类结构与继承关系核心功能分析1. 序列化器管理2. 反序列化器管理3. 处理器管理重要方

SpringBoot集成MyBatis实现SQL拦截器的实战指南

《SpringBoot集成MyBatis实现SQL拦截器的实战指南》这篇文章主要为大家详细介绍了SpringBoot集成MyBatis实现SQL拦截器的相关知识,文中的示例代码讲解详细,有需要的小伙伴... 目录一、为什么需要SQL拦截器?二、MyBATis拦截器基础2.1 核心接口:Interceptor