当前位置: 甲型病毒性肝炎 > 疾病治疗 > Python分析一下双色球,中大奖指日
当前位置: 甲型病毒性肝炎 > 疾病治疗 > Python分析一下双色球,中大奖指日
来源:志斌的python笔记
作者:Seon
一、需求简介
之前偶然见到一位网友提出了关于双色球数据的分析需求,感觉颇有趣味,便着手操作了一番。如下为某双色球发布站的页面,可以看到每期会产生红/蓝两种颜色的数字,其中红球为33选6,蓝球为16选1,一共是从49个球中选7个。假如想对某一色号球下注,比如红1球,要先分析一下该球以往的产出情况。
可以从以下3个方面来分析,以史明鉴,看看最新一期是否值得选取该球:
1、遗漏情况统计遗漏n期后命中的次数,如上图:遗漏1期后中的次数有2次,遗漏2期后中的次数有1次,遗漏5期后中的次数有2次,遗漏7期后中的次数有1次。
2、连续重号情况如图,连续重号3期的有1次。
3、重号前的遗漏情况如图,重号前遗漏2期的有1次。
二、源数据节选源数据如下,数据是以html表格形式储存的,td标签中class="yl01"表示未命中,class="chartBall01"表示命中红球,class="chartBall02"表示命中蓝球(文末获取近期数据样本)。
tr
tdclass="c_fbf5e3bd_rt_a"/tdtdclass="yl01"style="font-size:xx-small"1/tdtdclass="yl01"style="font-size:xx-small"1/tdtdclass="yl01"style="font-size:xx-small"8/tdtdclass="yl01"style="font-size:xx-small"2/tdtdclass="chartBall01"05/tdtdclass="chartBall01"06/tdtdclass="yl01"style="font-size:xx-small"1/tdtdclass="yl01"style="font-size:xx-small"4/tdtdclass="yl01"style="font-size:xx-small"6/tdtdclass="yl01"style="font-size:xx-small"4/tdtdclass="yl01"style="font-size:xx-small"5/tdtdclass="chartBall01"12/tdtdclass="yl01"style="font-size:xx-small"1/tdtdclass="chartBall01"14/tdtdclass="yl01"style="font-size:xx-small"2/tdtdclass="yl01"style="font-size:xx-small"2/tdtdclass="yl01"style="font-size:xx-small"13/tdtdclass="yl01"style="font-size:xx-small"9/tdtdclass="yl01"style="font-size:xx-small"2/tdtdclass="yl01"style="font-size:xx-small"5/tdtdclass="yl01"style="font-size:xx-small"6/tdtdclass="yl01"style="font-size:xx-small"9/tdtdclass="yl01"style="font-size:xx-small"1/tdtdclass="yl01"style="font-size:xx-small"1/tdtdclass="yl01"style="font-size:xx-small"7/tdtdclass="yl01"style="font-size:xx-small"2/tdtdclass="chartBall01"27/tdtdclass="chartBall01"28/tdtdclass="yl01"style="font-size:xx-small"4/tdtdclass="yl01"style="font-size:xx-small"13/tdtdclass="yl01"style="font-size:xx-small"12/tdtdclass="yl01"style="font-size:xx-small"8/tdtdclass="yl01"style="font-size:xx-small"7/tdtdclass="v_line"/tdtdclass="yl01"style="font-size:xx-small"4/tdtdclass="yl01"style="font-size:xx-small"5/tdtdclass="yl01"style="font-size:xx-small"42/tdtdclass="yl01"style="font-size:xx-small"3/tdtdclass="yl01"style="font-size:xx-small"8/tdtdclass="yl01"style="font-size:xx-small"2/tdtdclass="yl01"style="font-size:xx-small"16/tdtdclass="chartBall02"08/tdtdclass="yl01"style="font-size:xx-small"10/tdtdclass="yl01"style="font-size:xx-small"13/tdtdclass="yl01"style="font-size:xx-small"54/tdtdclass="yl01"style="font-size:xx-small"1/tdtdclass="yl01"style="font-size:xx-small"11/tdtdclass="yl01"style="font-size:xx-small"31/tdtdclass="yl01"style="font-size:xx-small"18/tdtdclass="yl01"style="font-size:xx-small"25/td/trtrtdclass="c_fbf5e3bd_rt_a"/tdtdclass="yl01"style="font-size:xx-small"2/tdtdclass="yl01"style="font-size:xx-small"2/tdtdclass="yl01"style="font-size:xx-small"9/tdtdclass="chartBall01"04/tdtdclass="yl01"style="font-size:xx-small"1/tdtdclass="chartBall01"06/tdtdclass="yl01"style="font-size:xx-small"2/tdtdclass="yl01"style="font-size:xx-small"5/tdtdclass="yl01"style="font-size:xx-small"7/tdtdclass="yl01"style="font-size:xx-small"5/tdtdclass="yl01"style="font-size:xx-small"6/tdtdclass="yl01"style="font-size:xx-small"1/tdtdclass="yl01"style="font-size:xx-small"2/tdtdclass="yl01"style="font-size:xx-small"1/tdtdclass="yl01"style="font-size:xx-small"3/tdtdclass="chartBall01"16/tdtdclass="yl01"style="font-size:xx-small"14/tdtdclass="yl01"style="font-size:xx-small"10/tdtdclass="yl01"style="font-size:xx-small"3/tdtdclass="yl01"style="font-size:xx-small"6/tdtdclass="yl01"style="font-size:xx-small"7/tdtdclass="yl01"style="font-size:xx-small"10/tdtdclass="yl01"style="font-size:xx-small"2/tdtdclass="chartBall01"24/tdtdclass="yl01"style="font-size:xx-small"8/tdtdclass="chartBall01"26/tdtdclass="yl01"style="font-size:xx-small"1/tdtdclass="yl01"style="font-size:xx-small"1/tdtdclass="yl01"style="font-size:xx-small"5/tdtdclass="yl01"style="font-size:xx-small"14/tdtdclass="yl01"style="font-size:xx-small"13/tdtdclass="yl01"style="font-size:xx-small"9/tdtdclass="chartBall01"33/tdtdclass="v_line"/tdtdclass="yl01"style="font-size:xx-small"5/tdtdclass="yl01"style="font-size:xx-small"6/tdtdclass="yl01"style="font-size:xx-small"43/tdtdclass="yl01"style="font-size:xx-small"4/tdtdclass="yl01"style="font-size:xx-small"9/tdtdclass="yl01"style="font-size:xx-small"3/tdtdclass="yl01"style="font-size:xx-small"17/tdtdclass="yl01"style="font-size:xx-small"1/tdtdclass="yl01"style="font-size:xx-small"11/tdtdclass="yl01"style="font-size:xx-small"14/tdtdclass="yl01"style="font-size:xx-small"55/tdtdclass="yl01"style="font-size:xx-small"2/tdtdclass="yl01"style="font-size:xx-small"12/tdtdclass="yl01"style="font-size:xx-small"32/tdtdclass="yl01"style="font-size:xx-small"19/tdtdclass="chartBall02"16/td/tr
三、解析实现1、数据结构为了便于表示球的命中与颜色、数值,我们可以按以下规则映射:
那么先用美丽汤把需要的内容捞出来,把所有td标签中表示名字与色号的内容取出,按上述规则映射表示。
soup=BeautifulSoup(res_table,"html.parser")item_lst=[]fortdinsoup.find_all(td): cls=td[class][0] num=td.string ifclsin[yl01,chartBall01,chartBall02]: item_lst.append(-.join([cls,num]))print(item_lst[:10])
输出前10个预览如下:
[yl01-3,yl01-5,chartBall01-03,yl01-5,yl01-1,yl01-5,chartBall01-07,yl01-9,yl01-6,yl01-6]
但这仅仅是一个超长列表,在此基础上处理起来还找不到头绪,至少要转变成和网页上结构一致的矩阵才好理解吧,那么可以想到利用numpy矩阵。先是用np.array将普通列表转为numpy数组,再利用reshape将一维数组转为二维数组,即期X49个球的矩阵。
array=np.array(item_lst).reshape(,49)
如图,numpy矩阵的每一行与原页面结构对应。
但我们想要进行的是纵向比较分析,比较各期之间的关系,所以还需要再对矩阵进行行列转置。
array_T=array.T#矩阵转置,一个元素对应一列
此时,array_T中的每一个元素即表示某一色号的近期情况。如果想看红1球,则是array_T[0];如果想看红2球,则是array_T[1];如果想看蓝1球,则是array_T[33](因为红球总共有33个,故第一个蓝球列的索引为33)。为了便于选球,我们可以写一个映射方法:
deftrans_col(txt):#翻译列索引号,如红1=0,红33=32,蓝1=33,蓝16=48if"红"intxt:col=int(txt.replace(红,))-1else:col=int(txt.replace(蓝,))+32returncol
接下来就可以正式开始实现统计分析的需求啦!
2、遗漏统计将转置矩阵和指定色号球所在列传入,获取该列后加入一个结束标记,因至少要出现1次未命中+1次命中才判定为遗漏1期,所以从该列的第二项数据开始判断,若其不等于前一项的值,且当前项为命中,则记录前一项的值(遗漏期数)。之后将记录列表中的表示遗漏的标记yl01去除,仅保留期数。
deffun_miss(array,col):#统计该列中遗漏后命中的次数line=array[col].tolist()order_grp=[]fori,vinenumerate(line):ifi0:ifv!=line[i-1]andchartinv:order_grp.append(line[i-1])order_grp=[i.replace(yl01-,)foriinorder_grp]c=dict(Counter(order_grp))result=sorted(c.items(),key=lambdax:int(x[0]))foriinresult:print(f"遗漏{i[0]}期后中的次数有{i[1]}次")fun_miss(array_T,trans_col("红1"))#执行
统计排序后输出如下:
遗漏1期后中的次数有4次遗漏2期后中的次数有2次遗漏3期后中的次数有3次遗漏5期后中的次数有2次遗漏7期后中的次数有2次遗漏8期后中的次数有1次遗漏9期后中的次数有2次遗漏13期后中的次数有1次
3、连续重号统计与遗漏统计相同,传入两个参数,先确定要选取的色号列。当前项等于前一项,且当前项为命中时表示重号,连续重号时count计数+1,当出现不满足重号条件时表示连续重号中断,记录连续次数,并将计数清零,待下一轮重号出现时重新计数。
deffun_repeat(array,col):#统计该列中的重号次数line=array[col].tolist()count_grp=[]count=0fori,vinenumerate(line):ifi0:ifv==line[i-1]andchartinv:count+=1elifcount0:count_grp.append(count)count=0c=dict(Counter(count_grp))result=sorted(c.items(),key=lambdax:x[0])foriinresult:print(f"连续重号{i[0]}次的有{i[1]}次")fun_repeat(wt,trans_col("红2"))#执行
统计排序后输出如下:
连续重号1次的有1次连续重号2次的有1次
4、重号前的遗漏统计因要出现重号+遗漏才算入,所以至少从第三项数据开始判定。
deffun_return(array,col):#统计该列中重号前的遗漏次数line=array[col].tolist()order_grp=[]fori,vinenumerate(line):ifi1:ifv==line[i-1]andv!=line[i-2]andchartinv:order_grp.append(line[i-2])order_grp=[i.replace(yl01-,)foriinorder_grp]c=dict(Counter(order_grp))result=sorted(c.items(),key=lambdax:int(x[0]))foriinresult:print(f"重号前遗漏{i[0]}期的有{i[1]}次")fun_return(wt,trans_col("红1"))#执行
统计排序后输出如下:
重号前遗漏2期的有1次
四、小结emmmm...不过如此看来,单单分析一个球是不够滴,还要和其他色号深入对比,有兴趣的同学自己尝试吧哈哈哈!
温馨提示:赌博有风险,本文仅做数据处理技术讨论,不构成任何资金投入建议!(不要怪我标题党啦)
END今日互动+留言送书
你尝试的结果如何?欢迎评论区留言~我们将从评论区选出一条走心留言,送上精美书籍《机器学习数学基础(Python语言实现)》一本,快多多留言参与吧~·幅图表展示,降低理解难度。
·知识点丰富,满足机器学习必备数学知识。
·基于Python编程的“小试牛刀”,检验学习效果。
·20个“专家点拨”,帮助读者答疑解惑。
·数学思想和人工智能解决方案的有效实践。
·提供书中相关案例的源代码,方便读者学习参考。
本书是一本系统介绍机器学习所涉及的数学知识和相关Python编程的实例工具书,同时还介绍了非常经典的综合案例,除了编写机器学习的代码,还编写了深度学习的代码。本书一共分为两部分。
第一部分为数学基础知识部分,包含8个章节,介绍了微积分、线性代数、概率统计、信息论、模糊数学、随机过程、凸优化和图论的系统知识体系及几个数学知识点对应的Python编程实例。通过这些实例,读者能够了解Scikit-learn、Scikit-fuzzy、Theano、SymPy、NetworkX和CVXPY中相应的库函数的应用。
第二部分为案例部分,包含4个章节,介绍了微积分、线性代数和概率统计问题的建模方法、求解流程和编程实现,以及工业生产领域的Python实战,包含了机器学习算法和深度学习PyTorch框架的应用。
在学习本书内容前,建议读者先掌握基本的Python编程知识和数学基础,然后将本书通读一遍,了解本书的大概内容,最后再跟着实例进行操作。
本书既注重数学理论,又偏重编程实践,实用性强,适用于对编程有一定基础,对系统的数学知识非常渴望,想从事人工智能、大数据等方向研究的读者。同时也适合作为广大职业院校相关专业的教材或参考用书。
周洋。成都嘉捷信诚解决方案专家,拥有12年toB行业大数据相关经验,对工业大数据、智慧电厂、智慧城市、智慧交通、智慧安防等行业趋势发展有前瞻性判断力。对机器学习、深度学习、大数据、知识图谱等技术有深入研究。
我们会私下联系你收集信息地址进行邮寄,注意查收,收到书籍后欢迎反馈哦~对图书感兴趣的小伙伴也可以点击“阅读原文”自行购买哦~
预览时标签不可点收录于话题#个上一篇下一篇转载请注明:http://www.lygangv.com/jbzl/13368.html