MrThoe

4.3.4

Sep 15th, 2022
1,000
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.89 KB | None | 0 0
  1. import pandas as pd
  2. import numpy as np
  3. from sklearn.linear_model import LinearRegression
  4.  
  5. data = pd.read_csv("data.csv")
  6. x = data["Age"].values
  7. y = data["Blood Pressure"].values
  8.  
  9. #Use reshape to turn the x values into 2D arrays:
  10. x = x.reshape(-1,1)
  11.  
  12. #Create the model
  13. model = LinearRegression().fit(x, y)
  14.  
  15. #Find the coefficient, bias, and r squared values.
  16. #Each should be a float and rounded to two decimal places.
  17. coef = round(float(model.coef_), 2)
  18. intercept = round(float(model.intercept_), 2)
  19. r_squared = model.score(x, y)
  20.  
  21.  
  22. #Print out the linear equation and r squared value
  23. print("Model's Linear Equation: y=",coef, "x+", intercept)
  24. print("R Squared value:", r_squared)
  25.  
  26. #Predict the the blood pressure of someone who is 43 years old.
  27. #Print out the prediction
  28. x_predict = 43
  29. prediction = model.predict([[x_predict]])
  30. print("Prediction of someone 48 years old: ", prediction)
Advertisement
Add Comment
Please, Sign In to add comment