首页 > 解决方案 > Pandas LinRegress of Subplots 导致值错误

问题描述

我有一个包含房屋地址的数据框以及一个列出房屋半径 5 英里内最近高中的高中名称排名(满分 5 星)的 api 调用。如果没有高中,则将 NaN 添加到该行。

我正在尝试制作 2 个散点图的子图,在其中比较附近有高中与附近没有高中的房屋大小和房屋价格。

这就是我曾经尝试实现的目标:

x_axis = data_df.loc[data_df['High School'].isna(), ["Total Sq Ft"]]
y_axis = data_df.loc[data_df['High School'].isna(), ["Sale Price"]]
x_axis_2 = data_df.loc[data_df['High School'].notna(), ["Total Sq Ft"]]
y_axis_2 = data_df.loc[data_df['High School'].notna(), ["Sale Price"]]

fig = plt.figure()
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)
(ax1slope, ax1intercept, ax1rvalue, ax1pvalue, ax1stderr) = st.linregress(x_axis, y_axis)
ax1_regress_values = x_axis * ax1slope + ax1intercept
ax1_line_eq = "y = " + str(round(ax1slope,2)) + "x + " + str(round(ax1intercept,2))
ax1.annotate(ax1_line_eq,(0,2000000),fontsize=10,color="blue")
ax1.plot(x_axis, ax1_regress_values,"b--")
ax1.scatter(x_axis, y_axis, s=10, c='b', marker="s", label='No HS nearby')

(ax2slope, ax2intercept, ax2rvalue, ax2pvalue, ax2stderr) = st.linregress(x_axis_2, y_axis_2)
ax2_regress_values = x_axis_2 * ax2slope + ax2intercept
ax2_line_eq = "y = " + str(round(ax2slope,2)) + "x + " + str(round(ax2intercept,2))
ax2.annotate(ax2_line_eq,(4000,3000000),fontsize=10,color="red")
ax2.plot(x_axis_2, ax2_regress_values,"r--")
ax2.scatter(x_axis_2, y_axis_2, s=10, c='r', marker="o", label='HS nearby')
ax1.legend(loc='best')
ax2.legend(loc='best')
plt.show()

这是出现的错误:

ValueError                                Traceback (most recent call last)
<ipython-input-28-4f54bd25003b> in <module>
      9 ax1 = fig.add_subplot(211)
     10 ax2 = fig.add_subplot(212)
---> 11 (ax1slope, ax1intercept, ax1rvalue, ax1pvalue, ax1stderr) = st.linregress(x_axis, y_axis)
     12 ax1_regress_values = x_axis * ax1slope + ax1intercept
     13 ax1_line_eq = "y = " + str(round(ax1slope,2)) + "x + " + str(round(ax1intercept,2))

~\anaconda3\lib\site-packages\scipy\stats\_stats_mstats_common.py in linregress(x, y)
    114 
    115     # average sum of squares:
--> 116     ssxm, ssxym, ssyxm, ssym = np.cov(x, y, bias=1).flat
    117     r_num = ssxym
    118     r_den = np.sqrt(ssxm * ssym)

ValueError: too many values to unpack (expected 4)

我在我的 jupyter 笔记本中使用 %matplotlib 笔记本,并且在添加用于 linregressing 2 个散点图的代码之前,子图脚本运行良好。有没有办法让 linregress 显示出来?

标签: pythonpython-3.xpandasdataframematplotlib

解决方案


推荐阅读