0
$\begingroup$

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,
         normalize=False)

regr.fit(x_train, y_train)

Outcome:nothing

regr.predict(x_test)

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?

$\endgroup$
0
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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