Python Data analysis 6: Shuangse qiu using linear regression algorithm to predict next-period winning results __ algorithm

Source: Internet
Author: User

This time will be the next issue of SHUANGSE Qiu number forecast, think of a little excitement ah.

The code uses the linear regression algorithm, which uses this algorithm to predict the effect, and you can consider using other algorithms to try the results.

Before discovering a lot of code is repetitive work, in order to make the code look more elegant, define the function, to call, suddenly tall

#!/usr/bin/python #-*-Coding:utf-8-*-#导入需要的包 Import pandas as PD import NumPy as NP import Matplotlib.pyplot as PLT I Mport operator from Sklearn import Datasets,linear_model from Sklearn.linear_model import logisticregression #读取文件 df = P
D.read_table (' Newdata.txt ', header=none,sep= ', ') #读取日期 tdate = sorted (df.loc[:,0]) #将以列项为数据, takes out the ball number, writes it to the CSV file, and takes 50 rows of data # 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.clo Se () #调用取号码函数 # 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 for numid data, y_parameter for Number data # Function to get Data def Get_data (file_name): data = Pd.read_csv (file_name) x_parameter = [] Y_parameter = [] for Single_square_fee
        T, 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 DA
    TA 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.PR Edict (predict_value) predictions = {} predictions[' intercept '] = regr.intercept_ predictions[' coefficient '] = Regr.coef_ predictions[' predicted_value ' = predict_outcome return predictions #获取预测结果函数 def get_predicted_num (i NpUtfile,num): X,y = Get_data (inputfile) predictvalue = Wuyi result = Linear_model_main (x,y,predictvalue) prin T "num" + str (num) + "Intercept value", result[' Intercept '] print "num" + str (num) + "coefficient", result[' coeffic Ient '] print "num" + str (num) + "predicted value:", result[' Predicted_value '] #调用函数分别预测红球, Blue ball get_predicted_num (' Redn Um1data.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) # Get X,y Data Predictive results # x,y = get_data (' rednum1data.csv ') # predictvalue = # result = Linear_model_m Ain (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 () #显示模型图像, if paint is needed, "get X , Y Data Prediction Results "This comment is removed, the call function predicts the red ball, the blue ball respectively," The Code annotation under # show_linear_line (x,y)

Paint results:


Forecast 2016-05-15 Lottery Results:

Actual lottery results: modified


The following are forecast values:

#取5个数, the result of the calculation
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]

Rounding Result:
2 9 12 16 28 33 5


#取12个数, the result of the calculation is rounded:
3 7 12 15 24 30 7

#取15个数, the result of the calculation is rounded:
4 7 13 15 25 31 7

#取18个数, the result of the calculation is rounded:
4 8 13 16 23 31 8

#取20个数, the result of the calculation is rounded:
4 7 12 22 24 27 10

#取25个数, the result of the calculation is rounded:
7 8 13 17 24 30 6

#取50个数, the result of the calculation is rounded:
4 10 14 18 23 29 8

#取100个数, the result of the calculation is rounded:
5 11 15 19 24 29 8

#取500个数, the result of the calculation is rounded:
5 10 15 20 24 29 9

#取1000个数, the result of the calculation is rounded:
5 10 14 19 24 29 9

#取1939个数, the result of the calculation is rounded:
5 10 14 19 24 29 9

It seems to predict the jackpot is really some difficulty, randomness is too high, SHUANGSE Qiu forecast case, just for getting started data analysis friend some ideas, to want to win the lottery is still difficult, do good deeds and more virtuous charity.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.