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


Posted in Python onFebruary 08, 2018

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

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

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

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

#!/usr/bin/python
# -*- coding:UTF-8 -*-
#导入需要的包
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import operator
from sklearn import datasets,linear_model
from 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 file
def 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 file
RedToCsv('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 data
def 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 model
def 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 model
def 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)

画图结果:

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

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

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

以下为预测值:

#取5个数,计算的结果
num 1 Intercept value 5.66666666667
num 1 coefficient [-0.6]
num 1 Predicted value: [ 2.06666667]
num 2 Intercept value 7.33333333333
num 2 coefficient [ 0.2]
num 2 Predicted value: [ 8.53333333]
num 3 Intercept value 14.619047619
num 3 coefficient [-0.51428571]
num 3 Predicted value: [ 11.53333333]
num 4 Intercept value 17.7619047619
num 4 coefficient [-0.37142857]
num 4 Predicted value: [ 15.53333333]
num 5 Intercept value 21.7142857143
num 5 coefficient [ 1.11428571]
num 5 Predicted value: [ 28.4]
num 6 Intercept value 28.5238095238
num 6 coefficient [ 0.65714286]
num 6 Predicted value: [ 32.46666667]
num 1 Intercept value 9.57142857143
num 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实现博客文章爬虫示例
Feb 26 Python
django2用iframe标签完成网页内嵌播放b站视频功能
Jun 20 Python
python爬虫之urllib3的使用示例
Jul 09 Python
详解python爬虫系列之初识爬虫
Apr 06 Python
python登录WeChat 实现自动回复实例详解
May 28 Python
浅谈keras保存模型中的save()和save_weights()区别
May 21 Python
python不到50行代码完成了多张excel合并的实现示例
May 28 Python
Python新手如何进行闭包时绑定变量操作
May 29 Python
浅谈pytorch中torch.max和F.softmax函数的维度解释
Jun 28 Python
pycharm实现猜数游戏
Dec 07 Python
详解pandas中利用DataFrame对象的.loc[]、.iloc[]方法抽取数据
Dec 13 Python
python 基于pygame实现俄罗斯方块
Mar 02 Python
Python编程argparse入门浅析
Feb 07 #Python
PyQt5主窗口动态加载Widget实例代码
Feb 07 #Python
学习python中matplotlib绘图设置坐标轴刻度、文本
Feb 07 #Python
PyQt5打开文件对话框QFileDialog实例代码
Feb 07 #Python
python OpenCV学习笔记直方图反向投影的实现
Feb 07 #Python
Python实现上下班抢个顺风单脚本
Feb 07 #Python
Python SqlAlchemy动态添加数据表字段实例解析
Feb 07 #Python
You might like
php模拟post提交数据的方法
2015/02/12 PHP
PHP的Yii框架中使用数据库的配置和SQL操作实例教程
2016/03/17 PHP
Yii净化器CHtmlPurifier用法示例(过滤不良代码)
2016/07/15 PHP
PHP flush 函数使用注意事项
2016/08/26 PHP
php之可变函数的实例详解
2017/09/13 PHP
JS解析XML的实现代码
2009/11/12 Javascript
两种常用的javascript数组去重方法思路及代码
2013/03/26 Javascript
怎么判断js脚本加载完成
2014/02/28 Javascript
利用jQuery实现打字机字幕效果实例代码
2016/09/02 Javascript
Kendo Grid editing 自定义验证报错提示的解决方法
2016/11/18 Javascript
jQuery基于随机数解决中午吃什么去哪吃问题示例
2018/12/29 jQuery
Vue-input框checkbox强制刷新问题
2019/04/18 Javascript
[58:35]OG vs EG 2019国际邀请赛淘汰赛 胜者组 BO3 第二场 8.22
2019/09/05 DOTA
用C++封装MySQL的API的教程
2015/05/06 Python
django manage.py扩展自定义命令方法
2018/05/27 Python
python实现下载pop3邮件保存到本地
2018/06/19 Python
python opencv判断图像是否为空的实例
2019/01/26 Python
pandas去除重复列的实现方法
2019/01/29 Python
pyqt弹出新对话框,以及关闭对话框获取数据的实例
2019/06/18 Python
Python socket非阻塞模块应用示例
2019/09/12 Python
pytorch 获取tensor维度信息示例
2020/01/03 Python
浅谈pycharm导入pandas包遇到的问题及解决
2020/06/01 Python
django教程如何自学
2020/07/31 Python
css3 旋转按钮 使用CSS3创建一个旋转可变色按钮
2012/12/31 HTML / CSS
HTML5 3D书本翻页动画的实现示例
2019/08/28 HTML / CSS
Clarins娇韵诗英国官网:来自法国的天然护肤品牌
2017/04/18 全球购物
英国领先的在线药房:Pharmacy First
2017/09/10 全球购物
Hush Puppies澳大利亚官网:舒适的男女休闲和正装鞋
2019/08/24 全球购物
父亲追悼会答谢词
2014/01/17 职场文书
质量标语大全
2014/06/12 职场文书
世界气象日活动总结
2015/02/27 职场文书
诚信考试承诺书范文
2015/04/29 职场文书
2015年医务科工作总结范文
2015/05/26 职场文书
2015年中秋晚会主持稿
2015/07/30 职场文书
2016新年晚会开场白
2015/12/03 职场文书
python geopandas读取、创建shapefile文件的方法
2021/06/29 Python