NameError: global name 'linregress' is not defined
NameError: global name 'linregress' is not defined
我有一个函数可以创建散点图并计算拟合和 R 平方。两个小时前我 运行 没有问题,但现在我尝试 运行 它并且我收到了新错误:
NameError: global name 'linregress' is not defined
有问题的部分来自 scipy 库,我在字母中找不到任何错误:
import seaborn as sns
from scipy import stats
def give_me_scatter(x, y, title, xlabel, ylabel):
#The problematic line:
slope, intercept, r_value, p_value, std_err = linregress(x, y)
#print('slope:',slope)
#print('intercept:',intercept)
#print('R:',r_value)
#print('R^2:',(r_value**2))
# use line_kws to set line label for legend
plt.figure(figsize=(7.5,6.5))
ax = sns.regplot(x='NDVI', y='nitrogen', data=merged_data, color='b', line_kws={'label':"y={0:.1f}x+{1:.1f}".format(slope,intercept)})
ax = sns.regplot(x='NDVI', y='nitrogen', data=merged_data,color='b', line_kws={'label':"R^2={0:.3}".format(r_value**2)})
ax.set_title('NDVI vs Nitrogen% ')
# plot legend
ax.legend()
plt.show()
# plot legend
ax.legend()
plt.show()
#The error appears when I try to show the chart:
give_me_scatter(x, y, 'NDVI vs Nitrogen ', 'NDVI', 'Nitrogen %')
我已经重启了几次内核,但在这个阶段仍然出现错误。
您确定没有忘记导入库吗?
例如,
from scipy import stats
def give_me_scatter(x, y, title, xlabel, ylabel):
#The problematic line:
slope, intercept, r_value, p_value, std_err = stats.linregress(x, y)
如果您希望使用 scipy.stats
模块中的 linregress
函数,那么您需要先导入它,以便您的脚本知道它。您可以在脚本顶部定义以下导入语句。
from scipy.stats import linregress
我有一个函数可以创建散点图并计算拟合和 R 平方。两个小时前我 运行 没有问题,但现在我尝试 运行 它并且我收到了新错误:
NameError: global name 'linregress' is not defined
有问题的部分来自 scipy 库,我在字母中找不到任何错误:
import seaborn as sns
from scipy import stats
def give_me_scatter(x, y, title, xlabel, ylabel):
#The problematic line:
slope, intercept, r_value, p_value, std_err = linregress(x, y)
#print('slope:',slope)
#print('intercept:',intercept)
#print('R:',r_value)
#print('R^2:',(r_value**2))
# use line_kws to set line label for legend
plt.figure(figsize=(7.5,6.5))
ax = sns.regplot(x='NDVI', y='nitrogen', data=merged_data, color='b', line_kws={'label':"y={0:.1f}x+{1:.1f}".format(slope,intercept)})
ax = sns.regplot(x='NDVI', y='nitrogen', data=merged_data,color='b', line_kws={'label':"R^2={0:.3}".format(r_value**2)})
ax.set_title('NDVI vs Nitrogen% ')
# plot legend
ax.legend()
plt.show()
# plot legend
ax.legend()
plt.show()
#The error appears when I try to show the chart:
give_me_scatter(x, y, 'NDVI vs Nitrogen ', 'NDVI', 'Nitrogen %')
我已经重启了几次内核,但在这个阶段仍然出现错误。
您确定没有忘记导入库吗? 例如,
from scipy import stats
def give_me_scatter(x, y, title, xlabel, ylabel):
#The problematic line:
slope, intercept, r_value, p_value, std_err = stats.linregress(x, y)
如果您希望使用 scipy.stats
模块中的 linregress
函数,那么您需要先导入它,以便您的脚本知道它。您可以在脚本顶部定义以下导入语句。
from scipy.stats import linregress