科学经得起实践检验-python3.6通过决策树实战精准准确预测今日大盘走势(含代码)
来源:互联网 发布:陈氏书法家 软件 编辑:程序博客网 时间:2024/05/01 15:03
科学经得起实践检验-python3.6通过决策树实战精准准确预测今日大盘走势(含代码)
春有百花秋有月,夏有凉风冬有雪;
若无闲事挂心头,便是人间好时节。
--宋.无门慧开
不废话了,以下训练模型数据,采用本人发明的极致800实时指数近期的一些实际数据,
预测采用今日的真实数据
#coding=utf-8__author__ = 'huangzhi'import mathimport 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]) / numEntries shannonEnt -= 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 - newEntropy if (infoGain > bestInfoGain): bestInfoGain = infoGain bestFeature = i return 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跌跌跌跌
阅读全文
0 0
- 科学经得起实践检验-python3.6通过决策树实战精准准确预测今日大盘走势(含代码)
- python3.5《机器学习实战》学习笔记(五):决策树算法实战之预测隐形眼镜类型
- Python3:《机器学习实战》之决策树算法(3)预测隐形眼镜类型
- 【机器学习实战-python3】决策树ID3
- 机器学习实战-python3决策树实例
- C4.5决策树+代码实践
- 决策树实践,参考《机器学习实战》
- spark高级数据分析实战---用决策树预测森林植被
- 【机器学习实战】决策树预测Titanic遇难者生还情况
- 2015.3.31大盘走势分析,及次日行情预测。
- 【机器学习实战】决策树-Python3文件读写错误小结
- 机器学习实战—决策树(python3+spyder)
- 6利用决策树模型预测天气质量
- 5-2 决策树算法预测销量高低代码
- ID3构造决策树预测隐形眼镜类型(代码笔记)
- 《机器学习实战》——决策树代码
- 《机器学习实战》决策树代码详解
- 【python实战】3 决策树(代码讲解)
- linux下常用的针对进程和线程的一些命令
- Hibernate初运行异常---:MySQL server version for the right syntax to use near 'type=InnoDB'
- 第七次作业
- Vue.extend构造器
- ContextLoaderListener的作用
- 科学经得起实践检验-python3.6通过决策树实战精准准确预测今日大盘走势(含代码)
- 生产者--消费者模式,理解多线程并发
- java 序列化与反序列化总结
- JS&JQUERY详解
- VC INT转RGB
- PreparedStatement用法
- 第十二次实验 继承
- request.getparameter和 request.getattribute的区别
- poj1644分苹果