Python數(shù)據(jù)分析之雙色球基于線性回歸算法預(yù)測(cè)下期中獎(jiǎng)結(jié)果示例
本文實(shí)例講述了Python數(shù)據(jù)分析之雙色球基于線性回歸算法預(yù)測(cè)下期中獎(jiǎng)結(jié)果。分享給大家供大家參考,具體如下:
前面講述了關(guān)于雙色球的各種算法,這里將進(jìn)行下期雙色球號(hào)碼的預(yù)測(cè),想想有些小激動(dòng)啊。
代碼中使用了線性回歸算法,這個(gè)場(chǎng)景使用這個(gè)算法,預(yù)測(cè)效果一般,各位可以考慮使用其他算法嘗試結(jié)果。
發(fā)現(xiàn)之前有很多代碼都是重復(fù)的工作,為了讓代碼看的更優(yōu)雅,定義了函數(shù),去調(diào)用,頓時(shí)高大上了
#!/usr/bin/python
# -*- coding:UTF-8 -*-
#導(dǎo)入需要的包
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])
#將以列項(xiàng)為數(shù)據(jù),將球號(hào)碼取出,寫入到csv文件中,并取50行數(shù)據(jù)
# 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()
#調(diào)用取號(hào)碼函數(shù)
# 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')
#獲取數(shù)據(jù),X_parameter為numid數(shù)據(jù),Y_parameter為number數(shù)據(jù)
# 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
#訓(xùn)練線性模型
# 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
#獲取預(yù)測(cè)結(jié)果函數(shù)
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']
#調(diào)用函數(shù)分別預(yù)測(cè)紅球、藍(lán)球
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數(shù)據(jù)預(yù)測(cè)結(jié)果
# 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數(shù)據(jù)預(yù)測(cè)結(jié)果”這塊注釋去掉,“調(diào)用函數(shù)分別預(yù)測(cè)紅球、藍(lán)球”這塊代碼注釋下
# show_linear_line(X,Y)
畫圖結(jié)果:

預(yù)測(cè)2016-05-15開獎(jiǎng)結(jié)果:
實(shí)際開獎(jiǎng)結(jié)果:05 06 10 16 22 26 11
以下為預(yù)測(cè)值:
#取5個(gè)數(shù),計(jì)算的結(jié)果 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]
四舍五入結(jié)果:
2 9 12 16 28 33 5
#取12個(gè)數(shù),計(jì)算的結(jié)果四舍五入: 3 7 12 15 24 30 7 #取15個(gè)數(shù),計(jì)算的結(jié)果四舍五入: 4 7 13 15 25 31 7 #取18個(gè)數(shù),計(jì)算的結(jié)果四舍五入: 4 8 13 16 23 31 8 #取20個(gè)數(shù),計(jì)算的結(jié)果四舍五入: 4 7 12 22 24 27 10 #取25個(gè)數(shù),計(jì)算的結(jié)果四舍五入: 7 8 13 17 24 30 6 #取50個(gè)數(shù),計(jì)算的結(jié)果四舍五入: 4 10 14 18 23 29 8 #取100個(gè)數(shù),計(jì)算的結(jié)果四舍五入: 5 11 15 19 24 29 8 #取500個(gè)數(shù),計(jì)算的結(jié)果四舍五入: 5 10 15 20 24 29 9 #取1000個(gè)數(shù),計(jì)算的結(jié)果四舍五入: 5 10 14 19 24 29 9 #取1939個(gè)數(shù),計(jì)算的結(jié)果四舍五入: 5 10 14 19 24 29 9
看來預(yù)測(cè)中獎(jiǎng)?wù)媸怯行╇y度,隨機(jī)性太高,雙色球預(yù)測(cè)案例,只是為了讓入門數(shù)據(jù)分析的朋友有些思路,要想中大獎(jiǎng)還是有難度的,多做好事善事多積德行善吧。
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
Python?jpg快速轉(zhuǎn)png并調(diào)整大小方式
這篇文章主要介紹了Python實(shí)現(xiàn)jpg快速轉(zhuǎn)png并調(diào)整大小方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
Jupyter Notebook折疊輸出的內(nèi)容實(shí)例
這篇文章主要介紹了Jupyter Notebook折疊輸出的內(nèi)容實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04
Python基于Tensorflow2.X實(shí)現(xiàn)汽車油耗預(yù)測(cè)
這篇文章主要為大家詳細(xì)介紹了Python基于Tensorflow2.X實(shí)現(xiàn)汽車油耗預(yù)測(cè)的相關(guān)方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-02-02
Qt實(shí)現(xiàn)炫酷啟動(dòng)圖動(dòng)態(tài)進(jìn)度條效果
最近接到一個(gè)新需求,讓做一個(gè)動(dòng)效進(jìn)度條。剛接手這個(gè)項(xiàng)目真的不知所措,后來慢慢理清思路,問題迎刃而解,下面小編通過本文給大家?guī)砹薗t實(shí)現(xiàn)炫酷啟動(dòng)圖動(dòng)態(tài)進(jìn)度條效果,感興趣的朋友一起看看吧2021-11-11
Python?print函數(shù):如何將對(duì)象打印輸出
這篇文章主要介紹了Python?print函數(shù):如何將對(duì)象打印輸出,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
tkinter動(dòng)態(tài)顯示時(shí)間的兩種實(shí)現(xiàn)方法
這篇文章主要介紹了tkinter動(dòng)態(tài)顯示時(shí)間的兩種實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01

