Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. # Creating functions to calculate the sum of squares
  2. def ssXY(x, y):
  3. """Calculates the Sum of Squares for 'x' and 'y'."""
  4. n = y.size
  5. return sum(x*y) - ((sum(x)*sum(y))/n)
  6.  
  7.  
  8. def ssXX(x):
  9. """Calculates the Sum of Squares for 'x'."""
  10. n = x.size
  11. return sum(x*x) - ((sum(x)*sum(x))/n)
  12.  
  13. # Finding/Printing the value of the slope, intercept, and the RMSE to csv
  14. slope = ssXY(features_DSH, Target_DSH)/ssXX(features_DSH)
  15. intercept = Target_DSH.mean() - slope * features_DSH.mean()
  16. slope_ = 'The slope is:'
  17. intercept_ = 'The intercept is:'
  18. outputDF = pd.DataFrame.from_dict({slope_:[slope], intercept_: [intercept],
  19. 'RMSE:': np.sqrt(metrics.mean_squared_error(Target_DSH, predict_Linear))})
  20.  
  21. # Writing to file
  22. counter = 1
  23. filepath = '/Users/Chris/Documents/Programming_Practice/gistHub/slrVIZ_' + str(counter) + '.csv'
  24. with open(filepath, 'w') as f:
  25. outputDF.to_csv(path_or_buf= f)
  26.  
  27. # Printing the value of the slope, intercept, and the RMSE to csv
  28. print(slope_, " ", slope, ' and ', intercept_, " ", intercept)
  29. print('\nRMSE: ', np.sqrt(metrics.mean_squared_error(Target_DSH, predict_Linear)))
  30.  
  31.  
  32. # Plotting the points in a scatter plot
  33. plt.figure(figsize= (10, 5))
  34. plt.scatter(features_DSH, Target_DSH,marker='*',color = '#1f77b4');
  35.  
  36. # Plotting the model
  37. plt.plot(features_DSH, slope* features_DSH + intercept, color = 'k')
  38. plt.title('Linear Regression Model')
  39. plt.xlabel('Features')
  40. plt.ylabel('Target');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement