Python data analysis-two-color ball-based linear regression algorithm to predict the next winning results example, python winning results

Source: Internet
Author: User

Python data analysis-two-color ball-based linear regression algorithm to predict the next winning results example, python winning results

This article describes how to use a two-color ball in Python data analysis to predict the next winning result based on a linear regression algorithm. We will share this with you for your reference. The details are as follows:

I have talked about various algorithms for two-color balls. Here we will make predictions for the next two-color ball number. I'm a little excited.

The linear regression algorithm is used in the Code. This algorithm is used in this scenario and the Prediction effect is average. You can use other algorithms to try the results.

I found that a lot of code is repetitive. In order to make the Code look more elegant, I defined the function and called it. Then I suddenly got taller.

#! /Usr/bin/python #-*-coding: UTF-8-*-# import the required package import pandas as pdimport numpy as npimport matplotlib. pyplot as pltimport operatorfrom sklearn import datasets, linear_modelfrom sklearn. linear_model import LogisticRegression # Read the file df = pd.read_table('newdata.txt ', header = None, sep =', ') # Read date tdate = sorted (df. loc [:, 0]) # extract the ball number from the column and write it into the csv file, and take 50 rows of data # 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'). insert (0, 'numid, number') s = '\ n '. join (a) fp = file (csv_name, 'w') fp. write (s) fp. close () # Call the number Function # create parameter ') # obtain data. X_parameter indicates numid and Y_parameter indicates 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 (parameters)]) Evaluate (float (parameters) return X_parameter, Y_parameter # Train the Linear model # 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 # obtain the prediction result function 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: "prediction ', 1) # obtain the prediction result of X and Y data # 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 () # display the model image. If you need to draw a picture, remove the comment "Get X, Y data prediction result, # show_linear_line (X, Y)

Drawing result:

Prediction results:

Actual results: 05 06 10 16 22 26 11

The predicted values are as follows:

# Obtain the number 5. The calculated result is num 1 Intercept value 5.666666667num 1 coefficient [-0.6] num 1 Predicted value: [2.06666667] num 2 Intercept value 7.33333333333num 2 coefficient [0.2] num 2 Predicted value: [8.53333333] num 3 Intercept value 14.636947619num 3 coefficient [-0.51428571] num 3 Predicted value: [11.53333333] num 4 Intercept value 17.7610947619num 4 coefficient [-0.37142857] num 4 Predicted value: [15.53333333] num 5 Intercept value 21.7142857143num 5 coefficient [1.11428571] num 5 Predicted value: [28.4] num 6 Intercept value limit 6 coefficient [0.65714286] num 6 Predicted value: [32.46666667] num 1 Intercept value 9.57142857143num 1 coefficient [-0.82857143] num 1 Predicted value: [4.6]

Rounding result:

2 9 12 16 28 33 5

# Take 12 numbers, and the calculation result is rounded to: 3 7 12 15 24 30 7 # Take 15 numbers. The calculation result is rounded to: 4 7 13 15 25 31 7 # Take 18 numbers, the calculation result is rounded to 4 8 13 16 23 31 8 #20 numbers, and the calculation result is rounded to 4 7 12 22 24 27 10 #25 numbers. The calculation result is rounded: 7 8 13 17 24 30 6 # obtain 50 numbers. The calculation result is rounded to 4 10 14 18 23 29 8 # The number is 100. The calculation result is rounded to 5: 5 11 15 19 24 29 8 # Take 500 numbers. The calculation result is rounded to 5 10 15 20 24 29 9 # take 1000 numbers. The calculation result is rounded to 5: 5 10 14 19 24 29 9 # obtain 1939 numbers. The calculation result is rounded to 5 10 14 19 24 29 9.

It seems that it is really difficult to predict the winning prize. The Randomness is too high. The two-color ball prediction case is just to make friends of beginners data analysis have some ideas. It is still difficult to win the prize, do more good deeds and good deeds.

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.