0

This code was functioning a has been reverted to the state were it was functioning but is now unfunctionial. Can someone help resolve the error

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

sns.set_theme(style="whitegrid")

iris = sns.load_dataset("iris")

dataset1 = iris

from scipy.stats import linregress

x = np.linspace(0,7,8)
y = a*x + b
plt.plot(x, y, 'r', label='Y = aX + b')

x = dataset1['petal_length'].to_numpy(dtype = float)
y = dataset1['sepal_length'].to_numpy(dtype = float)

a, b, r ,p, stderr = linregress(x,y)
print("\na: {:.4f}".format(a))
print("\nb: {:.4f}".format(b))
print("\nR Sqaured: {:.4f}".format(r**2))


sns.scatterplot(data=dataset1, x = 'petal_length', y = 'sepal_length')
plt.show()

error received

ValueError                                Traceback (most recent call last)
<ipython-input-185-aa2ea9694f18> in <module>
      2 
      3 x = np.linspace(0,7,8)
----> 4 y = a*x + b
      5 plt.plot(x, y, 'r', label='Y = aX + b')
      6 

ValueError: operands could not be broadcast together with shapes (3,4) (8,) 

new error one seaborn was uninstalled and reinstalled

NameError                                 Traceback (most recent call last)
<ipython-input-27-0c657297db5a> in <module>
      2 
      3 x = np.linspace(8,7,8)
----> 4 y = a*x + b (1)
      5 plt.plot(x, y, 'r', label='Y = aX + b')
      6 

NameError: name 'a' is not defined
RuthC
  • 1,269
  • 1
  • 10
  • 21

1 Answers1

0

Would have been useful to show the error you are receiving for simplicity rather than having to run the code. It seems you are facing an error with seaborn as follows:

ImportError: cannot import name 'remove_na'

This has been asked before and is supposedly caused by the following issue: "The problem is that seaborn seems to be using a private method from pandas". (Ref: I am facing this issue in seaborn import:) Advice is also given here for resolving this error i.e., uninstall seaborn and install it again.

EDIT FOLLOWING COMMENT:

The issue with your code is that it is written in an incorrect order (you are calling variable a in y = a*x + b before you have assigned it a value). Run the following (I have just changed the order and commented out the additional x call causing you a shape issue, as per your error above):

import pandas as pd
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import linregress

sns.set_theme(style="whitegrid")

iris = sns.load_dataset("iris")

dataset1 = iris

x = dataset1['petal_length'].to_numpy(dtype = float)
y = dataset1['sepal_length'].to_numpy(dtype = float)

a, b, r ,p, stderr = linregress(x, y)
y = a * x + b

# x = np.linspace(0,7,8)

plt.plot(x, y, 'r', label='Y = aX + b')

print("\na: {:.4f}".format(a))
print("\nb: {:.4f}".format(b))
print("\nR Sqaured: {:.4f}".format(r**2))


sns.scatterplot(data=dataset1, x = 'petal_length', y = 'sepal_length')
plt.show()

And you will receive the following output:

Code Output

York
  • 67
  • 6
  • Thanks forgot to add error to question. Have tries uninstalling and reinstalling seaborn but so far this has not resolved error – mrlobaloba Nov 19 '21 at 14:53
  • @mrlobaloba Have you uninstalled seaborn using 'pip uninstall seaborn' and installed as 'pip install seaborn', both at CMD? I have just done this and can now run your code fine. Try restarting your terminal if so. – York Nov 19 '21 at 15:00
  • yeah just did now it is displaying thie new error on the question – mrlobaloba Nov 19 '21 at 15:08
  • @mrlobaloba I have added to my answer above FYI. – York Nov 19 '21 at 15:30