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

預(yù)測2016-05-15開獎結(jié)果:
實際開獎結(jié)果:05 06 10 16 22 26 11
以下為預(yù)測值:
#取5個數(shù),計算的結(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個數(shù),計算的結(jié)果四舍五入: 3 7 12 15 24 30 7 #取15個數(shù),計算的結(jié)果四舍五入: 4 7 13 15 25 31 7 #取18個數(shù),計算的結(jié)果四舍五入: 4 8 13 16 23 31 8 #取20個數(shù),計算的結(jié)果四舍五入: 4 7 12 22 24 27 10 #取25個數(shù),計算的結(jié)果四舍五入: 7 8 13 17 24 30 6 #取50個數(shù),計算的結(jié)果四舍五入: 4 10 14 18 23 29 8 #取100個數(shù),計算的結(jié)果四舍五入: 5 11 15 19 24 29 8 #取500個數(shù),計算的結(jié)果四舍五入: 5 10 15 20 24 29 9 #取1000個數(shù),計算的結(jié)果四舍五入: 5 10 14 19 24 29 9 #取1939個數(shù),計算的結(jié)果四舍五入: 5 10 14 19 24 29 9
看來預(yù)測中獎?wù)媸怯行╇y度,隨機性太高,雙色球預(yù)測案例,只是為了讓入門數(shù)據(jù)分析的朋友有些思路,要想中大獎還是有難度的,多做好事善事多積德行善吧。
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)學(xué)運算技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
一次性徹底講透Python中pd.concat與pd.merge
本文主要介紹了一次性徹底講透Python中pd.concat與pd.merge,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
Python3批量創(chuàng)建Crowd用戶并分配組
這篇文章主要介紹了Python3批量創(chuàng)建Crowd用戶并分配組,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-05-05
Linux下升級安裝python3.8并配置pip及yum的教程
這篇文章主要介紹了Linux下升級安裝python3.8并配置pip及yum的教程,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-01
詳解Python如何實現(xiàn)壓縮與解壓縮數(shù)據(jù)
當(dāng)數(shù)據(jù)量大的時候,自然而然想到的就是對數(shù)據(jù)進行壓縮,這篇文章主要為大家介紹了Python可以實現(xiàn)壓縮與解壓縮數(shù)據(jù)的相關(guān)模塊的使用,希望對大家有所幫助2024-02-02
pandas中字典和dataFrame的相互轉(zhuǎn)換
有時候需要把dic轉(zhuǎn)換為DataFrame格式,便于查看和存儲,下面這篇文章主要給大家介紹了關(guān)于pandas中字典和dataFrame相互轉(zhuǎn)換的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-09-09
TensorFlow自定義損失函數(shù)來預(yù)測商品銷售量
這篇文章主要介紹了TensorFlow自定義損失函數(shù)——預(yù)測商品銷售量,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02
flask-SQLALchemy連接數(shù)據(jù)庫的實現(xiàn)示例
sqlalchemy是數(shù)據(jù)庫的orm框架,讓我們操作數(shù)據(jù)庫的時候不要再用sql語句了,本文就介紹了flask-SQLALchemy連接數(shù)據(jù)庫的實現(xiàn)示例,感興趣的可以了解一下2022-06-06
Python如何在windows環(huán)境安裝pip及rarfile
這篇文章主要介紹了Python如何在windows環(huán)境安裝pip及rarfile,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06
Python標(biāo)準(zhǔn)庫筆記struct模塊的使用
這篇文章主要介紹了Python標(biāo)準(zhǔn)庫筆記struct模塊的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02
Python網(wǎng)絡(luò)編程實戰(zhàn)之爬蟲技術(shù)入門與實踐
這篇文章主要介紹了Python網(wǎng)絡(luò)編程實戰(zhàn)之爬蟲技術(shù)入門與實踐,了解這些基礎(chǔ)概念和原理將幫助您更好地理解網(wǎng)絡(luò)爬蟲的實現(xiàn)過程和技巧,需要的朋友可以參考下2023-04-04

