Python 中浮点错误的无效文字

Invalid literal for Float error in Python

我正在尝试使用 sklearn 并在 Python 中使用 sklearn 库执行线性回归。

这是我用来训练和拟合模型的代码,当我 运行 预测函数调用时出现错误。

train, test = train_test_split(h1, test_size = 0.5, random_state=0)

my_features = ['bedrooms', 'bathrooms', 'sqft_living', 'sqft_lot', 'floors', 'zipcode']
trainInp = train[my_features]

target = ['price']
trainOut = train[target]

regr = LinearRegression()

# Train the model using the training sets

regr.fit(trainInp, trainOut)

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

testPred = regr.predict(test)

拟合模型后,当我尝试使用测试数据进行预测时,它抛出以下错误

Traceback (most recent call last):
  File "C:/Users/gouta/PycharmProjects/MLCourse1/Python.py", line 52, in <module>
    testPred = regr.predict(test)
  File "C:\Users\gouta\Anaconda2\lib\site-packages\sklearn\linear_model\base.py", line 200, in predict
    return self._decision_function(X)
  File "C:\Users\gouta\Anaconda2\lib\site-packages\sklearn\linear_model\base.py", line 183, in _decision_function
    X = check_array(X, accept_sparse=['csr', 'csc', 'coo'])
  File "C:\Users\gouta\Anaconda2\lib\site-packages\sklearn\utils\validation.py", line 393, in check_array
    array = array.astype(np.float64)
ValueError: invalid literal for float(): 20140604T000000

线性回归模型的系数为

('Coefficients: \n', array([[ -5.04902429e+04,   5.23550164e+04,   2.90631319e+02,
         -1.19010351e-01,  -1.25257545e+04,   6.52414059e+02]]))

以下是测试数据集的前五行

是不是系数值太大导致的错误?如何解决这个问题?

您的问题是您正在根据整个数据框中选定的一组特征拟合模型(您这样做 trainInp = train[my_features]),但您试图预测完整的特征集(regr.predict(test)),包括非数字特征,如 date.

因此,您应该 regr.predict(test[my_features]) 而不是 regr.predict(test)。更一般地说,请记住无论您对训练集应用何种预处理(归一化、特征选择、PCA 等),您也应该应用到测试集。

或者,您可以在进行训练-测试拆分之前减少到感兴趣的特征集:

my_features = ['bedrooms', 'bathrooms', ...]
train, test = train_test_split(h1[my_features], test_size = 0.5, random_state=0)