Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i fix "If using all scalar values, you must pass an index" error?

I am manually trying to build a linear regression model for understanding purpose without using the builtin function. I am getting the error while plotting the regression line. Kindly help me fix it.

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import seaborn as sb

data = {'X': list(np.arange(0,10,1)), 'Y': [1,3,2,5,7,8,8,9,10,12]}
df = pd.DataFrame(data) 
df2 = pd.DataFrame(np.ones(10), columns = ['ones'])
df_new = pd.concat([df2,df], axis = 1)

X = df_new.loc[:, ['ones', 'X']].values
Y = df_new['Y'].values.reshape(-1,1)

theta = np.array([0.5, 0.2]).reshape(-1,1)

Y_pred = X.dot(theta)
sb.lineplot(df['X'].values.reshape(-1,1),Y_pred)
plt.show()

Error message:

If using all scalar values, you must pass an index

like image 535
Pavan Avatar asked Nov 07 '25 12:11

Pavan


1 Answers

You are passing a 2d array, while seaborn's lineplot expects a 1d array (or a pandas column which is basically same). So change it to

sb.lineplot(df['X'].values,Y_pred.reshape(-1))  
like image 146
Julkar9 Avatar answered Nov 09 '25 08:11

Julkar9



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!