3
$\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$
  • $\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 Aug 13 '18 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 Aug 13 '18 at 13:34
  • $\begingroup$ I've tried a 3D plot using mplot3D for a similar problem. Check this out: medium.com/@anupriyaincbe/… $\endgroup$ – Anupriya Thirumurthy Oct 11 '18 at 19:21
1
$\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$

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.