首页 文章

Sklearn LinearRegression形状未对齐

提问于
浏览
0

我正在尝试使用sklearn进行线性回归预测 .

我有两个数字列表(年龄以秒表示1970年)和一些价格 .

当我尝试预测我的行时,我收到以下错误:

ValueError:形状(1,1000)和(14697,14697)未对齐:1000(暗淡1)!= 14697(暗淡0)

我的代码:

inkoop = np.asarray(list(df['Bedrag_Inkoop_Ingediend_Mono']),dtype=np.number)
tempT = list(df['dtGeboorteDatum'])

dtAge = np.asarray([(time-datetime.date(1970,1,1)).total_seconds() for time in tempT],dtype=np.number)

dtAgeTestY = dtAge[-1000:]
dtAgeTrainY = dtAge[:-1000]
inkoopTestX = inkoop[-1000:]
inkoopTrainX = inkoop[:-1000]
regr = linear_model.LinearRegression()
regr.fit(inkoopTrainX,dtAgeTrainY.reshape(1,-1))
agePredictY = regr.predict(inkoopTestX.reshape(1,-1))

# The coefficients
print('Coefficients: \n', regr.coef_)

# The mean squared error
print("Mean squared error: %.2f"
      % mean_squared_error(inkoopTest, agePredictY))

# Explained variance score: 1 is perfect prediction
print('Variance score: %.2f' % r2_score(inkoopTest, agePredictY))

plt.scatter(inkoopTestX, dtAgeTestY,  color='black')
plt.plot(dtAgeTestY, agePredictY, color='blue', linewidth=3)

plt.xticks(())
plt.yticks(())

plt.show()

他在agePredictY = regr.predict(inkoopTestX.reshape(1,-1))上出错 . 我也试过没有重塑

1 回答

  • 1

    您正在将 X 输入重新整形为 predict 方法,而不是 fit 方法的 X 输入 . 你应该只做一个或另一个 . 例如

    X = inkoop.reshape(-1, 1) # many rows, one feature
    trainX = X[:1000]
    testX = X[-1000:]
    regr.fit(trainX, trainY)
    regr.predit(testX)
    

相关问题