5
$\begingroup$

So I'm working on linear regression. So far I've managed to plot in linear regression, but currently I'm on Multiple Linear Regression and I couldn't manage to plot it, I can get some results if I enter the values manually, but I couldn't manage to plot it. Below is my code block and dataset and error, what can i change to plot it?

Dataset:

deneyim maas    yas
0.5 2500    22
0   2250    21
1   2750    23
5   8000    25
8   9000    28
4   6900    23
15  20000   35
7   8500    29
3   6000    22
2   3500    23
12  15000   32
10  13000   30
14  18000   34
6   7500    27

Code block:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.linear_model import LinearRegression

dataset = pd.read_csv("multiple-linear-regression-dataset.csv",sep = ";")

x = dataset.iloc[:,[0,2]].values
y = dataset.maas.values.reshape(-1,1)

multiple_lr = LinearRegression()
multiple_lr.fit(x,y)

b0 = multiple_lr.intercept_
b1 = multiple_lr.coef_
b2 = b1

multiple_lr.predict(np.array([[10,35],[5,35]]))

array = np.array([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]).reshape(-1,1)
y_head = multiple_lr.predict(array)

plt.scatter(x,y)
plt.plot(array, y_head, color = "red")
plt.show()

It says ValueError: shapes (16,1) and (2,1) not aligned: 1 (dim 1) != 2 (dim 0) when I try to compile it.

$\endgroup$
3
  • $\begingroup$ What does this code actually give then? Can you post a screenshot of the result? Or is there a bug? The plotting part seems ok to me, but is hard to test without having that dataset. $\endgroup$
    – n1k31t4
    Commented Aug 13, 2018 at 13:06
  • $\begingroup$ It seems like X has two features (iloc[:, [0, 2]]). But then you try to use the regression on that [0, 1, ..., 15] array that has only one feature. And even after you get the predictions, the visualization will have to be 3D (because of the two Xs plus the Y). $\endgroup$
    – Mephy
    Commented Aug 13, 2018 at 13:34
  • $\begingroup$ I've tried a 3D plot using mplot3D for a similar problem. Check this out: medium.com/@anupriyaincbe/… $\endgroup$ Commented Oct 11, 2018 at 19:21

2 Answers 2

2
$\begingroup$

You cannot plot graph for multiple regression like that. Multiple regression yields graph with many dimensions. The dimension of the graph increases as your features increases. In your case, X has two features. Scatter plot takes argument with only one feature in X and only one class in y.Try taking only one feature for X and plot a scatter plot. By doing so you will be able to study the effect of each feature on the dependent variable (which i think is more easy to comprehend than multidimensional plots).I think your issue should resolve.

$\endgroup$
0
$\begingroup$

See my answer over here : Plotting multivariate linear regression

The catch is that you can't plot more than three variable at once, so you are left with :

  • observing the interactions of the expected output with one to three variable, either by plotting the observed (or predicted) y against your variable or by using y as a color. This won't work for more than three variables.

  • Plotting the contours of the output of the model. It has some limitations as you need to fix a value for variables that are not plotted. It shouldn't really work for more than two variables.

So, generally speaking (quite independently of the model you want to use), you can only observe the interaction of y to only a few variables at once. You don't really get good visualisation with more than one variable. Plotting y against two variable is ok to see an interraction. Using three variables (and y as a color) is not really good ihmo, As you don't really see anything.

$\endgroup$

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.