RussianDeveloper

Linear Regression Algorithm Adaptation for Anaconda 2018.12 (Python 3.7.2)

Feb 27th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.64 KB | None | 0 0
  1. # coding=utf-8
  2. # Linear Regression Realization (http://dataaspirant.com/2014/12/20/linear-regression-implementation-in-python/)
  3.  
  4. # input_data.csv:
  5. # square_feet;price
  6. # 150;6450
  7. # 200;7450
  8. # 250;8450
  9. # 300;9450
  10. # 350;11450
  11. # 400;15450
  12. # 600;18450
  13.  
  14. # Required Packages
  15. import matplotlib.pyplot as plt
  16. import pandas as pd
  17. import numpy as np
  18. from sklearn import linear_model
  19.  
  20.  
  21. # TODO:
  22.  
  23. # Function to get data
  24. def get_data(file_name):
  25.     global x_parameter, y_parameter
  26.     try:
  27.         data = pd.read_csv(file_name, sep=";")
  28.         x_parameter = []
  29.         y_parameter = []
  30.         # TODO: Replace the names of the fields 'square foot', 'price' for your own values
  31.         for single_square_feet in data['square_feet']:
  32.             x_parameter.append([float(single_square_feet)])
  33.  
  34.         for single_price_value in data['price']:
  35.             y_parameter.append(float(single_price_value))
  36.     except IOError:
  37.         exit('File {0} not found. Exit'.format(file_name))
  38.  
  39.     return x_parameter, y_parameter
  40.  
  41.  
  42. # Function for Fitting our data to Linear model
  43. # noinspection PyPep8Naming
  44. def linear_model_main(x_parameters, y_parameters, predict_value):
  45.     lin_reg_score = []
  46.     # Create linear regression object
  47.     regr = linear_model.LinearRegression()
  48.     regr.fit(x_parameters, y_parameters)
  49.     # noinspection PyArgumentList
  50.     predict_outcome = regr.predict(predict_value)
  51.     r2score = regr.score(x_parameters, y_parameters)
  52.     lin_reg_score.append(r2score)
  53.     predictions = {'intercept': regr.intercept_, 'coefficient': regr.coef_, 'predicted_value': predict_outcome,
  54.                    'r2score': r2score}
  55.     return predictions
  56.  
  57.  
  58. # Function to show the resutls of linear fit model
  59. def show_linear_line(x_parameters, y_parameters):
  60.     # Create linear regression object
  61.     regr = linear_model.LinearRegression()
  62.     regr.fit(x_parameters, y_parameters)
  63.     plt.scatter(x_parameters, y_parameters, color='blue')
  64.     # noinspection PyArgumentList
  65.     plt.plot(x_parameters, regr.predict(x_parameters), color='red', linewidth=4)
  66.     # Supress axis value
  67.     plt.xticks(())
  68.     plt.yticks(())
  69.     plt.show()
  70.  
  71.  
  72. X, Y = get_data('input_data.csv')
  73. predicted_value = np.array([700])  # 700
  74. predicted_value = predicted_value.reshape(-1, 1)
  75. result = linear_model_main(X, Y, predicted_value)
  76. constant = result['intercept']
  77. coefficient = result['coefficient'][0]
  78. predicted_value = result['predicted_value'][0]
  79. r_square = result['r2score']
  80. print('Constant Value: {0}'.format(round(constant, 2)))
  81. print('Coefficient: {0}'.format(round(coefficient, 2)))
  82. print('Predicted Value: {0}'.format(round(predicted_value, 2)))
  83. print('R-Square: {0} ({1}%)'.format(r_square, round(r_square, 4) * 100))
  84. show_linear_line(X, Y)
Advertisement
Add Comment
Please, Sign In to add comment