侯体宗的博客
  • 首页
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Python数据分析之双色球基于线性回归算法预测下期中奖结果示例

Python  /  管理员 发布于 7年前   215

本文实例讲述了Python数据分析之双色球基于线性回归算法预测下期中奖结果。分享给大家供大家参考,具体如下:

前面讲述了关于双色球的各种算法,这里将进行下期双色球号码的预测,想想有些小激动啊。

代码中使用了线性回归算法,这个场景使用这个算法,预测效果一般,各位可以考虑使用其他算法尝试结果。

发现之前有很多代码都是重复的工作,为了让代码看的更优雅,定义了函数,去调用,顿时高大上了

#!/usr/bin/python# -*- coding:UTF-8 -*-#导入需要的包import pandas as pdimport numpy as npimport matplotlib.pyplot as pltimport operatorfrom sklearn import datasets,linear_modelfrom sklearn.linear_model import LogisticRegression#读取文件df = pd.read_table('newdata.txt',header=None,sep=',')#读取日期tdate = sorted(df.loc[:,0])#将以列项为数据,将球号码取出,写入到csv文件中,并取50行数据# Function to red number to csv filedef RedToCsv(h_num,num,csv_name): h_num = df.loc[:,num:num].values h_num = h_num[50::-1] renum2 = pd.DataFrame(h_num) renum2.to_csv(csv_name,header=None) fp = file(csv_name) s = fp.read() fp.close() a = s.split('\n') a.insert(0, 'numid,number') s = '\n'.join(a) fp = file(csv_name, 'w') fp.write(s) fp.close()#调用取号码函数# create fileRedToCsv('red1',1,'rednum1data.csv')RedToCsv('red2',2,'rednum2data.csv')RedToCsv('red3',3,'rednum3data.csv')RedToCsv('red4',4,'rednum4data.csv')RedToCsv('red5',5,'rednum5data.csv')RedToCsv('red6',6,'rednum6data.csv')RedToCsv('blue1',7,'bluenumdata.csv')#获取数据,X_parameter为numid数据,Y_parameter为number数据# Function to get datadef get_data(file_name): data = pd.read_csv(file_name) X_parameter = [] Y_parameter = [] for single_square_feet ,single_price_value in zip(data['numid'],data['number']):  X_parameter.append([float(single_square_feet)])  Y_parameter.append(float(single_price_value)) return X_parameter,Y_parameter#训练线性模型# Function for Fitting our data to Linear modeldef linear_model_main(X_parameters,Y_parameters,predict_value): # Create linear regression object regr = linear_model.LinearRegression() #regr = LogisticRegression() regr.fit(X_parameters, Y_parameters) predict_outcome = regr.predict(predict_value) predictions = {} predictions['intercept'] = regr.intercept_ predictions['coefficient'] = regr.coef_ predictions['predicted_value'] = predict_outcome return predictions#获取预测结果函数def get_predicted_num(inputfile,num): X,Y = get_data(inputfile) predictvalue = 51 result = linear_model_main(X,Y,predictvalue) print "num "+ str(num) +" Intercept value " , result['intercept'] print "num "+ str(num) +" coefficient" , result['coefficient'] print "num "+ str(num) +" Predicted value: ",result['predicted_value']#调用函数分别预测红球、蓝球get_predicted_num('rednum1data.csv',1)get_predicted_num('rednum2data.csv',2)get_predicted_num('rednum3data.csv',3)get_predicted_num('rednum4data.csv',4)get_predicted_num('rednum5data.csv',5)get_predicted_num('rednum6data.csv',6)get_predicted_num('bluenumdata.csv',1)# 获取X,Y数据预测结果# X,Y = get_data('rednum1data.csv')# predictvalue = 21# result = linear_model_main(X,Y,predictvalue)# print "red num 1 Intercept value " , result['intercept']# print "red num 1 coefficient" , result['coefficient']# print "red num 1 Predicted value: ",result['predicted_value']# Function to show the resutls of linear fit modeldef show_linear_line(X_parameters,Y_parameters): # Create linear regression object regr = linear_model.LinearRegression() #regr = LogisticRegression() regr.fit(X_parameters, Y_parameters) plt.figure(figsize=(12,6),dpi=80) plt.legend(loc='best') plt.scatter(X_parameters,Y_parameters,color='blue') plt.plot(X_parameters,regr.predict(X_parameters),color='red',linewidth=4) plt.xticks(()) plt.yticks(()) plt.show()#显示模型图像,如果需要画图,将“获取X,Y数据预测结果”这块注释去掉,“调用函数分别预测红球、蓝球”这块代码注释下# show_linear_line(X,Y)

画图结果:

预测2016-05-15开奖结果:

实际开奖结果:05 06 10 16 22 26  11

以下为预测值:

#取5个数,计算的结果num 1 Intercept value 5.66666666667num 1 coefficient [-0.6]num 1 Predicted value: [ 2.06666667]num 2 Intercept value 7.33333333333num 2 coefficient [ 0.2]num 2 Predicted value: [ 8.53333333]num 3 Intercept value 14.619047619num 3 coefficient [-0.51428571]num 3 Predicted value: [ 11.53333333]num 4 Intercept value 17.7619047619num 4 coefficient [-0.37142857]num 4 Predicted value: [ 15.53333333]num 5 Intercept value 21.7142857143num 5 coefficient [ 1.11428571]num 5 Predicted value: [ 28.4]num 6 Intercept value 28.5238095238num 6 coefficient [ 0.65714286]num 6 Predicted value: [ 32.46666667]num 1 Intercept value 9.57142857143num 1 coefficient [-0.82857143]num 1 Predicted value: [ 4.6]

四舍五入结果:

2 9 12 16 28 33 5

#取12个数,计算的结果四舍五入:3 7 12 15 24 30 7#取15个数,计算的结果四舍五入:4 7 13 15 25 31 7#取18个数,计算的结果四舍五入:4 8 13 16 23 31 8#取20个数,计算的结果四舍五入:4 7 12 22 24 27 10#取25个数,计算的结果四舍五入:7 8 13 17 24 30 6#取50个数,计算的结果四舍五入:4 10 14 18 23 29 8#取100个数,计算的结果四舍五入:5 11 15 19 24 29 8#取500个数,计算的结果四舍五入:5 10 15 20 24 29 9#取1000个数,计算的结果四舍五入:5 10 14 19 24 29 9#取1939个数,计算的结果四舍五入:5 10 14 19 24 29 9

看来预测中奖真是有些难度,随机性太高,双色球预测案例,只是为了让入门数据分析的朋友有些思路,要想中大奖还是有难度的,多做好事善事多积德行善吧。

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数学运算技巧总结》、《Python字符串操作技巧汇总》、《Python编码操作技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

希望本文所述对大家Python程序设计有所帮助。


  • 上一条:
    python OpenCV学习笔记实现二维直方图
    下一条:
    Python编程argparse入门浅析
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(95个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客