Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Creating functions to calculate the sum of squares
- def ssXY(x, y):
- """Calculates the Sum of Squares for 'x' and 'y'."""
- n = y.size
- return sum(x*y) - ((sum(x)*sum(y))/n)
- def ssXX(x):
- """Calculates the Sum of Squares for 'x'."""
- n = x.size
- return sum(x*x) - ((sum(x)*sum(x))/n)
- # Finding/Printing the value of the slope, intercept, and the RMSE to csv
- slope = ssXY(features_DSH, Target_DSH)/ssXX(features_DSH)
- intercept = Target_DSH.mean() - slope * features_DSH.mean()
- slope_ = 'The slope is:'
- intercept_ = 'The intercept is:'
- outputDF = pd.DataFrame.from_dict({slope_:[slope], intercept_: [intercept],
- 'RMSE:': np.sqrt(metrics.mean_squared_error(Target_DSH, predict_Linear))})
- # Writing to file
- counter = 1
- filepath = '/Users/Chris/Documents/Programming_Practice/gistHub/slrVIZ_' + str(counter) + '.csv'
- with open(filepath, 'w') as f:
- outputDF.to_csv(path_or_buf= f)
- # Printing the value of the slope, intercept, and the RMSE to csv
- print(slope_, " ", slope, ' and ', intercept_, " ", intercept)
- print('\nRMSE: ', np.sqrt(metrics.mean_squared_error(Target_DSH, predict_Linear)))
- # Plotting the points in a scatter plot
- plt.figure(figsize= (10, 5))
- plt.scatter(features_DSH, Target_DSH,marker='*',color = '#1f77b4');
- # Plotting the model
- plt.plot(features_DSH, slope* features_DSH + intercept, color = 'k')
- plt.title('Linear Regression Model')
- plt.xlabel('Features')
- plt.ylabel('Target');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement