TransWikia.com

Plotting in Multiple Linear Regression in Python 3

Data Science Asked by hael on October 1, 2021

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.

2 Answers

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.

Answered by Siddhi Kiran Bajracharya on October 1, 2021

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.

Answered by lcrmorin on October 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP