当前位置: 移动技术网 > IT编程>脚本编程>Python > Python数据分析之双色球基于线性回归算法预测下期中奖结果示例

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

2019年06月18日  | 移动技术网IT编程  | 我要评论

酒色电影,短期培训班,软骨隆鼻要多少钱

本文实例讲述了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)

画图结果:

预测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字符串操作技巧汇总》、《python编码操作技巧总结》、《python数据结构与算法教程》、《python函数使用技巧总结》、《python入门与进阶经典教程》及《python文件与目录操作技巧汇总

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网