I wonder how I can use machine learning to plot multiple linear regression in a figure. I have one independent variable (prices of apartments) and five independent (floor, builtyear, roomnumber, square meter, kr/sqm).

The task is first to use machine learning which gives the predicted values and the actual values. Then you have to plot those values in a figure.

I have used this code:

x_train, x_test, y_train, y_test = tts(xx1, y, test_size=3)

Outcome: LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None,

regr.fit(x_train, y_train)



Outcome: array([2.37671029, 3.91651234, 2.98472475])

np.mean((regr.predict(x_test) - y_test) ** 2)

Outcome: 2.976924398032532e-26

How can I plot the actual values of the dependent variable and the predicted ones in the same figure?

  • 1
    $\begingroup$ This does not present any kind of problem. You have two variables. Plot one on the horizontal axis and the other on the vertical axis. Perhaps you meant to ask a different question that involves more than two dimensions. $\endgroup$
    – Dave
    Aug 31, 2020 at 22:29

1 Answer 1


There might possibly be a better way but one way of doing this is to map the variable to different aesthetics of the graph. I used python and used library plotninne which is as I understand tries to mimic ggplot2 from R.

from plotnine import * # df is the data frame that contains all the vairables as columns (ggplot(df, aes('actual_value', 'predicted_value', color='(dependent_var2)',
size='dependent_var3',alpha='dependent_var4',shape='factor(dependent_var5)')) + geom_point() +theme(legend_title=element_text(size=8), legend_text=element_text(size=4) )

This will give you following graph: enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.