python資料分析6:雙色球 使用線性迴歸演算法預測下期中獎結果__演算法

來源:互聯網
上載者:User

本次將進行下期雙色球號碼的預測,想想有些小激動啊。

代碼中使用了線性迴歸演算法,這個情境使用這個演算法,預測效果一般,各位可以考慮使用其他演算法嘗試結果。

發現之前有很多代碼都是重複的工作,為了讓代碼看的更優雅,定義了函數,去調用,頓時高大上了

#!/usr/bin/python# -*- coding:UTF-8 -*-#匯入需要的包import pandas as pdimport numpy as npimport matplotlib.pyplot as pltimport operatorfrom sklearn import datasets,linear_modelfrom 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 filedef 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 fileRedToCsv('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 datadef 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 modeldef 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 modeldef 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

看來預測中獎真是有些難度,隨機性太高,雙色球預測案例,只是為了讓入門資料分析的朋友有些思路,要想中大獎還是有難度的,多做好事善事多積德行善吧。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.