AAlex
AAlex

Reputation: 63

why can't see regression line?

I do this:

from scipy.stats import linregress
slope, intercept, r_value, p_value, std_err =linregress(df_diesel['DEXUSAL'],df_diesel['price'])

from matplotlib import pyplot as plt 
%matplotlib inline
plt.title("linear regression on diesel price vs exchange")
plt.ylabel("price") 
plt.xlabel("DEXUSAL")
line = [slope*xi + intercept for xi in df_diesel['DEXUSAL']]
plt.plot(df_diesel['DEXUSAL'],line,'r-', linewidth=3)
plt.scatter(df_diesel['DEXUSAL'], df_diesel['price'])
plt.gcf().autofmt_xdate()
plt.show()

Dataframe is like this:

 > DATE              DEXUSAL Fuel price 

1543795200000000000 0.736 Diesel 1529

1543795200000000000 0.736 Diesel 1599

1543795200000000000 0.736 Diesel 1457

1543795200000000000 0.736 Diesel 1559

1543795200000000000 0.736 Diesel 1479

there is graph turn out ,but without the regression line?

Also, when I do this:

x = 0.75
y = slope * (x) + intercept
y

It also has a weird result.

How to solve this?

Upvotes: 2

Views: 81

Answers (1)

iblasi
iblasi

Reputation: 1307

@AAlex, you are doing a linear regression using 'DEXUSAL' in X axis where in your example DataFrame are all the same value at least. So the SciPy function is making a division by zero.

As a result you have a slope = nan and intercept = nan (nan means Not a Number) and that's the reason that it does not plot it. Thus slope is inf and intercept -inf

enter image description here

Next time show each value on screen and you may guess what it really happens.

Upvotes: 1

Related Questions