我有一个函数返回一个numpy数组,该数组将用于 MatPlotLib图。
def extentx(xp, länge):
xp = np.array(x)
today = datetime.datetime.now()
for d in range(1, länge):
local = (today + datetime.timedelta(days=d)).strftime("%d/%m/%y")
xp = np.append(xp, [[local]])
x_ex = xp.reshape(-1, 1)
return x_ex
def extenti(data, länge):
i_ex = np.array(data.index)
for d in range(len(i_ex) + 1, len(i_ex) + 30):
i_ex = np.append(i_ex, [[d]])
i_ex = i_ex.reshape(-1, 1)
return i_ex
def predict(x, y):
reg = linear_model.LinearRegression()
X_train, X_test, y_train, y_test, i_train, i_test = train_test_split(x, y, data.index, test_size=0.2, random_state=11)
X_train = X_train.reshape(-1, 1)
X_test = X_test.reshape(-1, 1)
i_train = i_train.values.reshape(-1, 1)
i_test = i_test.values.reshape(-1, 1)
reg.fit(i_train, y_train)
yp = reg.predict(extenti(data, 30))
return yp
plt.plot(extentx(xp, 30), predict(x, y), "-r")
plt.show()
Traceback (most recent call last):
File "C:\Users\simon\Downloads\tryout.py", line 63, in <module>
plt.plot(extentx(xp, 30), predict(x, y), "-r")
: unhashable type: 'numpy.ndarray'
我知道错误出在extentx函数中,因为当我使用x值(例如“ 1、2、3、4”)运行它时,什么也没发生。
这将返回错误“ TypeError:无法散列的类型:numpy.ndarray”,该错误已在标题中显示。 怎么解决?