python-[panda]-[sklearn]-[matplotlib]-Linear Prediction

Source: Internet
Author: User

Article:

http://python.jobbole.com/81215/

Python's library of functions is so powerful! After reading this blog will never use MATLAB ~ ~

This article uses "panda" to read the CSV data, use the Linear_model in "Sklearn" to train the model and make a linear prediction using the "matplotlib" The fitting situation is represented by a graph.

The table below is the table used to train the model:

The code is as follows:

#-*-coding:utf-8-*-" "Created on 2016/11/26@author:chensi" "#Required PackagesImportMatplotlib.pyplot as PltImportNumPy as NPImportPandas as PD fromSklearnImportdatasets, Linear_model fromNumpy.ma.coreImportGetData#Function to get DatadefGet_data (file_name): Data=Pd.read_excel (file_name) X_parameter=[] Y_parameter= []     forSingle_square_feet, Single_price_valueinchZip (data['Square_feet'],data[' Price']): X_parameter.append ([Float (single_square_feet)]) y_parameter.append (float (single_price_value)) returnX_parameter,y_parameter#Function for Fitting we data to Linear modeldefLinear_model_main (x_parameters,y_parameters,predict_value):#Create Linear Regression ObjectRegr =Linear_model. Linearregression () 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_outcomereturnpredictions#Function to show the resutls of linear fit modeldefShow_linear_line (x_parameters,y_parameters):#Create Linear Regression ObjectRegr =Linear_model. Linearregression () Regr.fit (X_parameters, Y_parameters) 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 ()#---------Test---------------#----------------------------X, y = Get_data ("G:/input_data.csv") show_linear_line (x, y)Print(Linear_model_main (x,y,150))#----------------------------#----------------------------

Graph of output:

Example two:

Code:

#-*-coding:utf-8-*-" "Created on 2016/11/26 @author: Chensi" "#Required PackagesImportCSVImportSYSImportMatplotlib.pyplot as PltImportNumPy as NPImportPandas as PD fromSklearnImportdatasets, Linear_model#Function to get DatadefGet_data (file_name): Data=Pd.read_excel (file_name) Flash_x_parameter=[] Flash_y_parameter=[] Arrow_x_parameter=[] Arrow_y_parameter= []     forX1,y1,x2,y2inchZip (data['Flash_episode_number'],data['flash_us_viewers'],data['Arrow_episode_number'],data['arrow_us_viewers']): Flash_x_parameter.append ([Float (x1)]) flash_y_parameter.append (float (y1)) Arrow_x_parameter.a Ppend ([Float (x2)]) arrow_y_parameter.append (float (y2) )returnFlash_x_parameter,flash_y_parameter,arrow_x_parameter,arrow_y_parameter#Function to know which Tv show 'll has more viewersdefmore_viewers (x1,y1,x2,y2): Regr1=Linear_model. Linearregression () regr1.fit (x1, y1) predicted_value1= Regr1.predict (9)    Print(predicted_value1) regr2=Linear_model. Linearregression () Regr2.fit (x2, y2) predicted_value2= Regr2.predict (9)#Print predicted_value1#Print Predicted_value2    ifPredicted_value1 >predicted_value2:Print("The Flash Tv Show would has more than viewers for next week")    Else:        Print("Arrow Tv Show 'll has more viewers for next week") X1,y1,x2,y2= Get_data ('g:/input_data_2.xlsx')#Print X1,y1,x2,y2More_viewers (X1,y1,x2,y2)

Output:

python-[panda]-[sklearn]-[matplotlib]-Linear Prediction

Related Article

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.