这只是我在一个教程中复制的一个代码示例,我不断遇到这个错误。有人能给我一个明确且详细的解决方案吗?
import matplotlib.pyplot as pltimport numpy as npfrom sklearn import datasets,linear_modelhouse_price=[245, 312, 279, 308, 199, 405, 324, 319, 255]size= [1400, 1600, 1700, 1875, 1100, 1550, 2350, 2450, 1425, 1700]size2 = np.array(size).reshape((-1,1))print(size2)regr = linear_model.LinearRegression()regr.fit(size2, house_price)print("coefficient: \n" ,regr.coef_)print("intercept: \n", regr.intercept_)def graph(formula, x_range): x = np.array(x_range) y = eval(formula) plt.plot(x,y)
回答:
房价的数量与大小(数组中的元素)数量不匹配,它们需要一致。否则,可以在缺失值的位置添加np.nan
(或一些填补值如中位数)。例如:
house_price=[245, 312, 279, 308, 199, 405, 324, 319, 255, np.nan]size= [1400, 1600, 1700, 1875, 1100, 1550, 2350, 2450, 1425, 1700]