This is for a 3D scatterplot. The instructions state: Now that we have fit our model, which means that we have computed the optimal model parameters, we can use our model to plot the regression line for the data. Below, I supply you with x_fit
and y_fit
that represent the x- and y-data of the regression line, respectively. All we need to do next is ask the model to predict a z_fit
value for each x_fit
and y_fit
pair by invoking the model's predict()
method. This should make sense when you consider the ordinary least squares linear regression equation for calculating z_fit
:
where are the computed model parameters. You must use
x_fit
and y_fit
as features to be passed together as a DataFrame to the model's predict() method, which will return z_fit
as determined by the above equation. Once you obtain z_fit
, you are ready to plot the regression line by plotting it against x_fit
and y_fit
. My code so far is:
features = df[['x', 'y']]
response = df['z']
#perform linear regression model fitting
from sklearn.linear_model import LinearRegression
model = LinearRegression().fit(features, response)
model
print("Model Coefficients: ", model.coef_)
print("Model Intercept : ", model.intercept_)
Output:
Model Coefficients: [ 8.02602396 15.95639323]
Model Intercept : -8.632876775060822
This is where I'm stuck:
# Plot Curve Fit
x_fit = np.linspace(0,21,1000)
y_fit = x_fit
#ENTER CODE HERE#
# Plot Curve Fit
x_fit = np.linspace(0,21,1000)
y_fit = x_fit
#ENTER CODE HERE#
Assuming
theta1, theta2 = model.coef_
theta3 = model.intercept_
Equation
z_fit = (theta1 * x_fit) + (theta2 * y_fit) + theta3
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.