这个数据集图正在使用最佳拟合线,现在它已经断了
This dataset graph was working withe the line of best fit now it has broke
此代码运行正常,已恢复到运行正常但现在无法运行的状态。谁能帮忙解决错误
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()
收到错误
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,)
新错误一个seaborn被卸载并重新安装
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
为了简单起见,显示您收到的错误会很有用,而不必 运行 代码。 seaborn 似乎遇到如下错误:
ImportError: cannot import name 'remove_na'
之前有人问过这个问题,据推测是由以下问题引起的:“问题是 seaborn 似乎正在使用来自 pandas 的私有方法”。 (参考:I am facing this issue in seaborn import:)
此处还给出了解决此错误的建议,即卸载 seaborn 并重新安装。
编辑以下评论:
您的代码的问题在于它的编写顺序不正确(您在为变量 y = a*x + b
赋值之前调用了 a
中的变量)。 运行 以下(根据上面的错误,我刚刚更改了顺序并注释掉了导致形状问题的额外 x 调用):
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()
您将收到以下输出:
此代码运行正常,已恢复到运行正常但现在无法运行的状态。谁能帮忙解决错误
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()
收到错误
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,)
新错误一个seaborn被卸载并重新安装
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
为了简单起见,显示您收到的错误会很有用,而不必 运行 代码。 seaborn 似乎遇到如下错误:
ImportError: cannot import name 'remove_na'
之前有人问过这个问题,据推测是由以下问题引起的:“问题是 seaborn 似乎正在使用来自 pandas 的私有方法”。 (参考:I am facing this issue in seaborn import:) 此处还给出了解决此错误的建议,即卸载 seaborn 并重新安装。
编辑以下评论:
您的代码的问题在于它的编写顺序不正确(您在为变量 y = a*x + b
赋值之前调用了 a
中的变量)。 运行 以下(根据上面的错误,我刚刚更改了顺序并注释掉了导致形状问题的额外 x 调用):
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()
您将收到以下输出: