Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- import numpy as np
- from sklearn.linear_model import LinearRegression
- data = pd.read_csv("data.csv")
- x = data["Age"].values
- y = data["Blood Pressure"].values
- #Use reshape to turn the x values into 2D arrays:
- x = x.reshape(-1,1)
- #Create the model
- model = LinearRegression().fit(x, y)
- #Find the coefficient, bias, and r squared values.
- #Each should be a float and rounded to two decimal places.
- coef = round(float(model.coef_), 2)
- intercept = round(float(model.intercept_), 2)
- r_squared = model.score(x, y)
- #Print out the linear equation and r squared value
- print("Model's Linear Equation: y=",coef, "x+", intercept)
- print("R Squared value:", r_squared)
- #Predict the the blood pressure of someone who is 43 years old.
- #Print out the prediction
- x_predict = 43
- prediction = model.predict([[x_predict]])
- print("Prediction of someone 48 years old: ", prediction)
Advertisement
Add Comment
Please, Sign In to add comment