python计算双色球数字概率_python绘制双色球走势图
友情提示:双色球预测分析只为练习技术,不要抱着预测中大奖的心态,不可能预测准确。 python数据分析1:获取双色球历史信息 python数据分析2:双色球 蓝红球分析统计 python数据分析3:双色球 单个红和蓝球哪个比例高 python数据分析4:双色球 两个红和蓝球哪组合比例高 python数据分析5:双色球 两个红球哪组合比例高 python数据分析6:双色球 使用线性回归算法预测下期中奖结果
本次将进行下期双色球号码的预测,想想有些小激动啊。
代码中使用了线性回归算法,这个场景使用这个算法,预测效果一般,各位可以考虑使用其他算法尝试结果。
发现之前有很多代码都是重复的工作,为了让代码看的更优雅,定义了函数,去调用,顿时高大上了
#!/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)
画图结果:
预测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
看来预测中奖真是有些难度,随机性太高,双色球预测案例,只是为了让入门数据分析的朋友有些思路,要想中大奖还是有难度的,多做好事善事多积德行善吧。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/171743.html原文链接:https://javaforall.cn
相关文章
- 二级Python选择题_二级python选择题题库
- python 基尼系数_Python计算
- python计算最大公约数和最小公倍数_python怎么求最大公约数和最小公倍数
- python解压bz2文件命令,在Python中解压缩.bz2文件
- Python-正则匹配
- python监控网站更新_Python 通过网站search功能监控网站内容更新[通俗易懂]
- python表情代码_Python实现表情包的代码实例[通俗易懂]
- 【说站】Python类属性如何使用
- 【说站】python查找计算函数的整理
- 简述Python特点_python优缺点
- python读取pkl_Python 读取文件
- Python爬虫抓取网站模板的完整版实现
- python中赋值以及平均值计算的两个小坑
- 在python中用来安装第三方库的常用工具_什么库用于安装管理Python扩展包
- python django 数据库_Apache+Mysql+PHP/Python简单项目
- Python应用之计算阶乘
- 云签——基于python flask的微信公众平台的动态签到
- Python - 一文入门Flask(Blueprint、SQLAlchemy部分)
- Python 强化学习实用指南:1~5
- python调用os.exit()方法来终止进程详解编程语言
- 计算时间差,时间加减运算python代码详解编程语言
- python在windows锁屏的代码详解编程语言
- 使用 Python 和 Pygame 模块构建一个游戏框架
- Python爬取MySQL数据,助力数据分析(python读取mysql数据)
- Linux系统下安装Python模块指南(linux安装python模块)
- 在Linux系统中使用Python编程语言(linux中python)
- Linux下的Python开发技术之旅(linux下python开发)
- python实现在pickling的时候压缩的方法