Python中浮点错误的文本无效

Python中浮点错误的文本无效,python,scikit-learn,linear-regression,sklearn-pandas,Python,Scikit Learn,Linear Regression,Sklearn Pandas,我正在尝试使用sklearn并使用sklearn库在Python中执行线性回归 这是我用来训练和拟合模型的代码,当我运行predict函数调用时,我得到了错误 train, test = train_test_split(h1, test_size = 0.5, random_state=0) my_features = ['bedrooms', 'bathrooms', 'sqft_living', 'sqft_lot', 'floors', 'zipcode'] trainInp = tr

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

这是我用来训练和拟合模型的代码,当我运行predict函数调用时,我得到了错误

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
以下是测试数据集的前五行


误差是由于系数的大值引起的吗?如何解决这一问题?

您的问题是,您正在将模型拟合到整个数据帧中选定的一组功能上(您确实执行
trainInp=train[my_features]
),但您正试图预测全套功能(
regr.predict(test)
),包括
date
等非数字功能

因此,与其做
regr.predict(test)
,不如做
regr.predict(test[my_features])
。更一般地说,请记住,无论对训练集应用什么样的预处理(规范化、特征选择、PCA等等),都应该应用于测试集

或者,在进行列车测试拆分之前,您可以缩减到感兴趣的功能集:

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

为什么值中有字母
T
?同样,考虑显示你的一些代码……请告诉我们实际抛出错误的代码。你能显示最初几行的<代码>测试< /代码>吗?“错误是因为系数的大值引起的吗?”对不起。我犯的错误是,我为列车输入和目标选择了许多列,但测试数据集中有所有列,因此测试数据集中有导致问题的其他变量。谢谢Mark。刚刚意识到这一点并发表了评论。
my_features = ['bedrooms', 'bathrooms', ...]
train, test = train_test_split(h1[my_features], test_size = 0.5, random_state=0)